Hello friends, welcome to new tutorial Python Sending Email Tutorial, in this tutorial we will discuss about how to send email from python.
In most of applications, we need to communicate with our users by sending messages electronically.Email is used to send password resets, confirmation of orders, and verification of user accounts etc.Email works with SMTP(Simple Mail Transfer Protocol) protocol which handles sending e-mails and routing e-mails between mail servers.
Contents
Get Started Python Sending Email Tutorial
Python Sending Email Module
Python provides smtplib for sending emails. The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
Importing smtplib
1 2 3 |
import smtplib |
Now let’s start coding part.
Create a new project , named it as sending_email or as you like. I am using PyCharm IDE , you can use anything.Now create a new python file by following the path- sending_email->Right click->New->Python File and named it as you wish.
Requirements for sending email
- SMTP domain name of service providers with port number.
- Sender’s username and password.
- Receiver’s email address.
E-mail service providers and their SMTP domain names
- Gmail – smtp.gmail.com , 587
- Yahoo – smtp.mail.yahoo.com , 587
Here I am using Gmail.
So let’s start to implement our program
Sending a Simple Email
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import smtplib // importing smttplib email_sender = 'laamzara@gmail.com' //create a variable that //stores sender's email address email_receiver = 'laamzara@gmail.com' //create a variable that //stores receiver's email address //create a SMTP object that is used for connection establishment with server. //It requires two parameters service provider's domain name and its port number. connection = smtplib.SMTP('smtp.gmail.com', 587) connection.starttls() //for encription //login() method is used to login into the server //It requires two parameters sender's email and password //Here i am using password for example ,you must provide a valid password connection.login(email_sender, 'your password') //for sending email we needs 3 things sender's and receiver's emails and message connection.sendmail(email_sender, email_receiver, 'Hi! this email is from python...') //for quiting the connection connection.quit() |
Now run the above code. While running we get a error that is a security error.
To solve this error we just need to go to My Account->Sign-in & Security->Apps with Account Access. After scrolling down we see a dialog box as below
Now this is off, you have to turn it on. Again run the code now there is no error and email sended successfully to the receiver.
On clicking this email we can see the format of email, it is quite simple.
email with Subject
Now we attach subject in our email. For this we modify our code little bit as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import smtplib //the MIMEText class is used to create MIME objects of major type text from email.mime.text import MIMEText //A subclass of MIMEBase, this is an intermediate base class for MIME messages that are multipart from email.mime.multipart import MIMEMultipart email_sender = 'laamzara@gmail.com' email_receiver = 'laamzara@gmail.com' subject = 'python!' msg = MIMEMultipart() //used for define multipart message msg['From'] = email_sender msg['To'] = email_receiver msg['Subject']= subject body = 'hi everyone ! this email is from python' msg.attach(MIMEText(body, 'plain')) // attach body to the message, here email is plain so email type plain is used text = msg.as_string() // used for converting object into plain text string connection = smtplib.SMTP('smtp.gmail.com', 587) connection.starttls() connection.login(email_sender, 'your password') connection.sendmail(email_sender, email_receiver, text ) connection.quit() |
Now check the email, and you will see the email received in your email.
email with attachment
Now we attach an attachment in our email. For this we modify our code as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import smtplib from email import encoders // this is for encode the payloads for transport through compliant mail servers from email.mime.base import MIMEBase //This is the base class for all the MIME-specific subclasses of Message from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart email_sender = 'laamzara@gmail.com' email_receiver = 'laamzara@gmail.com' subject = 'python!' msg = MIMEMultipart() msg['From'] = email_sender msg['To'] = email_receiver msg['Subject']= subject body = 'hi everyone ! this email is from python' msg.attach(MIMEText(body, 'plain')) filename = 'Document.txt' // this is a file that is added to our mail attachment = open(filename, 'rb') //for opening file, file is open in read mode //A MIME attachment with the content type "application/octet-stream" is a binary file. //Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. // If the attachment has a filename extension associated with it, you may be able to tell what kind of file it is part = MIMEBase('application', 'octet_stream') part.set_payload((attachment).read()) //Base64 encoding schemes are commonly used when there is a need to encode binary data //that needs be stored and transferred over media that are designed to deal with textual data. encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= "+filename) //add the file to the header msg.attach(part) //attach the attachment to the message text = msg.as_string() connection = smtplib.SMTP('smtp.gmail.com', 587) connection.starttls() connection.login(email_sender, '12345sad') connection.sendmail(email_sender, email_receiver, text ) connection.quit() |
- On running above code we get following output.
email with image
It is same as sending a file, and you can attach any file to your email and you can attach an image file as well.
1 2 3 |
filename = 'logo.png' |
The output will be as follows :
So that’s all for this Python Sending Email Tutorial friends. I hope you found it helpful if you did, then please SHARE this post with your friends who are learning python.
And yes, if you have any query regarding this Python Sending Email Tutorial then your questions are welcome. Thank You 🙂
I facing problem in emali sending tutorials.I m also message you on fb & comments on ‘Sending email’ post on fb..So please solve my problem
What’s the problem
Hola buen ¿como podría mandar mensaje SMS con Python sin usar un servidor de terceros?
Please translate it into english. I am not able to understand this fully.
Hi, how could I send an SMS message with Python without using a third-party server?