bob12603
Posts: 25
Joined: Mon Oct 06, 2014 9:16 pm

Trying to Send email (gmail) from a Python script

Sun Dec 14, 2014 9:34 pm

I need to be able to send email from a Python script. It doesn't have to be gmail but that is what I am trying (woefully unsuccessfully) at the moment.

So I followed instructions here (http://rpi.tnet.com/project/faqs/smtp) to get SMTP setup. Those instructions included changes to /etc/ssmtp/ssmtp.conf ...

Code: Select all

[email protected]
AuthPass=userpass
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
And I have written a python script identical to the one here (except for the email addresses and password): http://elinux.org/RPi_Email_IP_On_Boot_Debian

Note that I am not, yet, trying to run the script at boot up time.

The problem I am having is that I get a long error message (when I run the script via the IDLE3) the important parts of which seem to be:
SMTPAuthenticationError: (534
Please log in via your web browser and then try again
Learn more at https://support.google.com/mail/bin/ans ... swer=78754
I also received an email sent to my main Google account the subject of which was "Google Account: sign-in attempt blocked". But I have received that message only once although I have tried the script many times.

I've tried every corrective action I've come across but it still doesn't work. I am wondering if I am even on the right track. It seems very strange that I now have the email name and password I've set up for the RPi in both /etc/ssmtp/ssmtp.conf and in the python script. Why would they need to be in both places?

I am also wondering if I should try another email system such as Yahoo or Hotmail. But if possible I would like to stick to Gmail.

I've been banging my head against this wall for about three hours so if anyone has any suggestions I will sure appreciate it.

Thanks, Bob

User avatar
DougieLawson
Posts: 37755
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Trying to Send email (gmail) from a Python script

Sun Dec 14, 2014 10:24 pm

Try this

Code: Select all

import time
from datetime import datetime
import smtplib
# from email.mime.image import MIMEImage

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

f_time = datetime.now().strftime('%a %d %b @ %H:%M')

toaddr = '[email protected]'    # redacted CHANGE ME
me = '[email protected]' # redacted CHANGE ME
password = 'password' # redacted CHANGE ME
subject = 'Test message ' + f_time

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
msg.preamble = "Test message @ " + f_time
message = "Main body text here"
html = "<html><head></head><body><h1>Title</h1><p>Main text</body></html>"
plainText = MIMEText(message, 'plain')
htmlText = MIMEText(html,'html')
msg.attach(plainText)
msg.attach(htmlText)

try:
   s = smtplib.SMTP('smtp.gmail.com',587)
   s.set_debuglevel(True)
   s.starttls()
   s.login(me,password)
   s.send_message(msg)
   s.quit()
except:
   print ("Error: unable to send email")
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DirkS
Posts: 10219
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Trying to Send email (gmail) from a Python script

Sun Dec 14, 2014 10:25 pm

bob12603 wrote:The problem I am having is that I get a long error message (when I run the script via the IDLE3) the important parts of which seem to be:
SMTPAuthenticationError: (534
Please log in via your web browser and then try again
Learn more at https://support.google.com/mail/bin/ans ... swer=78754
Have you followed the advice in the message: log in via a web browser (on any PC) and then try to send a message again from the Pi?

Gr.
Dirk

bob12603
Posts: 25
Joined: Mon Oct 06, 2014 9:16 pm

Re: Trying to Send email (gmail) from a Python script

Sun Dec 14, 2014 11:47 pm

DirkS wrote:
bob12603 wrote:The problem I am having is that I get a long error message (when I run the script via the IDLE3) the important parts of which seem to be:
SMTPAuthenticationError: (534
Please log in via your web browser and then try again
Learn more at https://support.google.com/mail/bin/ans ... swer=78754
Have you followed the advice in the message: log in via a web browser (on any PC) and then try to send a message again from the Pi?

Gr.
Dirk
Several times!

DirkS
Posts: 10219
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Trying to Send email (gmail) from a Python script

Mon Dec 15, 2014 12:30 am

DougieLawson wrote:Try this
Works, but only in Python3.

@Bob: FYI, you don't need to set up all the external programs / configs if you want to use smtp from Python. Dougies's script works fine without them.
The code at http://rosettacode.org/wiki/Send_an_email#Python works for me with Python 2.7

Gr.
Dirk.

bob12603
Posts: 25
Joined: Mon Oct 06, 2014 9:16 pm

Re: Trying to Send email (gmail) from a Python script

Mon Dec 15, 2014 1:36 pm

Dougie, Dirk,

Thank you both. I might not be able to get back to this until Tuesday night or Wed. but I will get back to it and let you know the outcome. I appreciate your help.

Bob

TrevorAppleton
Posts: 74
Joined: Wed May 30, 2012 7:26 pm
Contact: Website

Re: Trying to Send email (gmail) from a Python script

Mon Dec 15, 2014 10:08 pm

If it helps I have previously written a blog post explaining how to do just this.

http://trevorappleton.blogspot.co.uk/20 ... ython.html
Check out my blog post for Raspberry Pi and Python tutorials.

http://trevorappleton.blogspot.co.uk/

bob12603
Posts: 25
Joined: Mon Oct 06, 2014 9:16 pm

Re: Trying to Send email (gmail) from a Python script

Tue Dec 16, 2014 1:25 pm

Dougie,

I had just a few minutes to try what you posted Sunday night. I got the error text below. I've taken a brief look at it and don't see anything I might have a chance of understanding. I have to head out now and will not have a chance to get back to this until tonight.

Thanks, Bob

Code: Select all

send: 'ehlo [127.0.1.1]\r\n'
reply: b'250-mx.google.com at your service, [67.82.240.254]\r\n'
reply: b'250-SIZE 35882577\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-CHUNKING\r\n'
reply: b'250 SMTPUTF8\r\n'
reply: retcode (250); Msg: b'mx.google.com at your service, [67.82.240.254]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8'
send: 'STARTTLS\r\n'
reply: b'220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: b'2.0.0 Ready to start TLS'
send: 'ehlo [127.0.1.1]\r\n'
reply: b'250-mx.google.com at your service, [67.82.240.254]\r\n'
reply: b'250-SIZE 35882577\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-CHUNKING\r\n'
reply: b'250 SMTPUTF8\r\n'
reply: retcode (250); Msg: b'mx.google.com at your service, [67.82.240.254]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8'
send: 'AUTH PLAIN AGJvYmdhdHNraVJQaTBAZ21haWwuY29tAGFsbFBJcHVycDBzZQ==\r\n'
reply: b'534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuDX\r\n'
reply: b'534-5.7.14 sFX46z3clOOKjKyYPOh3RFvfp_pjD_-wH4H1u-PXCLdzOSwm4Wph_2i0wOAFcU3KGRLv4y\r\n'
reply: b'534-5.7.14 w0Gb1fb4zWY-uZ2m0U69YGEp4WFq1751NLCLYZjk1nt2SqbNzi06tmedwTUH2l5x3nrKv1\r\n'
reply: b'534-5.7.14 evFAbXGQpjlTggzYOQSiAWdEGFG6vwCB0uHumc5RMxSIxAiWoZ8b_AlcV-LNZpWbOViysG\r\n'
reply: b'534-5.7.14 h3YRMaA> Please log in via your web browser and then try again.\r\n'
reply: b'534-5.7.14 Learn more at\r\n'
reply: b'534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 8sm693255qgf.36 - gsmtp\r\n'
reply: retcode (534); Msg: b'5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuDX\n5.7.14 sFX46z3clOOKjKyYPOh3RFvfp_pjD_-wH4H1u-PXCLdzOSwm4Wph_2i0wOAFcU3KGRLv4y\n5.7.14 w0Gb1fb4zWY-uZ2m0U69YGEp4WFq1751NLCLYZjk1nt2SqbNzi06tmedwTUH2l5x3nrKv1\n5.7.14 evFAbXGQpjlTggzYOQSiAWdEGFG6vwCB0uHumc5RMxSIxAiWoZ8b_AlcV-LNZpWbOViysG\n5.7.14 h3YRMaA> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 8sm693255qgf.36 - gsmtp'
send: 'AUTH LOGIN Ym9iZ2F0c2tpUlBpMEBnbWFpbC5jb20=\r\n'
reply: b'334 UGFzc3dvcmQ6\r\n'
reply: retcode (334); Msg: b'UGFzc3dvcmQ6'
send: 'YWxsUElwdXJwMHNl\r\n'
reply: b'534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvmS\r\n'
reply: b'534-5.7.14 RjmH3wpe7enfNzl0uK5WrK338l7sKFufDg6z6pwLudl1SE8_1D5tUfKjdtTNa7KgpkA5UL\r\n'
reply: b'534-5.7.14 Oiuy1mbp-9Pl0I26bn9g_cZccEcvOYdiLmDqvT3qz4vghWFkIjg1uMyAHCF5L4ovIaf6F4\r\n'
reply: b'534-5.7.14 VkLxJDK9gTOELNXEZRsGscEl92DgHWiKuwBb_ylgbQ2IDuIpniTry1corlhEDKRhBO2Opd\r\n'
reply: b'534-5.7.14 TZ8_D3MOYVuhV87O7TBkm4jD5fGQ> Please log in via your web browser and\r\n'
reply: b'534-5.7.14 then try again.\r\n'
reply: b'534-5.7.14 Learn more at\r\n'
reply: b'534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 8sm693255qgf.36 - gsmtp\r\n'
reply: retcode (534); Msg: b'5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvmS\n5.7.14 RjmH3wpe7enfNzl0uK5WrK338l7sKFufDg6z6pwLudl1SE8_1D5tUfKjdtTNa7KgpkA5UL\n5.7.14 Oiuy1mbp-9Pl0I26bn9g_cZccEcvOYdiLmDqvT3qz4vghWFkIjg1uMyAHCF5L4ovIaf6F4\n5.7.14 VkLxJDK9gTOELNXEZRsGscEl92DgHWiKuwBb_ylgbQ2IDuIpniTry1corlhEDKRhBO2Opd\n5.7.14 TZ8_D3MOYVuhV87O7TBkm4jD5fGQ> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 8sm693255qgf.36 - gsmtp'
Error: unable to send email
>>> 

DirkS
Posts: 10219
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Trying to Send email (gmail) from a Python script

Tue Dec 16, 2014 1:41 pm

You may have to enable access for 'less secure apps' in your gmail security settings.
It should be in Gmail settings / Accounts / Other google account settings / Access for less secure apps.
If you have set up two factor authentication you'll probably have to set up application specific password.

Gr.
Dirk.

User avatar
DougieLawson
Posts: 37755
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Trying to Send email (gmail) from a Python script

Tue Dec 16, 2014 1:43 pm

Check your gmail id and password.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bob12603
Posts: 25
Joined: Mon Oct 06, 2014 9:16 pm

Re: Trying to Send email (gmail) from a Python script

Wed Dec 17, 2014 5:09 am

Hi Dougie,

I just signed on to gmail.com by cutting and pasting AuthUser and AuthPass from /etc/ssmtp/ssmtp.conf into the Gmail sign in dialog. So yes, they are fine.

Thanks for sticking with me on this. Bob

User avatar
DougieLawson
Posts: 37755
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Trying to Send email (gmail) from a Python script

Wed Dec 17, 2014 10:11 am

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bob12603
Posts: 25
Joined: Mon Oct 06, 2014 9:16 pm

Re: Trying to Send email (gmail) from a Python script

Tue Dec 23, 2014 10:36 pm

Dougie and Dirk,

THANKS! And sorry for my long absence.

I had visited the url provided by Dougie and had enabled less secure applications before my first post. But when both of you mentioned it I thought maybe I should take a look at the url again. Maybe I hadn't read carefully enough or hadn't typed carefully enough. But my mistake was much dumber than carelessness. I think I must have enabled the wrong account for less secure applications. Going though it again and, this time, enabling the right account for less secure applications solved my problem.

Thanks very much to you both.

Bob

Bwleeds
Posts: 1
Joined: Tue Mar 29, 2016 8:23 pm

Re: Trying to Send email (gmail) from a Python script

Tue Mar 29, 2016 8:29 pm

When I use the app specific password for 2-factor authentication, where do I enter it? I use these often but I'm not sure I understand how to activate Python scripts for regular use.

Thanks!

DirkS
Posts: 10219
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Trying to Send email (gmail) from a Python script

Wed Mar 30, 2016 10:29 am

Bwleeds wrote:When I use the app specific password for 2-factor authentication, where do I enter it? I use these often but I'm not sure I understand how to activate Python scripts for regular use.

Thanks!
Just as any 'normal' password. For example: in http://trevorappleton.blogspot.co.uk/20 ... ython.html you would enter it in the line

Code: Select all

GMAIL_PASSWORD = 'your_gmail_password' #CAUTION: This is stored in plain text!

brianmanee
Posts: 5
Joined: Mon Jan 11, 2016 7:09 am

Re: Trying to Send email (gmail) from a Python script

Thu Nov 02, 2017 5:34 am

DirkS wrote:
Sun Dec 14, 2014 10:25 pm
bob12603 wrote:The problem I am having is that I get a long error message (when I run the script via the IDLE3) the important parts of which seem to be:
SMTPAuthenticationError: (534
Please log in via your web browser and then try again
Learn more at https://support.google.com/mail/bin/ans ... swer=78754
Have you followed the advice in the message: log in via a web browser (on any PC) and then try to send a message again from the Pi?

Gr.
Dirk
It is because 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:

Go to this link and select Turn On

https://www.google.com/settings/security/lesssecureapps

Moreover google block an ip when you try to send a email since a unusual location, so, you can unblock in the next link

https://support.google.com/accounts/answer/6009563

and clicked in

accounts.google.com/DisplayUnlockCaptcha .

More on...smtp mail

Return to “Python”