[英]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類發送郵件。 在這里我想發送帶有附件的郵件,我的附件文件(.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()創建一個新的文件附件並將其添加到郵件中。 有兩種方法可以調用attach():
另外,您可以傳遞attach()三個參數:文件名,內容和mimetype。 filename是將在電子郵件中顯示的文件附件的名稱,content是將包含在附件中的數據,mimetype是附件的可選MIME類型。 如果省略mimetype,將從附件的文件名中猜測MIME內容類型。
例如:message.attach('design.png',img_data,'image / png')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.