python - 将标题添加到 Django EmailMultiAlternatives

标签 python django email header smtp

Django EmailMultiAlternatives documentation没有关于如何在 EmailMultiAlternatives 中添加标题(如“格式”或“回复”)的内容。我花了一段时间才弄明白,我发送这篇文章是为了帮助其他人节省时间。

在django的源代码中可以看到,EmailMultiAlternatives继承自EmailMessage,因此它们在init构造函数中采用相同的参数。这样,我们可以添加标题,例如:

msg = EmailMultiAlternatives(
    subject, message, from_email, to_list,
    headers={'Reply-To': "email@example.com", 'format': 'flowed'}
)

最佳答案

早在 2015 年,OP 就提示说,文档中没有说明如何在 Django 邮件 (django.core.mail) 模块中添加标题,例如“格式”和“回复”。然而今天,在使用 same documentation link 时.我们可以轻松找到 2018 年的描述和示例:

class EmailMessage[source]

The EmailMessage class is initialized with the following parameters (in the given order, if positional arguments are used). All parameters are optional and can be set at any time prior to calling the send() method.

  • subject: The subject line of the email.
  • body: The body text. This should be a plain text message.
  • from_email: The sender’s address. Both fred@example.com and Fred <fred@example.com> forms are legal. If omitted, the DEFAULT_FROM_EMAIL setting is used.
  • to: A list or tuple of recipient addresses.
  • bcc: A list or tuple of addresses used in the “Bcc” header when sending the email.
  • connection: An email backend instance. Use this parameter if you want to use the same connection for multiple messages. If omitted, a new connection is created when send() is called.
  • attachments: A list of attachments to put on the message. These can be either email.MIMEBase.MIMEBase instances, or (filename, content, mimetype) triples.
  • headers: A dictionary of extra headers to put on the message. The keys are the header name, values are the header values. It’s up to the caller to ensure header names and values are in the correct format for an email message. The corresponding attribute is extra_headers.
  • cc: A list or tuple of recipient addresses used in the “Cc” header when sending the email.

For example:

email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
            ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
            headers = {'Reply-To': 'another@example.com', 'format': 'flowed'})

正如我们从例子中看到的,EmailMessageheaders参数(字典)也是,EmailMultiAlternatives根据 source code 中的文档字符串是:

A version of EmailMessage that makes it easy to send multipart/alternative
messages. For example, including text and HTML versions of the text is
made easier.

因此,如果您不需要特定的东西,EmailMessage很好,因为目前 EmailMultiAlternatives用于轻松包含文本和文本的 HTML 版本。

关于python - 将标题添加到 Django EmailMultiAlternatives,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29353815/

相关文章:

java - 处理电子邮件服务器时如何处理代理?

java - 如何 javax.mail.AuthenticationFailedException : failed to connect in sending mail by java?

email - 选择电子邮件发送服务

python - 使用 PySerial 从串口读取二进制数据

django - 获取切片后无法更新查询

python - 创建抽象枚举类

python - Django @csrf_exempt 装饰器不工作

python - Django "homepage"应用程序 urls.py 问题

python - 在 OSX 上使用格式语言的德语数字分隔符?

使用 sympy lambdify 和 scipy 进行 Python 优化