【问题标题】:Send multiple emails with Sendgrid not working with X-SMTPAPI headers使用 Sendgrid 发送多封电子邮件不使用 X-SMTPAPI 标头
【发布时间】:2017-07-20 16:51:39
【问题描述】:

我一直在发送电子邮件如下:

def send
    ActionMailer::Base.mail(content_type: "text/html", from: "\"name\" <email@gmail.com>", to: "email2@gmail.com", subject: "subject", body: "<h1>Hi</h1>" , priority: 2).deliver
end

并且一直运行良好,但现在我想发送多封电子邮件,因为我需要处理 1000 多个用户。所以我一直在阅读我可以使用 X-SMTPAPI 标头来完成这个,所以我尝试了这个

  def send_all
    recipients = ["users1@gmail.com", "users2@gmail.com"]
    ActionMailer::Base.headers["X-SMTPAPI"] = { :to => recipients }.to_json
    ActionMailer::Base.mail(content_type: "text/html", from: "\"name\" <email@gmail.com>", to: "email2@gmail.com", subject: "subject", body: "<h1>Hi</h1>" , priority: 2).deliver
  end

但 Sendgrid 只是发送电子邮件到 email2@gmail.com 而不是标题。我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails sendgrid sendgrid-api-v3 sendgrid-ruby


    【解决方案1】:

    我按照以下方式进行操作,我认为这不是最好的解决方案,因为我正在再次加载 Sendgrid 配置,但它确实有效

    def send
        Mail.defaults do
          delivery_method :smtp, { :address   => 'smtp.sendgrid.net',
                                   :port      => 587,
                                   :domain    => 'sendgrid.com',
                                   :user_name => 'yourSendGridUsername',
                                   :password  => 'yourSendGridPassword',
                                   :authentication => 'plain',
                                   :enable_starttls_auto => true }
        end
    
            recipients = ["users1@gmail.com", "users2@gmail.com"]
    
            mail = Mail.deliver do
              header['X-SMTPAPI'] =  { :to => recipients }.to_json
              to "ignore@gmail.com"
              from 'email@gmail.com'
              subject 'Ruby Example using X-SMTPAPI header'
              text_part do
                body 'You would put your content here, but I am going to say: Hello world!'
              end
              html_part do
                content_type 'text/html; charset=UTF-8'
                body '<b>Hello world!</b><br>Glad to have you here!'
              end
            end
    
    end
    

    我还需要在课堂上require 'mail'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多