Ruby on rails 使用Sendgrid发送多封不使用X-SMTPAPI头的电子邮件
我一直在发送以下电子邮件:Ruby on rails 使用Sendgrid发送多封不使用X-SMTPAPI头的电子邮件,ruby-on-rails,sendgrid,sendgrid-api-v3,sendgrid-ruby,Ruby On Rails,Sendgrid,Sendgrid Api V3,Sendgrid Ruby,我一直在发送以下电子邮件: 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发送 ActionMailer::Base.m
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发送
ActionMailer::Base.mail(内容类型:“text/html”,从:“\'name\”到:email2@gmail.com,主题:“主题”,主体:“嗨”,优先级:2)。交付
结束
并且一直工作良好,但现在我想发送多封电子邮件,因为我需要处理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
def发送所有
收件人=[”users1@gmail.com", "users2@gmail.com"]
ActionMailer::Base.headers[“X-SMTPAPI”]={:to=>recipients}.to_json
ActionMailer::Base.mail(内容类型:“text/html”,从:“\'name\”到:email2@gmail.com,主题:“主题”,主体:“嗨”,优先级:2)。交付
结束
但Sendgrid只发送电子邮件给email2@gmail.com而不是标题。如何修复此问题?我按如下方式操作,我认为这不是最好的解决方案,因为我正在再次加载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
def发送
Mail.do
传递方法:smtp,{:address=>'smtp.sendgrid.net',
:端口=>587,
:domain=>'sendgrid.com',
:user_name=>“yourSendGridUsername”,
:password=>“yourSendGridPassword”,
:身份验证=>“普通”,
:enable_starttls_auto=>true}
结束
收件人=[”users1@gmail.com", "users2@gmail.com"]
mail=mail.deliver.do
标题['X-SMTPAPI']={:to=>recipients}.to_json
"ignore@gmail.com"
从'email@gmail.com'
主题“使用X-SMTPAPI头的Ruby示例”
文字部分做什么
body‘你会把你的内容放在这里,但我会说:你好,世界!’
结束
你是做什么的
内容类型为“文本/html”;字符集=UTF-8'
身体'你好,世界<很高兴有你在这里
结束
结束
结束
我还需要在课堂上要求“邮件”