2012-09-17 36 views
2

我在MailChimp中使用模板構建器構建了一個電子郵件模板。與SendGrid一起使用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) 

回答

1

不知道你用得到模板什麼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("[email protected]", "subject", text_version_of_template, html) 
    message.add_to("[email protected]", "John Doe") 
    s.smtp.send(message) 

有關將您的HTML文本版本的詳細信息,請參閱this question