Django.fun

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. AttributeError: 'int' object has no attribute 'attach'. Can you give me an Idea on how to add attach file in sending email via send_email in django?Thank you

Tree

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

My send_mail command are inside the management folder

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()

Answers: 1

Answered by Marcus Aldrey, Oct. 13, 2021, 8:47 a.m.

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. To send an email with an attachment you should first build your message with Django Email Message and then use send_mail.

Attaching pdf's to emails in django