将文件附加到django电子邮件 [英] Attaching an ical file to a django email
本文介绍了将文件附加到django电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有很多关于如何将文件附加到电子邮件的示例,但是我找不到如何附加MIMEBase实例的示例。
There are plenty of examples of how to attach a file to an email, but I can't find an example of how to attach a MIMEBase instance.
从文档:附件这些可以是email.MIMEBase.MIMEBase实例,或(filename,content,mimetype)三元组。
From the docs: attachments "These can be either email.MIMEBase.MIMEBase instances, or (filename, content, mimetype) triples."
所以我正在生成一个iCal文件功能很好:
So I'm generating an iCal file in a function just fine:
def ical()
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH' # IE/Outlook needs this
vevent = cal.add('vevent')
vevent.add('dtstart').value = self.course.startdate
vevent.add('dtend').value = self.course.startdate
vevent.add('summary').value='get details template here or just post url'
vevent.add('uid').value=str(self.id)
vevent.add('dtstamp').value = self.created
icalstream = cal.serialize()
response = HttpResponse(icalstream, mimetype='text/calendar')
response['Filename'] = 'shifts.ics' # IE needs this
response['Content-Disposition'] = 'attachment; filename=shifts.ics'
return response
但这不工作:
myicalfile = ical()
message.attach(myicalfile)
推荐答案
在def ical()的末尾尝试此代码:
Try this code at the end of def ical():
from email.mime.text import MIMEText
part = MIMEText(icalstream,'calendar')
part.add_header('Filename','shifts.ics')
part.add_header('Content-Disposition','attachment; filename=shifts.ics')
return part
当然,导入代码应该移动到文件顶部,以符合编码标准。
Of course, the import code should be moved at the top of file to conform with coding standards.
这篇关于将文件附加到django电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文