发送附有 .ods 文件的电子邮件

Sending email with attached .ods File

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 class 发送邮件。这里我想发送带附件的邮件,我的附件文件(.ods)在本地存储。

尝试使用 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()

你必须使用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')

mail.send()

attach() creates a new file attachment and adds it to the message. There are two ways to call attach():

  • 你可以给它传递一个 email.MIMEBase.MIMEBase 的参数 实例。这将直接插入生成的消息中。
  • 或者,您可以向 attach() 传递三个参数:文件名、 内容和模仿类型。文件名是文件附件的名称 它会出现在电子邮件中,内容是将要 包含在附件和 mimetype 中的是可选的 MIME 类型的附件。如果省略 mimetype,则 MIME 内容类型 将从附件的文件名中猜测。

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