1
send_mail('Subject here', 'Here is the message.', 'selva@gmail.com', ['stab@gmail.com'], fail_silently=False)
mail = send_mail('Subject here', 'Here is the message.', 'selvakumaremmy@gmail.com', ['vsolvstab@gmail.com'], fail_silently=False)
mail.attach('AP_MODULE_bugs.ods','AP_MODULE_bugs.ods','application/vnd.oasis.opendocument.spreadsheet')
mail.send()

我正在使用 Django send_mail 类发送邮件。在这里我想发送带有附件的邮件,我的附件文件(.ods)在本地存储中。

4

2 回答 2

1

尝试使用attach_file()

前任:

mail = EmailMessage('Subject here', 'Here is the message.', 'selva@gmail.com',  ['stab@gmail.com'])
mail.attach_file('PATH TO AP_MODULE_bugs.ods', mimetype='application/vnd.oasis.opendocument.spreadsheet')
mail.send()
于 2018-06-20T11:47:20.600 回答
1

您必须使用EmailMessage

from django.core.mail import EmailMessage

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to1@example.com', 'to2@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},

)

mail.attach('AP_MODULE_bugs.ods',mimetype='application/vnd.oasis.opendocument.spreadsheet')

邮件发送()

attach() 创建一个新的文件附件并将其添加到消息中。调用attach()有两种方式:

  • 您可以将单个参数传递给它,即 email.MIMEBase.MIMEBase 实例。这将直接插入到生成的消息中。
  • 或者,您可以传递 attach() 三个参数:文件名、内容和 mimetype。filename 是电子邮件中显示的文件附件的名称,content 是附件中包含的数据,mimetype 是附件的可选 MIME 类型。如果省略 mimetype,则将从附件的文件名中猜测 MIME 内容类型。

    例如:message.attach('design.png', img_data, 'image/png')

于 2018-06-20T11:51:20.017 回答