In python sending e-mail can be done by using a protocol named Simple Mail Transfer Protocol (SMTP), which handles sending e-mail and routing e-mail between mail servers. Python provides another module called 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.
Syntax:
Parameters used in above syntax are explained below:
- host − This is the host running your SMTP server. This is an optional argument. It specifies the IP address of the host or a domain name like a Python network.
- port − If a host argument is provided, then it’ll be needed to specify a port, where the 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.
An SMTP object has an instance method called Sendmail, which is typically used to do the work of mailing a message. It takes three parameters as given below:
- Sender − A string with the address of the sender.
- Receivers − A list of strings, one for each recipient.
- Message − A message as a string formatted as specified in the various RFCs.
Example:
#Python sending email import smtplib sender = 'from@trydomain.com' receivers = ['t0@trydomain.com'] message = """From: From Person <from@trydomain.com> To: To Person <to@trydomain.com> Subject: SMTP e-mail test This is a demo e-mail message. """ try: smtpObject = smtplib.SMTP('localhost') smtpObject.sendmail(sender, receiver, message) print "Successfully sent demo email" except SMTPException: print "Error: unable to send demo email"
In the above example, we have placed a basic e-mail in the message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and Subject header, separated from the body of the e-mail with a blank line. To send the mail smtpObj is used to connect 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.
Sending an HTML E-mail using Python:
After a text message has been sent utilizing Python, that point all the content is treated as basic content. Indeed in case you include HTML labels in a content message, it is shown as basic content and HTML labels will not be organized according to HTML syntax. But Python gives the choice to send an HTML message as a real HTML message. While sending an e-mail message, you’ll specify an Emulate form, substance sort, and character set to send an HTML e-mail.
Example:
#sending html e-mail import smtplib message = """From: From 1person <from@trydomain.com> To: To 2Person <to@trydomain.com> MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test HTML E-mail <b>HTML message.</b> <h1>headline.</h1> """ try: smtpObject = smtplib.SMTP('localhost') smtpObject.sendmail(sender, receivers, message) print "Successfully sent demo email" except SMTPException: print "Error: unable to send demo email"
Sending Attachments as an E-mail:
To send an e-mail with mixed substance requires setting the Content-type header to multipart/mixed. At that point, content and connection segments can be specified inside boundaries. A boundary is begun with two hyphens taken after by a special number, which cannot appear within the message portion of the e-mail. The last boundary signifying the e-mail’s last segment must moreover end with two hyphens. The attached records should be encoded with the pack(“m”) work to have base64 encoding before transmission.
Example:
#Sending Attachments as an E-mail import smtplib import base64 filename = "/tmp/test.txt" #Read a file and encode it into base64 format fo = open(filename, "rb") filecontent = fo.read() encodedcontent = base64.b64encode(filecontent) # base64 sender = 'from@pythone-mail.com' reciever = 'to.admin@gmail.com' marker = "PYTHON E-MAIL" body =""" A demo email to send an attachement. """ # Defining main headers part1 = """From: From 1Person <from@pythone-mail.net> To: To 2Person <to.admin@gmail.com> 1Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker) # Defining message action part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s """ % (body,marker) # Defining attachment section part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s %s --%s-- """ %(filename, filename, encodedcontent, marker) message = part1 + part2 + part3 try: smtpObject = smtplib.SMTP('localhost') smtpObject.sendmail(sender, reciever, message) print "Successfully sent demo e-mail" except Exception: print "Error: unable to send demo e-mail"