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

SMTP mail problem

Sat Sep 14, 2019 11:38 pm

I am trying to send and email from a python script and I am getting some errors I don't understand. I am running Stretch so I can use SMTP. Any idea what is going one? Also why is there the smtp.conf file if you are entering the server information, email address and password in the code?
Here is my code:

Code: Select all

import smtplib, ssl
print('import')
server = smtplib.SMTP_SSL('smtp.gmail.com', 485)
#server = smtplib.SMTP('smtp.gmail.com', 587)
#server.ehlo()
#server.starttls()
print('line one')
server.login('myemail@gmail.com','password')
print('line two')
msg='Pi test'
print('mesage is ' + msg)
server.sendmail('myemailgmail.com','phonenumber@txt.att.net',msg)
server.quit()
Here are the errors and output I get.:
>>> %Run testemail.py
import
Traceback (most recent call last):
File "/home/pi/Documents/testemail.py", line 3, in <module>
server = smtplib.SMTP_SSL('smtp.gmail.com', 485)
File "/usr/lib/python3.5/smtplib.py", line 1021, in __init__
source_address)
File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.5/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.5/smtplib.py", line 1027, in _get_socket
self.source_address)
File "/usr/lib/python3.5/socket.py", line 712, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 703, in create_connection
sock.connect(sa)
OSError: [Errno 113] No route to host
>>>

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

Re: SMTP mail problem

Sun Sep 15, 2019 9:35 am

So first thing do you have a connection to the internet, can you browse web sites or do sudo apt update successfully ?

gmail smtp works on port 587, so you need to change that , you seem to have commented out the correct line for some reason, you also have server.starttls commented out for some reason so that need un-commenting as it required to log in to gmail.

you could try this basic set up to send mail , I use this to test any new pi's I set up to send email.

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 = "gamil account goes here"
email_password = "password goes here"
email_send = "sending to address goes here"

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()
print("mail sent")
this will work on stretch and buster.
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: SMTP mail problem

Sun Sep 15, 2019 1:40 pm

Thanks that worked. I had been having problems with this code for awhile and ad read just about every post there is on this subject. There is some discussion out there about port 587 vs 485 and google. I think I had another problem that was causing to not work and that was when I switched to port 485. After I inadvertently fixed the first problem I must had made a second one with the port change.

I was also wondering about using some other email server other than gmail. Our home email is through Time Warner now Spectrum and found I could use that server also with port 25. Since I have to hard code the password I think I will stick to a generic gmail account.

Return to “Beginners”