将 ical 文件附加到 django 电子邮件
有很多关于如何将文件附加到电子邮件的示例,但我找不到如何附加 MIMEBase 实例的示例。
来自文档:附件“这些可以是 email.MIMEBase.MIMEBase 实例,或(文件名、内容、mimetype)三元组。”
所以我在函数中生成一个 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)
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.
From the docs: attachments "These can be either email.MIMEBase.MIMEBase instances, or (filename, content, mimetype) triples."
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
But this is not working:
myicalfile = ical()
message.attach(myicalfile)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 def ical() 的末尾尝试此代码:
当然,导入代码应移至文件顶部以符合编码标准。
Try this code at the end of def ical():
Of course, the import code should be moved at the top of file to conform with coding standards.