代码之家  ›  专栏  ›  技术社区  ›  ravelinx

使用Sendgrid发送多封电子邮件,但不使用X-SMTPAPI标头

  •  0
  • ravelinx  · 技术社区  · 7 年前

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

    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
    

      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而不是标题。我怎样才能解决这个问题?

    1 回复  |  直到 7 年前
        1
  •  0
  •   ravelinx    7 年前

    我是这样做的,我认为这不是最好的解决方案,因为我正在再次加载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' 在课堂上