Skip to main content
 首页 » 编程设计

python之将 MailChimp 与 SendGrid 结合使用

2025年05月04日11Renyi-Fan

我已经使用 MailChimp 的模板生成器在 MailChimp 中构建了一个电子邮件模板。

我想在某些事件中将此电子邮件发送给个人。例如,如果有人忘记了密码,我想向他们发送我在 MailChimp 中创建的“忘记密码”模板。

我看过 MailChimp 的 API,但它似乎对非批量电子邮件发送不太友好。我如何发送这样的交易电子邮件,使用从 MailChimp 创建的 HTML 模板,但使用 SendGrid for SMTP?

代码看起来像这样:

if action == 'forgot_password': 
    email = mailchimp.get_template(forgot_password) 
    sendgrid.send_mail(user, subject, email, from) 

请您参考如下方法:

不确定您使用什么 Mailchimp 库来获取模板。做了一些谷歌搜索但无法弄清楚,但假设 get_template 返回电子邮件的 html 内容,您可以这样做:

import sendgrid 
 
s = sendgrid.Sendgrid('username', 'password', secure=True) 
 
if action == 'forgot_password' 
  html = mailchimp.get_template(forgot_password) 
  message = sendgrid.Message("from@mydomain.com", "subject", text_version_of_template, html) 
  message.add_to("someone@example.com", "John Doe") 
  s.smtp.send(message) 

参见 this question有关获取 html 的文本版本的详细信息。