Question

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!

Was it helpful?

Solution

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 info@ourcompany.com 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.

OTHER TIPS

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: "test@test.com", return_path: "test@test.com", 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 = "test@test.com"
    from_name = "#{name} <theman@test.com>"
    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 = ["alice@test.com", "bob@test.com"]
UserAnnouncementMailer.notice({:emails => emails}).deliver

See the sendgrid gem github readme for more details.

Delayed Job and SendGrid sound like the best option from what you say, but have you considered using one of the campaign mailers like Mailchimp instead? If you're sending out a lot of mails that are basically the same, they'll let you setup and campaign template and then fire a CSV of all the variables at it. They then effectively mail merge and fire them all out.

If however, you're only talking a few hundred you're on the right lines. SendGrid can easily handle the load, and you want to use Delayed Job so that you're not impacted by the performance of the SendGrid API should it not be favorable. Alternatively, look at Resque instead for sending mail as it may be more efficient.

SendGrid offers some advice here. They have a category for Deliverability and for Best practices on their blog.

I would imagine SendGrid can handle that kind of load. Most relay systems can. Also I would imagine if you sent the 500 in a CC API call, that their system would parse it and send them individually. I use Elastic Email (http://elasticemail.com) - and I know that this is how they handle it and it works great.

This is how I've done it in Rails 4

class NewsMailer < ApplicationMailer
  include SendGrid

  sendgrid_category :use_subject_lines

  default from: 'My App! <support@myapp.com>'

  def mass_mailer(news)
    # Pass it in template
    @news = news

    # Custom method to get me an array of emails ['user1@email.com', 'user2@email.com',...] 
    array_of_emails = @news.recipients.pluck(:email) 

    # You can still use
    # headers["X-SMTPAPI"] = { :to => array_of_emails }.to_json
    sendgrid_recipients array_of_emails

    mail to: 'this.will.be.ignored@ignore.me', subject: 'Weekly news'
  end

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top