使用SendGrid和Cc批量发送电子邮件

时间:2020-06-27 13:26:50

标签: ruby-on-rails ruby email sendgrid

我是SendGrid的新手。我需要使用SendGrid发送批量电子邮件。我将github称为示例示例,如下所示,很好地了解如何使用它是很好的。参考链接here。 现在,我已经安装gem 'sendgrid-ruby'并使用 SendGrid凭据进行了配置,并在下面的示例代码中尝试了控制台上的自定义详细信息。

  mail = SendGrid::Mail.new
  mail.from = Email.new(email: 'test@example.com')
  mail.subject = 'Hello World from the Twilio SendGrid Ruby Library'
  personalization = Personalization.new
  personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
  personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User'))
  personalization.add_cc(Email.new(email: 'test3@example.com', name: 'Example User'))
  personalization.add_cc(Email.new(email: 'test4@example.com', name: 'Example User'))
  personalization.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User'))
  personalization.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User'))
  personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
  personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
  personalization.add_header(Header.new(key: 'X-Mock', value: 'False'))
  personalization.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
  personalization.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
  personalization.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
  personalization.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
  personalization.send_at = 1443636843
  mail.add_personalization(personalization)

  personalization2 = Personalization.new
  personalization2.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
  personalization2.add_to(Email.new(email: 'test2@example.com', name: 'Example User'))
  personalization2.add_cc(Email.new(email: 'test3@example.com', name: 'Example User'))
  personalization2.add_cc(Email.new(email: 'test4@example.com', name: 'Example User'))
  personalization2.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User'))
  personalization2.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User'))
  personalization2.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
  personalization2.add_header(Header.new(key: 'X-Test', value: 'True'))
  personalization2.add_header(Header.new(key: 'X-Mock', value: 'False'))
  personalization2.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
  personalization2.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
  personalization2.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
  personalization2.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
  personalization2.send_at = 1443636843
  mail.add_personalization(personalization2)

  mail.add_content(Content.new(type: 'text/plain', value: 'some text here'))
  mail.add_content(Content.new(type: 'text/html', value: '<html><body>some text here</body></html>'))
@client.mail._("send").post(request_body: mail.to_json)

在这里,我需要发送“抄送”数组作为通用数组,而不是在所有个性化设置中添加。我尝试添加单个个性化设置和to_emails列表中的所有电子邮件,并添加了抄送。我收到了带有其他所有邮件列表的邮件。为了避免这种情况,我需要进行不同的个性化设置。将cc添加为通用的任何想法吗?

0 个答案:

没有答案