rpiarduinomusings

Raspberry Pi, Arduino, Sensors and the data they produce

Email Module

This is intended to be used as a module. To test it just supply your specific email connection criteria and run the module from the command line.

When developing programs that need email support with attachments, simply import this module. For example:

import ModSendEmail

Before sending email, you’ll have to provide the connection criteria. Use the config function to do that and pass the required parameters.

config("smtp.gmail.com", 587, "somebody@email.com", "senditnow")

Then send the email. As written, the sendEmail function will only accept picture type attachments.
The parameters are for the subject, imageData and the location of the image file. Such a path might be something like “/mnt/Data/PiPhotos/myPicture.jpg”.

imageData = None
sendMail("Hello!", None, None)

 
The module is listed below.

#********************************************************************** # Module : ModSendEmail.py # Date : 20160526 # # # This module provides the ability to send email. The connection criteria # that is seen in the test function are to be stored in a properties file # as name/value pairs and are accessed outside of this module, keeping # with the philosophy of keeping configurable data external to the code. # # # #********************************************************************** # Import smtplib for the actual sending function import os from datetime import datetime import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart #********************************************************************** #***** VARIABLES ***** #********************************************************************** smtpProvider = "notInitialized" smtpPort = "notInitialized" emailAddress = "notInitialized" password = "notInitialized" #********************************************************************** #***** FUNCTION DECLARATIONS ***** #********************************************************************** #---------------------------------------------------------------------- # #---------------------------------------------------------------------- def config(_smtpProvider, _smtpPort, _emailAddress, _password): global smtpProvider #ex: smtp.gmail.com global smtpPort #ex: 587 global emailAddress #ex: raulmendez@yardwork.com global password smtpProvider = _smtpProvider smtpPort = _smtpPort emailAddress = _emailAddress password = _password return #---------------------------------------------------------------------- # Send eMail #---------------------------------------------------------------------- def sendMail(msgText, imageData, fileName): #use the global variables global smtpProvider #ex: smtp.gmail.com global smtpPort #ex: 587 global emailAddress #ex: raulmendez@yardwork.com global password print "Sending email" UserName = emailAddress UserPassword = password FromAddr = UserName ToAddr = UserName msg = MIMEMultipart() msg['Subject'] = msgText msg['From'] = UserName msg['To'] = UserName text = MIMEText(msgText) msg.attach(text) #Attach image if not blank if(imageData != None and fileName != None): image = MIMEImage(imageData, name = os.path.basename(fileName) ) msg.attach(image) s = smtplib.SMTP(smtpProvider,smtpPort) s.ehlo() s.starttls() s.ehlo() s.login(UserName, UserPassword) s.sendmail(FromAddr, ToAddr, msg.as_string()) s.quit() #---------------------------------------------------------------------- def test(): config("smtp.gmail.com", 587, "somebody@email.com", "senditnow") print "smtpProvider = " + smtpProvider print "smtpPort = " + str(smtpPort) print "emailAddress = " + emailAddress print "password = " + password imageData = None sendMail("Hello!", None, None) return #********************************************************************** #***** M A I N L I N E C O N T R O L L E R ***** #********************************************************************** if __name__ == '__main__': try: test() finally: print("Done!") #********************************************************************** #********************************************************************** #***** E N D O F S O U R C E ***** #********************************************************************** #**********************************************************************
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: