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

时间:2018-06-20 11:38:40

标签: python django django-rest-framework django-mailer

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)位于本地存储中。

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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')

mail.send()

  

attach()创建一个新的文件附件并将其添加到邮件中。有两种方法可以调用attach():

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

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

相关问题