Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically set unsubscribe link in sendgrid using ruby on rails

I am using sendgrid for sending mails. There are around 20 mail templates.

I have set unsubscribe template in the settings of sendgrid app "Subscription Tracking".

My requirement is different texts for unsubscribe link for different mail templates.

Currently, only one static unsubscribe link as set in the sendgrid app "Subscription Tracking" is coming.

Can any one help me how to dynamically set the unsubscribe link in my user_mailer class.

I followed this link To give unsubscribe link in the mail using sendgrid XSMTPAPI header. But I do not know how to implement it in ruby.

Below is the code what I have tried in my user_mailer class yet.

    def abuse_notification(post,current_user,eventid)
     headers['X-SMTPAPI'] = '{"filters":{"subscriptiontrack":{"settings":{"enable":1,"text/html":"Unsubscribe <%Here%>","text/plain":"Unsubscribe Here: <% %>"}}}}'.to_json()
  UserNotifier.smtp_settings.merge!({:user_name => "[email protected]"})

    @recipients  = "[email protected]"
    @from        = "xxxx"
    @subject     = "Report Abuse Notification"
    @sent_on     = Time.now
    @body[:user] = current_user
    @body[:event] = post

  end
like image 456
Krishna Rani Sahoo Avatar asked Aug 06 '13 12:08

Krishna Rani Sahoo


People also ask

How do I create a custom unsubscribe link in SendGrid?

To place your Custom Unsubscribe link into your email, highlight any text within the body of your email and click the small link icon to specify a hyperlink. In the URL field that appears, enter the tag {{{unsubscribe}}} .

How do I unsubscribe from SendGrid?

In the Design Editor, you can also click on the Unsubscribe Module within the Single Send to toggle the Address Line with your sender information and the Unsubscribe Preferences option, which allows you to create a landing page with multiple Unsubscribe Groups that allows recipients to choose which Groups from which ...


1 Answers

You're on the the right track, however to use the SendGrid SMTP API, you'll add the header to each email, rather than to your settings. In your SMTP settings you'll store your (at a minimum) user_name, password, address, the SendGrid Docs, detail configuration further. With ActionMailer you would configure it as follows:

ActionMailer::Base.smtp_settings = {
  :user_name => 'sendgridusername',
  :password => 'sendgridpassword',
  :domain => 'yourdomain.com',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

Once you've configured ActionMailer you'll need to set up your UserNotifier class to look something similar to the following. Each individual method will set the X-SMTPAPI header:

class UserNotifier < ActionMailer::Base
  default :from => "[email protected]"

  def send_message(name, email, message)
    @name = name
    @email = email
    @message = message

    headers['X-SMTPAPI'] = '{"filters":{"subscriptiontrack":{"settings":{"enable":1,"text/html":"Unsubscribe <%Here%>","text/plain":"Unsubscribe Here: <% %>"}}}}'

    mail(
      :to => '[email protected]',
      :from => email,
      :subject => 'Example Message'
    )
  end

end

Note that the X-SMTPAPI header is in JSON, if you wish to convert a Ruby object into JSON, you'll need to use the JSON gem.

like image 138
Nick Q. Avatar answered Nov 01 '22 22:11

Nick Q.