Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

smtplib send pdf

def send_email_pdf_figs(path_to_pdf, subject, message, destination, password_path=None):
    ## credits: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script
    from socket import gethostname
    #import email
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    import smtplib
    import json

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    with open(password_path) as f:
        config = json.load(f)
        server.login('me@gmail.com', config['password'])
        # Craft message (obj)
        msg = MIMEMultipart()

        message = f'{message}
Send from Hostname: {gethostname()}'
        msg['Subject'] = subject
        msg['From'] = 'me@gmail.com'
        msg['To'] = destination
        # Insert the text to the msg going by e-mail
        msg.attach(MIMEText(message, "plain"))
        # Attach the pdf to the msg going by e-mail
        with open(path_to_pdf, "rb") as f:
            #attach = email.mime.application.MIMEApplication(f.read(),_subtype="pdf")
            attach = MIMEApplication(f.read(),_subtype="pdf")
        attach.add_header('Content-Disposition','attachment',filename=str(path_to_pdf))
        msg.attach(attach)
        # send msg
        server.send_message(msg)
Comment

PREVIOUS NEXT
Code Example
Python :: python checking if something is equal to NaN 
Python :: python find intersection of two lists 
Python :: convert list into integer in python 
Python :: seaborn pairplot 
Python :: RuntimeError: Broken toolchain: cannot link a simple C program 
Python :: pandas column name equal to another column value 
Python :: how do i convert a list to a string in python 
Python :: hardest python questions 
Python :: full form of rom 
Python :: how to sort a dictionary py 
Python :: python docstring example 
Python :: django drop all tables 
Python :: drop all characters after a character in python 
Python :: pip install qrcode python 
Python :: How to scale a pandas dataframe 
Python :: python nested list comprehension 
Python :: pandas dataframe 
Python :: make the program title a name python 
Python :: pandas dataframe unique multiple columns 
Python :: run linux command using python 
Python :: convert datetime to date python 
Python :: button onclick message box in python tkinter 
Python :: pil normalize image 
Python :: how to use turtle in python in python 3.9 
Python :: python convert string to bytes 
Python :: python convert string to byte array 
Python :: print specific list item python 
Python :: thread with args python 
Python :: outliers removal 
Python :: end python program 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =