Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

email via gmail not working

Wed Oct 30, 2019 11:49 pm

Anyone know why this is hanging at the server=smtplib.SMTP('smtp.gmail.com',587) line? It will print line 1 but not line 2. This worked a month ago.

Code: Select all

import smtplib
print('line 1')
server=smtplib.SMTP('smtp.gmail.com',587)
print('line 2')
server.starttls()
server.login('email@gmail.com', 'Password')
msg='test'
server.sendmail('email@gmail.com, 'recipient email',msg)
server.quit()

User avatar
neilgl
Posts: 2185
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: email via gmail not working

Thu Oct 31, 2019 1:09 am

They have made the login more secure

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Thu Oct 31, 2019 1:20 am

I take it if this has happened within the last month no one has a work around yet?

User avatar
neilgl
Posts: 2185
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: email via gmail not working

Thu Oct 31, 2019 8:17 am

Look at my working code in the thread here
https://www.raspberrypi.org/forums/vie ... 9#p1559129

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: email via gmail not working

Thu Oct 31, 2019 10:35 am

The original code still works for me , just tested it .

do you have " allow less secure apps " enabled ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Thu Oct 31, 2019 11:37 am

The email I am using for this project is not an email I use on a regular basis. I just went into it and noticed there was a security alert back on Oct 4. Hopefully I have that worked out now. I will try my code when I get home tonight. Now I have to figure out why the gmail app on my phone is not sending me notifications so I can catch this kind of thing earlier.

Neil. I tried your SSL, 465 back when I was writing this code and I could not get that to work. I ended up using port 587.

User avatar
neilgl
Posts: 2185
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: email via gmail not working

Thu Oct 31, 2019 1:13 pm

My code is working for me, so must be some other bug/difference. What error does using 465 give you?

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Thu Oct 31, 2019 4:40 pm

I don't remember exactly what errors I was getting. I think it was just hanging at that line like it is now. I had my trial code with both your line and the 587 line and would comment out one or the other. I was just happy to get something working. I had also tried using push notifications with Pushetta and Pushbullet. I could get those to work but the notification was unreliable. I could also make this work with my home email service through my ISP. I didn't want a hard coded password with my home email which is why I used gmail. If gmail keeps trying to protect me from myself I may try another email account with my ISP or a mail.com email.

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Thu Oct 31, 2019 10:37 pm

Well I tired several other email services. gmail using port 587 or 485 and hotmail using 587 just hang at the server=smtplib.SMTP line. Mail.com lets me setup the server but then pukes at the login. It seems the only one that reliably works for me is my roadrunner amount through my ISP.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: email via gmail not working

Thu Oct 31, 2019 11:07 pm

When I set up several gmail accounts for my pi's to use I used only the 0-9, A-Z & a-z for both the user name and passwords ,

you will also have to enable " allow less secure apps " on the email account in question before you can use it with smtplib.

if you follow these simple suggestions you should find that you can send emails ok using this code

Code: Select all

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

email_user = "account@gmail.com"
email_password = "password"
email_send = "address to send to"

subject = "Test email from pi"

msg = MIMEMultipart()
msg["From"] = email_user
msg["To"] = email_send
msg["Subject"] = subject

body = "Hi there, sending this email from Python!"
msg.attach(MIMEText(body,"plain"))

text = msg.as_string()
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(email_user,email_password)


server.sendmail(email_user,email_send,text)
server.quit()

Some times, but not always, you have to log in to the gmail account via a browser, after you first attempt to send an email from the pi and allow the pi as your new device to send mail.

both my accounts still work using this code
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Fri Nov 01, 2019 12:46 am

I had 2-factor authentication turned on so I had an app specific password. This worked on Oct 1st. I turned off 2-factor so I could turn on less secure apps. This still does not get past the set up of the SMTP server. If it was an account issue shouldn't the script be hanging up at the server.login line not the server=smtplib... line? Mail.com let me do the server setup but then told me my credentials are invalid at the server.login line.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: email via gmail not working

Fri Nov 01, 2019 9:32 am

Try creating a new account using only the suggested characters as is my other post and see if you can get that to work.

if you other attempt using Mail.com said you had provided invalid credentials are you sure you have entered the correct information ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Fri Nov 01, 2019 12:21 pm

My gmail username and password are only letters and numbers, no special characters. Here is an example of my code

Code: Select all

import smtplib
print ('1')
server = smtplib.SMTP("smtp.gmail.com",587)
print ('2')
server.starttls()
print ('3')
server.login(email_user,email_password)
What is confusing me is the output I get is a "1" then it just hangs no other output. Doesn't that "server =" line just define the variable "server" to smtplib.SMTP("smtp.gmail.com",587)? When I use mail.com I get a "1", "2", "3" then several errors with the final saying I had bad credentials. I would think if there was a problem with my gmail account using less secure apps or not I should get similar output like mail.com but with maybe different error codes.

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: email via gmail not working

Sat Nov 02, 2019 12:20 am

Well using your code with just the credentials added for one of my pi email accounts ,
my pi will log on the to account and then quit successfully.

Code: Select all

import smtplib

email_user = "xxx@gmail.com"
email_password = "abcdef"

print ('1')
server = smtplib.SMTP("smtp.gmail.com",587)
print ('2')
server.starttls()
print ('3')
server.login(email_user,email_password)
print("quit")
server.quit()
running the code produces

pi@BusterPi:~ $ python3 sendt.py
1
2
3
quit
pi@BusterPi:~ $


So ether there is a problem with the account you are trying to use or there is a problem with your SD card/OS.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Sat Nov 02, 2019 3:54 pm

So I have done more testing. If I put a print(server) after server=smtplib... I get <smtplib.SMTP object at 0xb6294430>. I am assuming that is some memory location since it changes each time I run the script. I get this output only with smtp-server.rochester.rr.com, 587 or smtp.mail.com, 587. If I use smtp.gmail.com, 587 or 485, smtp.live.com, 587 or smtp-mail.outlook.com, 587 the script will hang at the server=... line.

Now for the real weird stuff. If I use the rochester.rr.com server I can login and send email from a rochester.rr.com, hotmail.com and gmail.com email account. The script will complete with a mail.com account but no text is received.

If I use the mail.com server it doesn't matter what email account I try to log into I always get an authentication error. Also tithe rochester.rr.com server the server.starttls() is not supported.

Why some smtp servers will work and why some others won't, I don't understand. It also seems very strange that one server can log into and send from different email domains.

Now I just need to figure out why I am not getting anything in the message part of the text.

Code: Select all

msg='Pi test'
print('message is ' + msg)
server.sendmail('user.gmail.com', 'phonenumber@txt.att.net',msg)
In the shell I get "message is Pi test" but message field in the text is blank.

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: email via gmail not working

Sat Nov 02, 2019 4:34 pm

Fixed the body of the message. I used the email.mime library that pcmanhbob showed and that worked.

paulapalmer
Posts: 1
Joined: Wed Oct 09, 2019 9:49 am
Contact: Website

Re: email via gmail not working

Wed Nov 13, 2019 12:59 pm

Hi,
It would have been good if you have mentioned your query in brief as people faces many issues in regards to email issue has different steps & procedure.
The conclusion can be more accurate when you would have mentioned your mail account you are using

So let's just take one topic in consideration for you
Your topic i'am mentioning here is Frontier Email IMAP, SMTP & POP3 Server Settings-

Follow the following steps mentioned below-

Incoming Mail Server
The server ought to reflect imap.mail.yahoo.com

The port worth ought to be 993

In 'required SSL' type yes

For ' Outgoing Mail Server':-

smtp.mail.yahoo.com

The port worth ought to be 465 or 587

'Required SSL' ought to reflect Yes

Required Authentication ought to likewise reflect Yes

Link- https://emailsfix.com/frontier-email/fr ... -settings/

These steps mentioned are update if anyone wants to get a brief details of it can refer to the website i have been through!!!

Thanks
Iam a technology & software lover. Love to solve problem related to software issues.
Browse each and every forum in search of question in relation to emails, printers, router, computer/laptops etc.

Link- https://emailsfix.com/

Return to “Beginners”