Monday 24 October 2016

Email sending using python Programming...and Your gmail account....

To code a script to send emails could be very useful for reporters. We could use it to create personalized alerts about any topic we want or to monitor our running scripts at a distance.
Note: To send yourself an email with a Python script, you need minimal knowledge of programming. I madethis tutorial for people willing to learn. Once done with the tutorial, you should be able to use the code I wrote below!

#I Personalized alerts

By sending an email to yourself with the conditions of your choice, as coded in your script, you can create completely customized alerts.
You want to closely monitor an elected representative? Code a script that will check his Twitter and Facebook accounts every 10 minutes. If something new has been posted, an email will be automatically sent to you!
Your program could check three times a day all the transcript files of all the meetings he is supposed to attend. He said something? Tada! Your script could copy-paste his interventions in an email that you’ll receive a few seconds later!
Isn’t that great? Google Alerts are a joke compared to this!

#II Monitor you scripts at a distance

Another interesting use: to indicate when your scripts are done or when they stopped for an unexpected reason.
Some scripts can run for a very long time… If you are a web scraping addict, you know what I am talking about. If there is a lot of data to extract, your scripts will give your grey hair!
I remember a scraper I coded for one of my class, for my master’s degree. The goal was to extract all the data from the pro-active disclosure section of Public Works and Government Services Canada. Thousands of contracts, all on different webpages…! On top of that, I was regularly disconnected from their server. It took me days!
But when you are a reporter always running here and there, it’s impossible for you to stay close to your computer and check your beloved scripts! Hopefully, email updates could avoid you a lot of stress.

#III For a basic email

[ EDIT: Gmail made some security change change recently. You now have to allow “less secure apps” on your account. Otherwise, you won’t be able to connect to it with your python script. ]
There’s a native library in Python to send emails: smtplib. No need to install external librairies!
To send a basic email (without subject line), with a Gmail address, the code is fairly simple:
On line 2, it’s the parameters for the Gmail server. First, the server location (or its ip address), then the port to use. If you have an email address from another service, like Yahoo for example, you have to find the corresponding information.
On line 4, there’s a security function, needed to connect to the Gmail server. It will protect your password.
Donc forget to indicate your email address and your password on line 5.
The msg variable will contain your message and the last ligne will send it!

#IV For a more elaborate email

If you want to automatically send an email to the police for an interview each time there’s a press release regarding drug trafficking, you need to send more professional looking emails!
With the code below, you will send a clean email, with a sender, a receiver and a subject line.
To do this, you need two more modules: email.MIMEMultipart et email.MIMEText. They are part of the basic Python librairies. No need to install them.
Again, don’t forget to change the following lines:
  • Line 6 with your email address
  • Line 6 with the receiver’s email address
  • Line 11 with the email subject
  • Line 13 with your message
  • Line 18 with your password

#V To send an email with attachment

[ EDIT: Allow “less secure apps” on your Gmail account before running your script. ]
To have files attached to an email requires a more complicated code. While doing some research, I noticed almost everybody has a slightly different method to do this (my sources are all at the end of this article).
I tried to keep the code as simple as possible. However, many modules are still needed, as you can see below.
In summary, the essential step is to convert the file into a Base64 before sending it. My code works for text files, pdf files, images, audio files and video files!
On top of all the authentification needed, this time you also need to indicate:
  • The name of the file on line 21,
  • The path to the file on line 22.
It’s done! You have everything you need to add email sending to your scripts!