smtplib send pdf

Solutions on MaxInterview for smtplib send pdf by the best coders in the world

showing results for - "smtplib send pdf"
Angelo
07 Jun 2016
1def send_email_pdf_figs(path_to_pdf, subject, message, destination, password_path=None):
2    ## credits: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script
3    from socket import gethostname
4    #import email
5    from email.mime.application import MIMEApplication
6    from email.mime.multipart import MIMEMultipart
7    from email.mime.text import MIMEText
8    import smtplib
9    import json
10
11    server = smtplib.SMTP('smtp.gmail.com', 587)
12    server.starttls()
13    with open(password_path) as f:
14        config = json.load(f)
15        server.login('me@gmail.com', config['password'])
16        # Craft message (obj)
17        msg = MIMEMultipart()
18
19        message = f'{message}\nSend from Hostname: {gethostname()}'
20        msg['Subject'] = subject
21        msg['From'] = 'me@gmail.com'
22        msg['To'] = destination
23        # Insert the text to the msg going by e-mail
24        msg.attach(MIMEText(message, "plain"))
25        # Attach the pdf to the msg going by e-mail
26        with open(path_to_pdf, "rb") as f:
27            #attach = email.mime.application.MIMEApplication(f.read(),_subtype="pdf")
28            attach = MIMEApplication(f.read(),_subtype="pdf")
29        attach.add_header('Content-Disposition','attachment',filename=str(path_to_pdf))
30        msg.attach(attach)
31        # send msg
32        server.send_message(msg)
33