Build Notification Features
02 Jul 2016Data processing time will becomes longer and longer as the increasing rate of data volumes. Users may check-in frequently to see the whether it is finished. Most of the time they will found it hasn't, doing this the users make contact switch which break the flow of whatever the using was doing.
Sometimes the user can't stop doing so, either because they are impatient, or because they really have a deadline to catch. Also, with less likelihood, they might find errors in the processing, either because of the QC check fails, or running out of computational resources.
Giving this fact, it really makes sens to have your program actively inform the user on the process so that they doesn't need to check-in at all. Because users will be notified immediately whenever the whole progressing is completed, or there's error that the user needs to take action onup.
This blog posts walk though the basics of sending Emails in Python, composing and sending out Emails. Each component is broken down into small piceses. It helps you debug/tests Email program, and personalise your Emails. In the end, you should be able to build a email robot.
Prerequisite
Before going into the technical details, you have to check that you are able to send out emails. You need the
[ ]
SMTP server,[ ]
user name,[ ]
password,[ ]
port number, and[ ]
communication protocol.
You could easily found out these information from your Email service provider. An example of Gmail is at Here.
To check if you have all the information correct, run the following snippet. It will try to send out an empty Email to yourself. Make sure you fill in the username and password before hit go.
If you don't see error messages, that's great, you are all set. There should be an Email in your Inbox. It has no subject and for testing in the main body.
If you do, you need to double check the information, and try again. If you are sure that the information are correct, btu still can't send out, check your network configuration, maybe the firewall block the connection.
Once you are able to send out an empty email the next is to compose an full Email.
Compose An Email in Python
An Email is consisted of multiple parts, the Subject, Body, attachments, Signature, and also some meta-data including from, to, and date.
Firstly, create an object of MIMEMutiple()
class. It will be the
building block of your Email. The approached described here is to
add each components into it.
Email meta data
Start with meta data.
Email body
For plain text, simply add
You could also try to compose an complex HTML email in Python, or use diffenret tool to generate the HTML and import in Python.
Attachment
You can attach files of any type in an Email, specially, for image,
you could use MIMEImage
, and for audio, you could use
MIMEAudio
.
But you don't have to be specific. MIMEApplication
would be
sufficeincy for all cases. It will configure the file type of the
attached file. Use it as follows:
Put Everything Together
Wrap everything in a Class
At JBARML, we are planing to send users emails for a progress update. Many of the data generating process are lured together and automated by a workflow manager. The whole programcan takes upto weeks to complete. Actively sending update progeress to user is much more senssible then user loggin in to a remote machien and check it now and then.
In this case,
email to notify user the progress of the data generating workflow.