{"id":129,"date":"2013-07-06T10:06:26","date_gmt":"2013-07-06T10:06:26","guid":{"rendered":"https:\/\/code4reference.com\/?p=129"},"modified":"2023-10-01T13:30:40","modified_gmt":"2023-10-01T13:30:40","slug":"code4referencesimple-python-script-to-send-an-email-code4reference","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=129","title":{"rendered":"Simple Python script to send an email."},"content":{"rendered":"<p>Programmatically sending an email is really a good tool for developer\u2019s paraphernalia. This is really handy when one automates task and wants to get notified by an email for success or failure. Lately I have written Python script to automate some jobs and incorporated method to send email notification. I thought to share this simple script here so my readers can get benefited.<\/p>\n<pre>\n#!\/usr\/bin\/env python\nimport smtplib\nfrom email.mime.text import MIMEText\n\nEMAIL_SUBJECT = \"Email from Python script\"\nEMAIL_FROM = \"notification@code4reference.com\"\nEMAIL_RECEIVERS = ['your_email@gmail.com']\n\ndef listToStr(lst):\n    \"\"\"This method makes comma separated list item string\"\"\"\n\n    return ','.join(lst)\n\ndef getCurrentTime():\n    return datetime.now().strftime(DB_DATE_FORMAT)\n\ndef send_email(msg):\n    \"\"\"This method sends an email\"\"\" \n  \n    msg_header = \"From: \" + EMAIL_FROM + \"\\n\" + \\\n                 \"To: \" + listToStr(EMAIL_RECEIVERS) + \"\\n\" + \\\n                 \"Subject: \" + EMAIL_SUBJECT + \"\\n\"\n    msg_body =  msg_header + msg\n\n    try:\n      smtpObj = smtplib.SMTP('localhost')\n      smtpObj.sendmail(EMAIL_FROM, EMAIL_RECEIVERS, msg_body)\n    except SMTPException as error:\n      print \"Error: unable to send email :  {err}\".format(err=error)\n\ndef main():\n    \"\"\"This is a simple main() function which demonstrate sending of email using smtplib.\"\"\"\n    send_email(\"Test email was generated by Python using smtplib and email libraries\");\n\nif __name__ == \"__main__\":\n   \"\"\"If this script is run as stand alone then call main() function.\"\"\"\n    main()\n<\/pre>\n<p>Before running this script make sure your system has Mail Transfer Agent(MTA) configured. If it is not then read <a href=\"http:\/\/code4reference.com\/2013\/07\/setting-email-server-on-ec2-using-exim4\/\">this post to configure Mail-server<\/a> on Ubuntu 12.04. You can also get the source code from <a href=\"http:\/\/github.com\/rakeshcusat\/Code4Reference\/blob\/master\/PythonPrograms\/SendEmail.py\">github\/code4reference<\/a>.<\/p>\n<p>Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below. You can find <a href=\"http:\/\/www.facebook.com\/Code4Reference\">Facebook page here<\/a>.<\/p>\n<h4>Related topics<\/h4>\n<ol>\n<li><a href=\"http:\/\/code4reference.com\/2013\/07\/setting-email-server-on-ec2-using-exim4\/\">Setting Mail Transfer Agent (MTA) on EC2 using exim4<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Programmatically sending an email is really a good tool for developer\u2019s paraphernalia. This is really handy when one automates task and wants to get notified by an email for success or failure. Lately I have written Python script to automate some jobs and incorporated method to send email notification. I thought to share this simple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":1,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions\/268"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}