Story behind this project
Sending emails has been a consistent time-eater for most of us. Especially those who are working in IT offices like me, I have to send hundreds of emails everyday and monotony occurs when I have to send the same email copied and pasting it to several people because of many reasons, mainly because I cannot CC that mail to everyone because of some organizational restrictions or maybe I just forgot, which is so common for me. In case of such events, I have to open the previous email to copy it and paste it on another email, write down or copy the email address and hit Send. The easier it is to tell, the harder the job is. It becomes boring and I spend almost a couple of hour everyday addressing emails and sending them, but a job which cannot be bunked nevertheless.
The most recent challenge which I faced was this. My Netflix subscription was over and so I had to order a new one. This time, I did not want to take it alone, because it costs more. So some of my colleagues and friends joined me in the more screens plan. But more people in the group means it is also my responsibility for their Netflix-ing, for any updates on a password change and anything important or trivial. So, I have in hand a more daunting task almost every month to send the same email to different people.
I decided to automate this via some inbuilt Python packages and only one click on a script would do this whole task for me. It would send all the emails to the mentioned addresses and I would remain free to, maybe, Netflix more.
You can find the code for this project at GitHub here and can fork or star it from the buttons above.
- smtplib - A native library in Python, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
- email - An email and MIME handling package, also native in Python.
Working of the script
Automatic Email Sender sends mails for you, when you run the script in your terminal, with proper email text and body. It is actually one Python script at its core, run by two different inbuilt Python packages, which are mentioned above. We import these packages in the first three lines of the program.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
Now, all the email IDs are mentioned here, the ID you want it to be delivered it from (your own ID), the recipients (for To :) and also the BCC recipients in the recipients array. We will talk more about how the BCC feature works only when the addresses are mentioned in a different array like this. The To addresses are mentioned in to_addr array.
fromaddr = "YOUR OWN EMAIL ID" to_addr = ['EMAIL ID 1', 'EMAIL ID 2'] recipients = ['EMAIL ID 3','EMAIL ID 4']
MIMEMultipart class now takes the inputs for the From, To and Subject fields. Notice that I do not include the BCC recipients to the To part here, because the only information that a receiver receives is whatever we hook this msg object with and we strictly do not want that the To recipients can see who I have BCC-ed the email. The BCC recipients array is used later.
msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = ", ".join(to_addr) msg['Subject'] = "SUBJECT OF EMAIL"
The body of the email is now attached to msg object and the type of body is set to plain, which can also be sent to HTML by html parameter instead of plain, for which you have to write a properly formatted HTML file in body.
body = "EMAIL TEXT" msg.attach(MIMEText(body, 'plain'))
Now, smtp starts its job by first setting its parameters via GMail, because here I am sending emails via a GMail ID. Server location and port number are the two paramters. For any other mail service, you have to replace both of these below.
server = smtplib.SMTP('smtp.gmail.com', 587)
Next, a security function protects your password when you enter it below.
SMTPlib logs in to your email ID when you enter your mail password.
server.login(fromaddr, "YOUR EMAIL PASSWORD")
The whole msg object is now converted to a String to be sent.
text = msg.as_string()
The server object now sends the mail by accepting the following parameters. Here, we use the recipients array of BCC recipients, via which we achieve the objective of not hooking it up the msg object but still adding it to the senders’ list.
server.sendmail(fromaddr, to_addr + recipients, text)
The server object is shut down and the program is quit by the following line.
The script is done and another prerequisite before you run this on your computer is, you have to allow GMail to allow to be used by less secure apps because SMTP is one. You can do it here.
Now you are good to go and set up for automatically your sending emails by just running this whole script.