Context:
I need to send bulk-email using send grid in a rails app. I will be sending emails to maybe around 300 subscribers. I have read that it can be accomplished using
headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json
I have tried following that.
The following is my ActionMailer:
class NewJobMailer < ActionMailer::Base
default from: "from@example.com"
def new_job_post(subscribers)
@greeting = "Hi"
headers['X-SMTPAPI'] = { :to => subscribers.to_a }.to_json
mail(
:to => "this.will@be.ignored.com",
:subject => "New Job Posted!"
)
end
end
I call this mailer method from a controller
..
@subscribers = Subscriber.where(activated: true)
NewJobMailer.new_job_post(@subscribers).deliver
..
The config for send-grid is specified in the config/production.rb file and is correct, since I am able to send out account activation emails.
Problem:
The app works fine without crashing anywhere, but the emails are not being sent out. I am guessing the headers config is not being passed along ? How can I correct this ?
UPDATE:
I checked for email activity in the send grid dashboard. Here is a snapshot of one of the dropped emails:
You are grabbing an array of ActiveRecord objects with
@subscribers = Subscriber.where(activated: true)
and passing that into the smtpapi header. You need to pull out the email addresses of those ActiveRecord objects.
Depending on what you called the email
field, this can be done with
headers['X-SMTPAPI'] = { :to => subscribers.map(&:email) }.to_json