Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for sending email to lots of recipients (Rails + SendGrid)

I will be sending bulk emails from a Rails app and plan on using SendGrid. I am assuming that it is best to send a separate email to each recipient (as opposed to using BCC for all the recipients). If that is true, should I be using something like DelayedJob to queue the messages going over to SendGrid, or would it be safe to throw 500 messages at it all at once? Thanks!

like image 277
Matt Fordham Avatar asked Oct 19 '11 17:10

Matt Fordham


2 Answers

500 messages really isn't that much to SendGrid. It's not even a blip on their radar. I worked for a company that sent out 2.7 million emails in a single month, and even then it's only just a blip.

With the SendGrid API's capabilities, you wouldn't be sending out 500 emails, you would send one email which has a specific SendGrid API header set. Why? Because have you ever tried to send 500 individual email messages and timed how long that takes? How about a single email? The single email's going to be quicker.

The SendGrid API has a Ruby example which is here: https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html.

That's quite long winded and messy, so let me simplify it for you. Basically, you set this in your email:

headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json

SendGrid will then parse this and then send that one email you sent it out to that array of recipients. I seem to recall that they ask you to limit this to about 1000 recipients per email, so it would be wise to split it up over multiple emails if you wanted that. That is when you would bring in something like the delayed_job or resque gems to deal with it.

Oh, and by the way you'll still need to specify a to address for this email just to make the Mail gem happy. We had [email protected] for that.

The SendGrid API will also support filters in their emails, so you can have placeholder strings such as {{ firstname }} and, assuming you send it through with the SMTPAPI header, it will do the "mail merge" on the email and customize them.

It would do you a great deal of good if you read the SendGrid API documentation. It's really useful and what they provide is super powerful.

like image 74
Ryan Bigg Avatar answered Sep 25 '22 09:09

Ryan Bigg


I recommend using the sendgrid gem ( https://github.com/stephenb/sendgrid ) as it simplifies your calling code.

Here's an example rails 3 action mailer example:

class UserAnnouncementMailer < ActionMailer::Base
  include SendGrid
  default reply_to: "[email protected]", return_path: "[email protected]", from: "Test"

  # bulk emailer
  # params - opts a hash of
  #            emails: array of emails
  #
  def notice(opts={})
    raise "email is nil" unless opts[:emails]

    sendgrid_category :use_subject_lines
    sendgrid_recipients opts[:emails]

    name = "The Man"
    to = "[email protected]"
    from_name = "#{name} <[email protected]>"
    subject = "Important"

    mail({from: from_name, to: to, subject: subject})
  end
end

And the corresponding calling code. It's recommended to have the emails array to be < 1000 emails.

emails = ["[email protected]", "[email protected]"]
UserAnnouncementMailer.notice({:emails => emails}).deliver

See the sendgrid gem github readme for more details.

like image 39
tommy chheng Avatar answered Sep 24 '22 09:09

tommy chheng