用Python发送HTML电子邮件

351

如何使用Python发送带有HTML内容的电子邮件?我能够发送简单的文本。


2
只是一个大而明显的警告。如果你正在使用 Python < 3.0 发送非 ASCII 邮件,请考虑使用 Django 中的电子邮件。它可以正确地包装 UTF-8 字符串,并且使用起来也更简单。你已经被警告了 :-) - Anders Rune Jensen
3
如果您想发送带有Unicode的HTML,请参见此处:http://stackoverflow.com/questions/36397827/send-html-mail-with-unicode - guettli
12个回答

2
这是使用boto3处理AWS的答案。
    subject = "Hello"
    html = "<b>Hello Consumer</b>"

    client = boto3.client('ses', region_name='us-east-1', aws_access_key_id="your_key",
                      aws_secret_access_key="your_secret")

client.send_email(
    Source='ACME <do-not-reply@acme.com>',
    Destination={'ToAddresses': [email]},
    Message={
        'Subject': {'Data': subject},
        'Body': {
            'Html': {'Data': html}
        }
    }

2
如果您想要更简单的东西:
from redmail import EmailSender
email = EmailSender(host="smtp.myhost.com", port=1)

email.send(
    subject="Example email",
    sender="me@example.com",
    receivers=["you@example.com"],
    html="<h1>Hi, this is HTML body</h1>"
)

PyPI上安装Red Mail:

pip install redmail

Red Mail很可能拥有您发送邮件所需的所有功能,包括以下特性: 文档: https://red-mail.readthedocs.io/en/latest/index.html 源代码: https://github.com/Miksus/red-mail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接