You are on page 1of 3

Declaring string variables

Update and upgrade packages

Sudo apt-get install ssmtp mailutils
Sudo nano /etc/ssmtp/ssmtp.conf

Forward to a Gmail Mail server

To configure SSMTP, you will have to edit its configuration file( /etc/ssmtp/ssmtp.conf ) and
enter your settings
# The user that gets all the mails (UID < 1000, usually the admin)
# The mail server (where the mail is sent to), both port 465 or 587 should be
# See also
# The address where the mail appears to come from for user authentication.
# The full hostname
# Use SSL/TLS before starting negotiation
# Username/Password
# Email 'From header's can override the default domain?

To test whether the Gmail server will properly forward your email:

$ echo test | mail -v -s "testing ssmtp setup"

Note: Gmail has recently started blocking emails from senders that do not authenticate using OAuth. To
allow SSMTP to use gmails SMTP server, you need to allow access to unsecure apps.
OAuth: provides to clients a "secure delegated access" to server resources on behalf of a resource owner.
It specifies a process for resource owners to authorize third-party access to their server resources without
sharing their credentials.

SSMTP (Secure simple mail transfer protocol):

SSMTP is a program which delivers email from a local computer to a configured mail
host (mail hub). It is not a mail server (like feature-rich mail server send mail) and does not
receive mail, expand aliases or manage a queue. One of its primary uses is for forwarding
automated email (like system alerts) off your machine and to an external email address.
MIME: Multipurpose Internet Mail Extensions. It allows documents, other than plain text, to
be attached to an email message.
B64dCode (Base 64 Encode/Decode Utility): will decode MIME/Base 64 files back into their
original configuration or encode any other file type into MIME/Base 64. Base 64 is a coding
standard that allows 8-bit files (graphics, compressed files, word processing documents, etc.) to
be transported via Internet email.
how to create and send a simple text message code:
#import smtplib for the actual sending function
Import smtplib
#import the email modules we will need
Fp = open (textfile,rb)
Msg = mimetext(fp,read( ))
Msg[subject] = the contents of %s %textfile
Msg[from] = me
Msg[to] = you
S= smtplib.smtp(localhost)
s.sendmail(me,[you],msg.as_string( ) )
s.quit( )

mail with attachment

Sudo code:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import base64
import smtplib
def Mail_sent():
strFrom ='
strTo = ''
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Raspberry pi image'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgText = MIMEText('This is the alternative plain text message.')
msgText = MIMEText(RFID_NO + email_body + '<img
src="cid:image1"><br>', 'html')
fp = open('img00.jpg', 'rb')
msgImage = MIMEImage(
msgImage.add_header('Content-ID', '<image>')
smtp = smtplib.SMTP('')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
print "mail sent successfully"

Mail_sent() ##fuction call