{"id":721,"date":"2018-09-15T06:00:22","date_gmt":"2018-09-15T06:00:22","guid":{"rendered":"http:\/\/buklijas.info\/blog\/?p=721"},"modified":"2018-12-14T21:13:55","modified_gmt":"2018-12-14T21:13:55","slug":"sending-email-from-python","status":"publish","type":"post","link":"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/","title":{"rendered":"Sending email from Python"},"content":{"rendered":"

Published on:<\/strong> 15.09.2018<\/p>\n

I will show how to send emails from Python programing language using your Gmail account.<\/p>\n

Mail server<\/h3>\n

When you want to send email from GUI\/CLI app or from some computer language (in this example Python), you need to have access to a mail server.<\/p>\n

The mail server<\/a> is a computer that is in charge of sending and receiving your email.<\/p>\n

Usually, you have access to the mail server via username and password.<\/p>\n

With Gmail username is your email address and password should only be known to you :-).<\/p>\n

Use yagmail<\/h3>\n

For sending emails from Python using your Gmail account IMHO best is to use yagmail<\/a> packet.<\/p>\n

You can waste time with smtplib<\/a> library, but if you are using a Gmail account, just use yagmail<\/a>.<\/p>\n

Install it with:
\npipenv install yagmail<\/code><\/p>\n

Code examples<\/h3>\n

This is just a simple code example, for production code do not put your password in the source code.<\/p>\n

Personally, I use one JSON file (this file is committed to source control with dummy credentials) per project for storing usernames and passwords for my personal projects.<\/p>\n

This is a simple one line example:<\/p>\n

import yagmail     \nyag = yagmail.SMTP('YOUR_EMAIL@gmail.com', 'YOUR_PASSWORD').send('to@someone.com', 'subject', 'This is the body')\n<\/code><\/pre>\n

I usually use it like this:<\/p>\n

import yagmail\nyag = yagmail.SMTP('YOUR_EMAIL@gmail.com', 'YOUR_PASSWORD')\nbody = 'large body that I have generated '\nyag.send('to@someone.com', 'subject', body)\n<\/code><\/pre>\n

Adding image in the body of the email, you need to use yagmail.inline<\/code>:<\/p>\n

import yagmail\nyag = yagmail.SMTP('YOUR_EMAIL@gmail.com', 'YOUR_PASSWORD')\ncontents = [\"Some Text\", yagmail.inline( full_path_to_image )]\nyag.send(send_to, subject, contents)\n<\/code><\/pre>\n

My advice and experience<\/h3>\n

Gmail have daily limits of how much emails can you send.<\/p>\n

At the time of this writing, it is 500 emails per day<\/a>, if you exceed it you will not be able to send emails for next 24h (at least that happened to me, and I have sent only 100 emails in a row).<\/p>\n

So, my advice is to have separate Gmail account for just for sending emails automatically from your code, you do not want to lose the ability to send email from your personal email account (I was there and it is not funny).<\/p>\n

\"\"<\/a><\/p>\n

Also, have some delay between sending emails, when I got my account locked for 24h I have only sent around 100 emails in a row, but after I have added time.sleep(15)<\/code> I never had that problem again.<\/p>\n

I am not using Gmail to spam people, I am using it for sending the email report to myself, mostly from web scraping tasks (around 20 per day).<\/p>\n

If you have error 534<\/h4>\n
  File \"\/usr\/lib64\/python3.6\/smtplib.py\", line 730, in login\n    raise last_exception\n  File \"\/usr\/lib64\/python3.6\/smtplib.py\", line 721, in login\n    initial_response_ok=initial_response_ok)\n  File \"\/usr\/lib64\/python3.6\/smtplib.py\", line 642, in auth\n    raise SMTPAuthenticationError(code, resp)\nsmtplib.SMTPAuthenticationError: (534, b'5.7.14 <https:\/\/accounts.google.com\/signin\/continue?sarp=1&scc=1&plt=AKgnsbvo\\n5.7.14 ADvphf123p0OEtQXN-eqs-JnkK6mEO6qRtmz4c4RpYb9J_QmyzfLS8aYf3yjjSGcN7melQ\\n5.7.14 5RpQd9saqWhfH6cjNUJclYW1qrLDpLPwbVOcAqipfT8-mHk_v5P1jvvDY3mnES9n4RfeHi\\n5.7.14 AV6enQwb-XtJH9CMJUU4JrtJlhYDnOIBOluQoWbCoiYhs7JV-ej11QqeaJQTVkTIZStB4p\\n5.7.14 HWPlUWD3LHmsIgnWN1N68psDRNs5C75SnCiYMr4wc7ve_Vj_Yf> Please log in via\\n5.7.14 your web browser and then try again.\\n5.7.14  Learn more at\\n5.7.14  https:\/\/support.google.com\/mail\/answer\/78754 204-v6sm5976350wmh.25 - gsmtp')\n<\/code><\/pre>\n

Solution is<\/p>\n

\n Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can, however, turn on\/off this safety feature by going to the link below:<\/p>\n

Go to this link and select Turn On
\n
https:\/\/www.google.com\/settings\/security\/lesssecureapps<\/a>\n<\/p><\/blockquote>\n

From:
\n
https:\/\/stackoverflow.com\/a\/26852782\/2006674<\/a><\/p>\n

Conclusion<\/h3>\n

Sending emails from Python is easy with yagmail<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

Published on: 15.09.2018 I will show how to send emails from Python programing language using your Gmail account. Mail server When you want to send email from GUI\/CLI app or from some computer language (in this example Python), you need to have access to a mail server. The mail server is a computer that is […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"Sending emails from Python #yagmail #python #email","jetpack_is_tweetstorm":false},"categories":[27],"tags":[47,4,34],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"yoast_head":"\nSending email from Python - Sasa Buklijas<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sending email from Python - Sasa Buklijas\" \/>\n<meta property=\"og:description\" content=\"Published on: 15.09.2018 I will show how to send emails from Python programing language using your Gmail account. Mail server When you want to send email from GUI\/CLI app or from some computer language (in this example Python), you need to have access to a mail server. The mail server is a computer that is […]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Sasa Buklijas\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-15T06:00:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-14T21:13:55+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/buklijas.info\/blog\/wp-content\/uploads\/2018\/09\/gmail_send_limit.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\">\n\t<meta name=\"twitter:data1\" content=\"Sasa Buklijas\">\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data2\" content=\"3 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/buklijas.info\/blog\/#website\",\"url\":\"http:\/\/buklijas.info\/blog\/\",\"name\":\"Sasa Buklijas\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/buklijas.info\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/i1.wp.com\/buklijas.info\/blog\/wp-content\/uploads\/2018\/09\/gmail_send_limit.png?fit=549%2C173\",\"width\":549,\"height\":173},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/#webpage\",\"url\":\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/\",\"name\":\"Sending email from Python - Sasa Buklijas\",\"isPartOf\":{\"@id\":\"http:\/\/buklijas.info\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/#primaryimage\"},\"datePublished\":\"2018-09-15T06:00:22+00:00\",\"dateModified\":\"2018-12-14T21:13:55+00:00\",\"author\":{\"@id\":\"http:\/\/buklijas.info\/blog\/#\/schema\/person\/780025d597f1c5df3cc156eaffc8c561\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/buklijas.info\/blog\/2018\/09\/15\/sending-email-from-python\/\"]}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/buklijas.info\/blog\/#\/schema\/person\/780025d597f1c5df3cc156eaffc8c561\",\"name\":\"Sasa Buklijas\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/buklijas.info\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/9f6f7de5a4882517ca0e4a8ebd607925?s=96&d=mm&r=g\",\"caption\":\"Sasa Buklijas\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5YHGV-bD","_links":{"self":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":11,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":789,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions\/789"}],"wp:attachment":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}