{"id":161,"date":"2016-01-26T16:03:37","date_gmt":"2016-01-26T16:03:37","guid":{"rendered":"http:\/\/www.science-now.org\/giampaolo\/?p=161"},"modified":"2018-08-21T14:20:28","modified_gmt":"2018-08-21T13:20:28","slug":"161","status":"publish","type":"post","link":"https:\/\/www.science-now.org\/giampaolo\/2016\/01\/26\/161\/","title":{"rendered":"Sending emails using Python"},"content":{"rendered":"<p>Thanks to the large standard library, Python is the right tool to write a portable script to send emails. The core functionality to encode an email is contained inside the &#8220;email&#8221; Python module. The conncetion to the smtp is managed by means of the &#8220;smtp&#8221; module.<br \/>\n<!--more--><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom smtplib import SMTP\r\nfrom smtplib import SMTP_SSL\r\n\r\nimport email\r\nimport email.mime.application\r\nimport email.mime.text\r\n<\/pre>\n<p>We are going to write a small class (called &#8220;smtpSender&#8221;) to take advantage of the Python object oriented programming style. The constructor of the class will be in charge of storing information about the smtp server whereas the &#8220;send&#8221; method will perform all the actions required to actually send emails. This splitting (constructor \/ send method) is useful when we want to send multiple email through the same smtp server.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass smtpSender:\r\n    def __init__(self, server_addr=None, server_port=None, \\\r\n        username=None, password=None, \\\r\n        method='plain'):\r\n...\r\n\r\n    def send(self, from_addr, to_addr, subject='', text='', \\\r\n        attachments=[]):\r\n    ...\r\n<\/pre>\n<p>An instance of this class can be created specifying the smtp server address, server port, username, password and encription method.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nif __name__ == &quot;__main__&quot;:\r\n\r\n    # ------------------------------\r\n    # Common settings - Example for GMAIL\r\n\r\n    server_addr = &quot;smtp.gmail.com&quot;\r\n    server_port = 587\r\n    username = &quot;your_email_address@gmail.com&quot;\r\n    password = &quot;your_password&quot;\r\n    method = 'TLS'\r\n\r\n    # ------------------------------\r\n    # Test settings\r\n\r\n    from_addr = &quot;sender@gmail.com&quot;\r\n    to_addr = [&quot;receiver@blabla.com&quot;]\r\n\r\n    subj = &quot;Test&quot;\r\n    message_txt = &quot;Hello!&quot;\r\n\r\n    # ------------------------------\r\n    # Send the message\r\n\r\n    sender = smtpSender(server_addr, server_port, \\\r\n        username, password, method)\r\n\r\n    # Just text!\r\n    sender.send(from_addr, to_addr, subj, message_txt)\r\n\r\n    # With attachment!\r\n    sender.send(from_addr, to_addr, subj, message_txt, \\\r\n        ['attachment.txt'])\r\n<\/pre>\n<p>Please, find the complete code in the <a href=\"http:\/\/www.science-now.org\/giampaolo\/files\/python_send_email.pdf\">document enclosed<\/a>! Any comment is welcomed! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thanks to the large standard library, Python is the right tool to write a portable script to send emails. The core functionality to encode an email is contained inside the &#8220;email&#8221; Python module. The conncetion to the smtp is managed by means of the &#8220;smtp&#8221; module.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[12,10,11],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-programming","tag-email","tag-python","tag-software"],"_links":{"self":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":6,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts\/161\/revisions\/200"}],"wp:attachment":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}