sending_email

= Python - Sending Email using SMTP =

  



Simple Mail Transfer Protocol (SMTP) is a protocol which handles sending e-mail and routing e-mail between mail servers. Python provides **smtplib** module which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. Here is a simple syntax to create one SMTP object which can later be used to send an email: code import smtplib

smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )

code || Here is the detail of the parameters: > > > An SMTP object has an instance method called **sendmail**, which will typically be used to do the work of mailing a message. It takes three parameters: > > >
 *  **host:** This is the host running your SMTP server. You can specifiy IP address of the host or a domain name like tutorialspoint.com. This is optional argument.
 *  **port:** If you are providing //host// argument then you need to specifiy a port where SMTP server is listening. Usually this port would be 25.
 *  **local_hostname**: If your SMTP server is running on your local machine then you can specify just //localhost// as of this option.
 *  The //sender// - A string with the address of the sender.
 *  The //receivers// - A list of strings, one for each recipient.
 *  The //message// - A message as a string formatted as specified in the various RFCs.

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here is a simple way to send one email using Python script. Try it once: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import smtplib

sender = 'from@fromdomain.com' receivers = ['to@todomain.com']

message = """From: From Person <from@fromdomain.com> To: To Person <to@todomain.com> Subject: SMTP e-mail test

This is a test e-mail message. """

try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email" code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mails requires a **From**, **To**, and **Subject** header, separated from the body of the e-mail with a blank line. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">To send the mail you use //smtpObj// to connect to the SMTP server on the local machine and then use the //sendmail// method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these aren't always used to route mail). <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If you're not running an SMTP server on your local machine, you can use //smtplib// client to communicate with a remote SMTP server. Unless you're using a webmail service (such as Hotmail or Yahoo! Mail), your e-mail provider will have provided you with outgoing mail server details that you can supply them, as follows: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">smtplib.SMTP('mail.your-domain.com', 25) code || = Sending an HTML email using Python: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">When you send a text message using Python then all the content will be treated as simple text. Even if you will include HTML tags in a text message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. But Python provides option to send an HTML message as actual HTML message. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">While sending an email message you can specify a Mime version, content type and character set to send an HTML email.

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following is the example to send HTML content as an email. Try it once: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import smtplib

message = """From: From Person <from@fromdomain.com> To: To Person <to@todomain.com> MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

This is HTML message. This is headline. """

try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email" code || = Sending Attachements as an e-mail: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">To send an email with mixed content requires to set **Content-type** header to**multipart/mixed**. Then text and attachment sections can be specified within **boundaries**. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">A boundary is started with two hyphens followed by a unique number which can not appear in the message part of the email. A final boundary denoting the email's final section must also end with two hyphens. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Attached files should be encoded with the **pack("m")** function to have base64 encoding before transmission.

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following is the example which will send a file **/tmp/test.txt** as an attachment. Try it once: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import smtplib import base64

filename = "/tmp/test.txt"

fo = open(filename, "rb") filecontent = fo.read encodedcontent = base64.b64encode(filecontent) # base64
 * 1) Read a file and encode it into base64 format

sender = 'webmaster@tutorialpoint.com' reciever = 'amrood.admin@gmail.com'

marker = "AUNIQUEMARKER"

body =""" This is a test email to send an attachement. """ part1 = """From: From Person <me@fromdomain.net> To: To Person <amrood.admin@gmail.com> Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker)
 * 1) Define the main headers.

part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit
 * 1) Define the message action

%s --%s """ % (body,marker)

part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s
 * 1) Define the attachment section

%s --%s-- """ %(filename, filename, encodedcontent, marker) message = part1 + part2 + part3

try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, reciever, message) print "Successfully sent email" except Exception: print "Error: unable to send email" code ||

<span style="background-color: #ffffff; color: #900b09; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> <span style="background-color: #ffffff; color: #900b09; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> <span style="background-color: #ffffff; color: #900b09; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">