7

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 => "info@xxxx.example.com"})

    @recipients  = "test@xxx.example.com"
    @from        = "xxxx"
    @subject     = "Report Abuse Notification"
    @sent_on     = Time.now
    @body[:user] = current_user
    @body[:event] = post

  end
Community
  • 1
  • 1
Krishna Rani Sahoo
  • 1,539
  • 1
  • 14
  • 25

1 Answers1

7

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 => "bob@example.com"

  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 => 'george@example.com',
      :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.

Nick Q.
  • 3,947
  • 2
  • 23
  • 37
  • I tried as per your suggestion. But the unsubscribe link is coming as per set in the **Subscription Tracking**, not as I am sending in my code. Should I delete the **Subscription Tracking** setting in `sendgrid` – Krishna Rani Sahoo Aug 07 '13 at 07:15
  • No, don't delete the subscription tracking app on the SendGrid site. Are all the other parts of your email sending (other than the subscription tracking, it's looking as it should)? – Nick Q. Aug 07 '13 at 15:41
  • Yes, other things are working fine in my mailer class except the header. For your clarity I am editing my question to add the details of my mailer class method. – Krishna Rani Sahoo Aug 08 '13 at 06:17
  • Remove the `.toJSON()` from the line `headers['X-SMTPAPI'] = '…'.toJSON()` – Nick Q. Aug 08 '13 at 18:54