Folks i hope you all are enjoying our tutorial. In this section let’s learn some more interesting stuffs which you can include in any of the python application. So we’ll cover how can we send email using python to any recipient without any effort of login to any of you Email UI login sites. To understand the thing better in case if you are new to python, i would request you to go through the Beginner’s section.
Send Email Using Python
To begin with let me say that there are number of Modules available to perform the action of sending Email’s but in my knowledge i say that the email and smtplib modules in Python are very powerful. So in this section i will be concentrating on smtplib module to achieve our target.
To understand the working of smtplib module let’s learn what is SMTP.
SMTP stands for Simple Mail Transfer Protocol, this protocol basically handles sending Emails and routing Emails between mail servers.
Let’s define now the smtplib module – “smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP listener deamon.”
So in short sending Email is done with smtplib module using an SMTP server.
In order to send Email using python we need to create SMTP object, we have a simple syntax for it with its optional arguments.
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
- host – This is the host running your SMTP server. You can specifiy IP address of the host or a domain name like pythonlovers.net
- port – If you are providing host argument, then you need to specify a port, where SMTP server is l listening. Usually this port would be 25.
- local_hostname – If your SMTP server is running on your local machine, then you can specify just localhost as of this option.
SMTP object defines a simple instance method sendmail, it takes the following parameters which are self-explanatory.
Here is small working example which demonstrates the working :
import smtplib gmail_user = 'email@example.com' gmail_pwd = 'user1PWD' FROM = 'firstname.lastname@example.org' TO = ['email@example.com'] SUBJECT = "Hi!" TEXT = "Hello!" # Prepare actual message message = """\From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) try: server = smtplib.SMTP("SMTP.GMAIL.COM", 587) server.ehlo() server.starttls() server.ehlo() server.login(gmail_user, gmail_pwd) server.sendmail(FROM, TO, message) server.close() print 'successfully sent the mail' except smtplib.SMTPException: print "failed to send mail”
Explanation : In the first line we have imported the smtplib module, then in the lines before try block we have set the gmai_user’s mailID and the password followed by FROM variable which is the sender of the email and then we have TO variable who is the recipient(s) ( remember that recipient(s) is a list which means we can send the Email to set of different users ).
Then we have SUBJECT and the TEXT of the Email. So, let’s move on with how we can send email using python.
Then we setup our message in message variable which includes FROM , TO , SUBJECT and TEXT header separated by a blank line, because the Email requires this header format.
In try block we send the Email required. we create a SMTP object here it is server, we are sending the mail to gmail server so we have “SMTP.GMAIL.COM” and 587 is server port for TLS.
with “server.ehlo()” we identify ourselves to smtp gmail client and with “server.starttls()” we secure our email with tis encryption.
Then we login to gmail using the provided password and mailID. Followed by as we discussed about the Instance method “sendmail” then we close the server connection.
If the above process is successful then we print the message as “successfully sent the mail” else we catch the Exception in except block and print the message “failed to send mail”.
So this is pretty much about the smtplib module, as it is said Programming language “C” is like Ocean, the same hold good with Mr. PYTHON, there is lot to learn in python this article will just help you to understand the concepts in Layman terms but to explore about it more you can refer the Official Python Document.
So in case of any queries or question do reach us, We will help you in best possible way to keep the things going in you favour, Thank you.