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)
root=username@gmail.com
# The mail server (where the mail is sent to), both port 465 or 587 should be
acceptable
# See also http://mail.google.com/support/bin/answer.py?answer=78799
mailhub=smtp.gmail.com:587
# The address where the mail appears to come from for user authentication.
rewriteDomain=gmail.com
# The full hostname
hostname=localhost
# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes
# Username/Password
AuthUser=username
AuthPass=password
# Email 'From header's can override the default domain?
FromLineOverride=yes

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

$ echo test | mail -v -s "testing ssmtp setup" tousername@somedomain.com

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( ))
Fp.close()
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 = sender_mail_id@gmail.com'
strTo = 'receiver_mail_id@gmail.com'
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')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText(RFID_NO + email_body + '<img
src="cid:image1"><br>', 'html')
msgAlternative.attach(msgText)
fp = open('img00.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image>')
msgRoot.attach(msgImage)
u='sender_mail_id@gmail.com'
p='password'
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.starttls()
smtp.login(u,p)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
print "mail sent successfully"

Mail_sent() ##fuction call