Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

SparkPost Transaction Options #31

Closed
djtarazona opened this issue Mar 27, 2016 · 16 comments
Closed

SparkPost Transaction Options #31

djtarazona opened this issue Mar 27, 2016 · 16 comments

Comments

@djtarazona
Copy link

I recently began testing SparkPost for transactional emails and noticed that it was rewriting all my URLs for click tracking and inserting a small image for open tracking. While I think that is fine for marketing / campaign emails, I'd rather have those features disabled for transactional emails.

To disable these features, you need to include an options object in the JSON payload with the appropriate keys such as:

"options": {
    "open_tracking": false,
    "click_tracking": false,
    "transactional": true
}

There isn't an easy way to do this — you must extend/override the send method in SparkPostTransport. I propose adding an options array to the sparkpost service configuration.

'sparkpost' => [
    'secret' => env('SPARKPOST_SECRET'),
    'options' => [
        'open_tracking' => false,
        'click_tracking' => false,
        'transactional' => true,
    ],
],

Maybe not adding this to the default services.php config, but at least supporting it? I'd be happy to make this improvement and submit a pull request. I feel like most users will send transactional email from Laravel and may want to opt-out of these features as well. Thoughts?

@tomschlick
Copy link

I think this would be easiest by going with what was suggested in #20

Allowing you to send mail via classes instead of the closure syntax would allow for maximum extensibility & overriding.

@slakbal
Copy link

slakbal commented Sep 26, 2016

@djtarazona agree so much with this and yes click tracking on transactional emails is in my opinion also not a good idea, for marking mails its ok, but not for transactional. How did you solve this now? Lately I've noticed e.g. on for example an account confirmation email with the token-link in there; that the "click tracking" is actually firing the links. Yes, you got it, Sparkposts services seems like they are actually checking if the links exists and in the process also fires of e.g. the new account confirmation link. I've litterly send of an email and saw the call came through a couple of seconds later...bit scary. Thus the users are getting confused that their accounts are verified automagically. We really need to be able to switch off the "click tracking" in an Laravel style and not have to extend and override classes etc. It is really a pitty that sparkpost doesn't allow it to be switched of via their web-portal... wonder why?

@djtarazona
Copy link
Author

@slakbal Ya, I don't know why SparkPost doesn't allow you to disable the click tracking at the account level. However, I made a PR for this and it was merged into Laravel v5.2.40 so you should be able to pass the appropriate options in your services.php config.

My services.php looks like this which has disabled the click tracking for us.

'sparkpost' => [
    'secret'  => env('SPARKPOST_SECRET'),
    'options' => [
        'open_tracking'  => false,
        'click_tracking' => false,
        'transactional'  => true,
    ],
],

@slakbal
Copy link

slakbal commented Sep 26, 2016

@djtarazona really!!! Let me try that out immediately! Thanks so much!

@slakbal
Copy link

slakbal commented Sep 26, 2016

@djtarazona this rocks thanks! Maybe we can get a PR on the laravel documentation for this?

@mehdisbys
Copy link

@djtarazona The options do not seem to be taken into account (Laravel v5.4.13).

I have the options in my services.php file

'sparkpost' => [
    'secret'  => env('SPARKPOST_SECRET'),
    'options' => [
        'open_tracking'  => false,
        'click_tracking' => false,
        'transactional'  => true,
    ],
]

But the email links are still visited by Sparkpost.

I tried these options through Postman and it works (the link is not visited), here is my example :

{
   "options":{
      "open_tracking":false,
      "click_tracking":false,
      "transactional": true
   },
   "metadata":{
      "some_useful_metadata":"testing_sparkpost"
   },
   "recipients":[
      {
         "address":{
            "email":"me@mymail.com"
         },
         "tags":[
            "learning"
         ]
      }
   ],
   "content":{
      "from":{
         "name":"Awesome Company",
         "email":"testing@mycompany.com"
      },
      "subject":"Registration confirmation",
      "text":"Hi please confirm your account by visiting :  http://www.mycompany.com/register/confirm/zySprEXo9Tmm17dYir1gH8LMRFeWKX"
   }
}

Is it still working for you guys ?

@KristofMorva
Copy link

KristofMorva commented Mar 5, 2017

@mehdisbys based on Mail/Transport/SparkPostTransport.php, options is merged right next to recipients and content, however, SparkPost expects options to be under the options key. So actually what you have to do is:

'sparkpost' => [
	'secret' => env('SPARKPOST_SECRET'),
	'options' => [
		'options' => [
                        'open_tracking'  => false,
                        'click_tracking' => false,
                        'transactional'  => true,
		]
	],
],

One options for Laravel, and one for SpartPost. Looks strange, but works under 5.4.

@tomschlick
Copy link

We should probably get it changed from options -> settings in the config file to avoid confusion in the future.

@a-ghasemi
Copy link

@KristofMorva thank you so much, your comment helped me to save my time. rergards.

@djtarazona
Copy link
Author

@tomschlick @KristofMorva @mehdisbys @a-ghasemi Looks like this behavior changed in 5dace8f0. It was not intended for you to have to do:

'sparkpost' => [
	'secret' => env('SPARKPOST_SECRET'),
	'options' => [
		'options' => [
                        'open_tracking'  => false,
                        'click_tracking' => false,
                        'transactional'  => true,
		]
	],
],

But rather:

'sparkpost' => [
	'secret' => env('SPARKPOST_SECRET'),
	'options' => [
                'open_tracking'  => false,
                'click_tracking' => false,
                'transactional'  => true,
	],
],

I'd say this is a bug now. Ping @taylorotwell. I can submit a PR to fix unless Taylor wants to fix it real quick.

@spaceemotion
Copy link

For anybody else stumbling upon this:

I had a very similar problem today, but wanted to set the options on a per-message basis. Searching only brought me to one laracasts thread (https://laracasts.com/discuss/channels/laravel/mailsend-passing-custom-parameters-for-sparkpost-api-call) where they eventually used the SMTP layer with a header in the end. I did not want to use that alternative and managed to add the functionality as a Laravel transport extension.

As suggested by the thread here's a handy package out of my version: https://github.com/spaceemotion/laravel-sparkpost-options

@WallyJ
Copy link

WallyJ commented Dec 19, 2019

Did this get resolved? I am trying to use the Sparkpost Sandbox, and need to set the parameter in the options section and can't get anything to work.

@spaceemotion
Copy link

@WallyJ does my package not help you out? Not sure if it works with Laravel 6 though...

@WallyJ
Copy link

WallyJ commented Dec 19, 2019

I appreciate that you created a package, and I'm still on 5.8, btw.
However, the sandbox functionality should work with Laravel/Mail without the need for any special coding/packages.
I simply need to figure out where to put the sandbox option in Laravel so that Sparkpost receives that parameter as they are expecting. I have Googled and can't find anything on it. And I don't want to go back to straight PHP for this functionality as it adds the Sparkpost package to my project, and removes my ability to use blade templates that I've already created for use with the Laravel/Mail functionality. Quite frankly I am very surprised that I can't find numerous resources online on how to easily connect my Laravel project to the Sparkpost sandbox. These types of basic emailing testing functions should be popular, simple to set up, then switch from dev to production, with a couple of lines of code in each step. Mailtrap is practically no setup. It just works. Sparkpost or Mailgun should be no different. How is it even possible that I seem to be the first developer on the planet to be searching for how to use Laravel with the Sparkpost Sandbox. I fully expected to find 60 Youtube Videos and 100 online blog tutorials with screenshots. I can't find a single one.

@spaceemotion
Copy link

@WallyJ please try using the package and see if it works with it. I had the same experience, not even with just the sandbox (the X-MSYS-API header in general). There's some funky stuff going on under the hood which the solution I came up with tries to circumvent. While not the prettiest, it would at least confirm that it works and could be integrated into Laravel via a PR.

@WallyJ
Copy link

WallyJ commented Dec 19, 2019

So, if I download and try your package, what syntax would I use to add the sandbox option?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants