gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Sending an attachment in mail

Thu Sep 10, 2015 7:52 am

I've been using a script to send simple text to my email address, but now I need to send an attachment.
How do I do it?
I couldn't find a working code online, please help.

Code: Select all

SMTP_SERVER = 'smtp.gmail.com'
    SMTP_PORT = 587
    GMAIL_USERNAME = ''
    GMAIL_PASSWORD = '' #CAUTION: This is stored in plain text!

    recipient = ''
    subject = 'The Temperature In Your Room'
    emailText = z

    emailText = "" + emailText + ""

    headers = ["From: " + GMAIL_USERNAME,
           "Subject: " + subject,
           "To: " + recipient,
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
    headers = "\r\n".join(headers)

    session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

    session.ehlo()
    session.starttls()
    session.ehlo

    session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

    session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + emailText)


email(0)

Return to “General discussion”