I am trying to send mail using python smtplib module, but I got error.
import smtplib
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login("xxxxxxxx#gmail.com", "yyyyyyy")
message = "Message_you_need_to_send"
s.sendmail("xxxxxxxxx#gmail.com", "aaaaaaaaa#gmail.com", message)
s.quit()
I got error like below:
Traceback (most recent call last): File "/home/engineer/demo.py",
line 52, in
s.starttls() File "/usr/lib/python2.7/smtplib.py", line 637, in starttls
raise SMTPException("STARTTLS extension not supported by server.") SMTPException: STARTTLS extension not supported by server.
I refer the solution from another link. You may try to remove s.ehlo() before s.starttls().
I tested your code with my own gmail account, it seems the code should work with s.echlo(). You may like to check your gmail security setting eg enable Less secure apps, Let less secure apps use your account
You also enable debug by using s.set_debuglevel(1)
import smtplib
s = smtplib.SMTP('smtp.gmail.com', 587)
s.set_debuglevel(1)
s.ehlo()
s.starttls()
No changes as far as code is concerned, end up finding out, Issue was associated with enough firewall permissions.
Related
A program I wanted to create requires to login to a mail server.
I was able to login using smtplib on Gmail, but it didn't work on Yahoo mail.
SMTP Port 465
Server mail: smtp.mail.yahoo.co.jp
python code on IDLE
import smtplib
smtp_obg = smtplib.SMTP("smtp.mail.yahoo.co.jp", 465)
Error message
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
smtp_obg = smtplib.SMTP("smtp.mail.yahoo.co.jp", 465)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
(code, msg) = self.getreply()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
I would appreciate if someone could help. Thank you in advance.
You must run login after creating smtp_obj (smtp_obg.login('username', 'password')). If you trying to login, please mention it.
You must activate SMTP feature at Yahoo's web page (Same as Gmail). Sorry for Japanese, but not sure if yahoo.co.jp is using somewhere outside Japan. :)
In English, this parameter should be called like: IMAP/POP/SMTP access and mail transfer
I checked my account. STMP access is disabled by default.
When you create a connection to your smtp server you need to know more about the server about what way you want to connect to it.
There are normal with and without SSL connection based on port number to your SMTP server.
I use this site https://settings.email/yahoo-co-jp-email-settings/ to get more info about your SMTP server you want to use from yahoo and it says the port 465 is an SSL connection so you need to connect with SSL connection else you can't sending out this way.
in smtplib in python you have smtplib.SMTP and smtplib.SMTP_SSL functions to work with, the first it's used when it's not SSL requirement, and the second its used when you need SSL like yahoo smtp.
Can you try out to use smtplib.SMTP_SSL function and see about its working for you?
import smtplib
smtp_obg = smtplib.SMTP_SSL("smtp.mail.yahoo.co.jp", 465)
Mabey its required a username and password to sending out, I did not have so much info about yahoo SMTP to can explain that part for you.
So I've created a script which sends an update e-mail when the script is finished running. It all works fine locally. But when deployed and ran on a Linode server it doesn't work anymore. Here is the code:
def email(products, new_products, updated_products):
#print(products, new_products, updated_products)
message = EmailMessage()
message.set_content("Products scraped: {}, New products: {}, Updated products: {}".format(products, new_products, updated_products))
message['FROM'] = "e-mail"
message['TO'] = ["e-mail"]
message['SUBJECT'] = "Update"
context = ssl.create_default_context()
#set up SMTP server
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.starttls(context=context)
smtp.login(message['FROM'], "password")
smtp.send_message(message)
smtp.quit()
The error occurs on the line: with smtplib.SMTP('smtp.gmail.com', 587) as smtp. I've tried different ports, but it all results in this error: TimeoutError: [Errno 110] Connection timed out. Has anyone had this problem? Or does anyone have another good way to send an e-mail from a server? Thanks in advance!
Linode block outgoing mail by default to prevent spam.
https://www.linode.com/community/questions/19082/i-just-created-my-first-linode-and-i-cant-send-emails-why-mailing-ports-25-465-a
Try enabling less secure apps for the email id you want to send emails from
I am trying to use Office365 smtp server for automatically sending out emails. My code works previously with gmail server, but not the Office365 server in Python using smtplib.
My code:
import smtplib
server_365 = smtplib.SMTP('smtp.office365.com', '587')
server_365.ehlo()
server_365.starttls()
The response for the ehlo() is: (501, '5.5.4 Invalid domain name [DM5PR13CA0034.namprd13.prod.outlook.com]')
In addition, .starttls() raises a SMTPException: STARTTLS extension not supported by server
Any idea why this happens?
The smtplib ehlo function automatically adds the senders host name to the EHLO command, but Office365 requires that the domain be all lowercase, so when youe default host name is uppercase it errors.
You can fix by explicitly setting sender host name in the ehlo command to anything lowercase.
import smtplib
server_365 = smtplib.SMTP('smtp.office365.com', '587')
server_365.ehlo('mylowercasehost')
server_365.starttls()
I had to put SMTP EHLO before and after starttls().
Thanks!
import smtplib
server_365 = smtplib.SMTP('smtp.office365.com', '587')
server_365.ehlo('mylowercasehost')
server_365.starttls()
server_365.ehlo('mylowercasehost')
I am not at all familiar with SMTP but I am working on sending emails through Python code. I have the code but I need to pass SMTP host name for it to actually work. Is there any service which provides a free SMTP service that I leverage for testing out my code? I looked around to create my own SMTP server but couldn't find something that provides a step by step guide to create a SMTP server. I want to create a free server(or if there is any free service) that will provide me with a host name(ip address) so that I can put that host name in my python code and execute it from any machine.
If anyone can point me in the right direction it will be helpful.
import smtplib
username = 'user'
password = 'pwd'
from_addr = 'username#gmail.com'
to_addrs = 'username#gmail.com'
msg = "\r\n".join([
"From: username#gmail.com",
"To: username#gmail.com",
"Subject: subject",
"",
"message"
])
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_addr, to_addrs, msg)
server.quit()
You can use mutt linux command also here.
See :
https://docs.python.org/3/library/smtplib.html
https://support.google.com/a/answer/176600?hl=en
You need service like https://mailtrap.io/. You'll get SMTP server address (eventually port number) that you point your application to. All e-mails produced by your application will be then intercepted by mailtrap (thus not delivered to the real To: address).
They offer free variant that seems to be suitable for your needs.
So I have tried at least 5-10 examples online to send an email, but I keep getting the same error.
Traceback (most recent call last):
File "C:/Users/alxu/Project/LogFilter.py", line 88, in ?
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 292, in connect
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
socket.gaierror: (11004, 'getaddrinfo failed')
The last example I used code was like this...
import smtplib
to = 'mkyong2002#yahoo.com'
gmail_user = 'mkyong2002#gmail.com'
gmail_pwd = 'yourpassword'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()
I am using Python 2.4.3.
EDIT: McAfee is has host intrusion prevention so all the times I attempted to do this it blocked it.
Since you are behind a corporate proxy, you can't directly connect to any outside server, you need to connect through the proxy. In this case, you're trying to connect to a SMTP server, which uses port 25 or 587. I assume you're behind a HTTP proxy that doesn't block SMTP ports. If your corporate proxy blocks these ports, then there's nothing you can do to connect to the server.
The smtplib module doesn't include the functionality to connect to a SMTP server through a proxy. As a workaround, I wrote the custom class ProxySMTP, which I have posted here along with instructions on how to use it.
Let me know if it works for you.
Your last example code should work. One thing you need to do is to make sure you allow access to less secure apps by going to the following link.
google settings for secure apps
The reason for this is google flags such automated email scripts as less secure. You should also be aware of the risks. Make sure you change this setting back if you are not gonna need it.