Saturday, 1 November 2014

Sending Email using Python

I often see people asking the question how do you send Email using Python?
Well a few years ago I had the idea to write a program, which would automatically send an Email should a certain event happen. I have been meaning to include the Email code in a blog post, but have never managed to get around to it.

Until now...

So here is the code I used. I know it works with Gmail, but have not tested it with other providers, although I am sure a little modification would make it work.

Remember this program requires you to store your Email password in plain text, something you need to be aware of!

Just ensure you add your username and password where necessary, and change the other fields as required.


#!/usr/bin/python
import smtplib

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

recipient = 'recipient@email_address.com'
subject = 'Email Subject'
emailText = 'This is the content of the e-mail.'

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)
session.quit()

3 comments:

  1. Nice one, I guess we just need to stop for a brief moment and read through. I dont read that much but I really had some good time grabbing some knowledge. Still I am out to speak and propose you exotic stuffs like
    exotic carts official
    buy exotic carts
    buy exotic carts online.
    exotic carts review
    exotic carts website
    gorilla glue exotic carts.
    exotic carts fake
    kingpen official
    kingpen review.
    mario carts official
    mario carts online

    ReplyDelete
  2. Thanks For Important Information.
    Galaxy Aircon
    Ac repairing In vadodara
    Best Ac Repair In Vadodara
    24*7 Emergency Service In Vadodara
    Galaxy Aircon
    Ac Repairing In Vadodara
    Thanks For Important Information. https://galaxyaircon.com/
    Brands We Service for AC Repair in Vadodara, Gujarat, India. Carrier-Service-Center-Vadodara. Electrolux-Service-Center-Vadodara. Haier-Service-Center-Vadodara. Hitachi-Service-Center-Vadodara. LG-Service-Center-Vadodara. Mitsubishi-Service-Center-Vadodara. Kelvinator-Service-Center-Vadodara. OGeneral-Service-Center
    http://bestacrepair.in/
    Noor Aircon
    Ac Repairing In Vadodara
    Best Ac Repair In Vadodara
    Ac Repairing Near Me
    https://galaxyaircon.com/

    Noor Aircon
    Ac Repairing In Vadodara
    Thanks For Important Information.
    AC Repairing & Installation
    Panel Air Conditioner
    Precision Air Conditioner
    Cassette Air Conditioner
    Ductable Air Conditioner
    Window Air Conditioner
    Split Air Conditioner
    Refrigerator Repairing Services
    Compact Refrigerator
    French Door Refrigerator
    Side By Side Refrigerator
    Double Door Refrigerator
    Single Door Refrigerator
    Bottom Freezer Fridge
    Top-Freezer Refrigerator
    HVAC System
    Air Handling Unit
    Water Cooler
    Deep Freezer
    VRF System Repair
    Ducting Repair
    Air Cooler Repair
    Chillier Repair
    Electronics Repairing Services
    http://bestairtechengineers.com/
    Airtech Engineers
    Ac Repairing In Vadodara
    Best Ac Repair In Vadodara
    Ac Repairing Near Me
    http://bestairtechengineers.com/
    Airtech Engineers
    Ac Repairing In Vadodara
    Thanks For Important Information.

    ReplyDelete