简体   繁体   English

带有 PDF 附件的 Django Send_mail

[英]Django Send_mail with PDF attachment

I already done sending email with HTML template, I want to add a attachment(PDF) in email but I got an error.我已经用 HTML 模板发送了电子邮件,我想在电子邮件中添加一个附件(PDF),但出现错误。 AttributeError: 'int' object has no attribute 'attach'. AttributeError: 'int' 对象没有属性 'attach'。 Can you give me an Idea on how to add attach file in sending email via send_email in django?Thank you你能给我一个关于如何在通过 django 中的 send_email 发送电子邮件时添加附件文件的想法吗?谢谢

Tree

├── Folder
│   ├── management
│   ├── templates
│   │   └── p.pdf

My send_mail command are inside the management folder我的 send_mail 命令在管理文件夹内

send email code发送电子邮件代码

subject = 'Management Automated Email- ' + (item.emp)
html_message = render_to_string('email.html',data)
plain_message = item.emp
recipient_list = [item.email]
from_email = <email@gmail.com>'
toaddrs = recipient_list 
mail = send_mail(subject, plain_message, from_email, toaddrs, html_message=html_message, fail_silently=False)
mail.attach('papers.pdf', 'pdf/plain')
mail.send()

The function send_mail() returns either 0 or 1, indicating if the message was sent successfully or not, so your mail variable is an int, that's why you got that error.函数 send_mail() 返回 0 或 1,指示消息是否成功发送,因此您的邮件变量是一个 int,这就是您收到该错误的原因。 To send an email with an attachment you should first build your message with Django Email Message and then use send_mail.要发送带有附件的电子邮件,您应该首先使用 Django 电子邮件消息构建您的消息,然后使用 send_mail。

Attaching pdf's to emails in django 将pdf附加到django中的电子邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM