Search code examples
phplaravelsmtpsendgridemail-headers

How to dynamically set unsubscribe link in sendgrid using PHP / Laravel


I'd like to dynamically set unsubscribe links using Sendgrid in my PHP/Laravel similar to how its been demonstrated with Ruby on Rails.

I see that I need to set a header with JSON. Can someone help figure out how this header gets integrated with a simple signup confirm code? See below:

Mail::send('emails.confirmation', ['user' => $user, 'resend_text' => $resend_text], function ($m) use ($user, $resend_text) {
    $m->to($user->email)->subject('Confirm your email address'.$resend_text);
});

Solution

  • I ended up setting the substitution text via sendgrid.com and setting the link in my HTML like this <a href="UNSUBSCRIBE_LINK">Unsubscribe</a>. Specifically, on the subscription tracking section on sendgrid.com, I set REPLACEMENT TAG to UNSUBSCRIBE_LINK. So Sendgrid parses your email's HTML and substitutes the hrefs. Pretty cool.