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

后端 未结 6 1613
生来不讨喜
生来不讨喜 2020-12-23 14:50

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

6条回答
  •  星月不相逢
    2020-12-23 15:48

    This is how I've done it in Rails 4

    class NewsMailer < ApplicationMailer
      include SendGrid
    
      sendgrid_category :use_subject_lines
    
      default from: 'My App! '
    
      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
    

提交回复
热议问题