有很多关于如何将文件附加到电子邮件的示例,但我找不到一个关于如何附加MIMEBase实例的示例。
从文档中可以看到:附件“这些可以是email.MIMEBase.MIMEBase实例,或者是(文件名,内容,MIME类型)三元组。”
因此,我在函数中成功地生成了一个iCal文件:
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)