我正在尝试使用 sendgrid 从我的 Python 应用程序发送电子邮件。
使用邮件助手和以下语法发送正常:
sgmessage = mail.Mail(from_email, message.subject, to_email, content)
sg.client.mail.send.post(request_body=sgmessage.get())
但是我找不到发送具有文本和 html 版本的多部分/替代内容类型的电子邮件的方法?
我试过这样的事情:
sgmessage = mail.Mail(from_email, message.subject, to_email)
sgmessage.add_content(content)
sgmessage.add_content(htmlcontent)
但是当我尝试发送时,这给了我错误的请求。
有人可以向我指出实现此目标的文档或其他技巧吗?
还有附件——我找不到那个方便的邮件助手的文档,它可以帮助我在我的代码中添加附件功能。
最佳答案
邮件助手源 https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py
mail_html = Content(type_='text/html', value='<h1>Test Mail</h1><p>This is a test email message.</p>')
mail_txt = Content(type_='text/plain', value='This is a test email message.')
mail = Mail(mail_from, mail_subject, mail_to, mail_html)
mail.add_content(mail_txt)
response = sg.client.mail.send.post(request_body=mail.get())
示例链接仍然是 404。
关于python - 使用 sendgrid 和 python 发送 html/文本电子邮件的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43240050/