I try to send mail via python. I got a function:
def mail():
fromaddr = 'mymail#gmail.com'
toaddrs = 'targetmail#o2.pl'
msg = 'There was a terrible error that occured and I wanted you to know!'
# Credentials (if needed)
username = 'mymail#gmail.com'
password = 'password'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
When I try to execute this function it whows me WinError 10060. Error text below:
Traceback (most recent call last):
File "C:\python-factory\ServiceChecker\main\mainBlock.py", line 75, in <module> mail()
File "C:\python-factory\ServiceChecker\main\mainBlock.py", line 65, in mail server = smtplib.SMTP('smtp.gmail.com:587')
File "C:\Python34\Lib\smtplib.py", line 242, in __init__ (code, msg) = self.connect(host, port)
File "C:\Python34\Lib\smtplib.py", line 321, in connect self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\Lib\smtplib.py", line 292, in _get_socket self.source_address)
File "C:\Python34\Lib\socket.py", line 509, in create_connection raise err
File "C:\Python34\Lib\socket.py", line 500, in create_connection sock.connect(sa)
TimeoutError: [WinError 10060]
I don't know where is problem... i tried many mail servers (gmail,o2,wp,) all of them returned the same error. I tried also other available solutions in the internet to send mail via pyython.... the same problem...
Please help
According to your posted backtrace, you are failing at the line
server = smtplib.SMTP('smtp.gmail.com:587')
I've run that line and it works for me. The clue is in the TimeoutError. I believe you are having networking problems and the code itself is fine.
You do have an extra call to ehlo but as it stands you are not getting that far.
Additionally, it would probably be better to pass the port number as a separate argument, rather than using a colon to split it. E.g.
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
The documentation for smptlib is here: https://docs.python.org/2/library/smtplib.html
Try removing the second server.ehlo() . The one just before login. That worked for me.
toaddrs should be a list
toaddrs = ['targetmail#o2.pl']
Related
pwd = "mypassword"
import smtplib
server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("gvpcse113#gmail.com",pwd)
msg = "YOUR MESSAGE!"
server.sendmail("gvpcse113#gmail.com", "sender#xyz.com", msg)
server.quit()
I have tried sending a mail through python...
Error :
Traceback (most recent call last):
File "H:/my projects/PythonCourse/test_cont/mail_test4.py", line 4, in <module>
server = smtplib.SMTP('smtp.gmail.com')
File "C:\Python27\lib\smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
I am connecting through a proxy connection
I set the proxy through cmd in windows.
Please help me with this.
Update :
I am sure with the internet connection :
import urllib2
def internet_on():
try:
response=urllib2.urlopen('https://www.google.co.in',timeout=1)
return True
except urllib2.URLError as err: pass
return False
print internet_on()
Output is True
Your code works fine for me, so it's probably the connection settings.
Try changing server to:
server = smtplib.SMTP('64.233.184.108')
(that's the IP address of smtp.gmail.com, to bypass DNS resolution)
Try updating the server = smtplib.SMTP('smtp.gmail.com') to include a port. The port for gmail is 587, so add that as a second parameter: server = smtplib.SMTP('smtp.gmail.com', 587)
If this does not work, make sure that you have used this site to make sure that the account will let you in:
https://www.google.com/settings/security/lesssecureapps
I am trying to use SMTP in Python 3.4 to send emails but I am receiving the following error when I run my code:
Traceback (most recent call last):
File "C:/Users/Anna Hughes/Documents/Python Projects/2015.10.16/Mail Application.py", line 9, in <module>
server.login(user_email, user_password)
File "C:\Python34\lib\smtplib.py", line 613, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
Here is my code:
import smtplib
server = smtplib.SMTP("smtp.gmail.com:587")
user_email = "user#gmail.com"
user_password = "password"
recipient_email = "recipient#gmail.com"
msg = "Test."
server.login(user_email, user_password)
server.ehlo()
server.starttls()
server.sendmail(user_email, recipient_email, msg)
server.quit()
Thanks.
EDIT:
I have changed the second line to server = smtplib.SMTP("smpt.gmail.com, 587") as suggested. That seemed to fix the error, but I am now getting a new one:
Traceback (most recent call last):
File "C:/Users/Anna Hughes/Documents/Python Projects/2015.10.16/Mail Application.py", line 2, in <module>
server = smtplib.SMTP("smtp.gmail.com,587")
File "C:\Python34\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python34\lib\smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\lib\smtplib.py", line 292, in _get_socket
self.source_address)
File "C:\Python34\lib\socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
You can also try yagmail:
import yagmail
yag = yagmail.SMTP("user#gmail.com", "password")
yag.send("recipient#gmail.com", subject="sub", contents="Test.")
Install using pip install yagmail.
Lots of more tricks can be found on the github page, such as passwordless script.
I can help.
See here and turn on.
Then
server = smtplib.SMTP("smtp.gmail.com, 587")
server.starttls()
server.login("username","p455w0rd")
I think perhaps your problem lies with this: server = smtplib.SMTP("smtp.gmail.com:587")
I've got a few programs that send e-mails, and I use the following format instead:
server = smtplib.SMTP("smtp.gmail.com, 587")
If you can't see the difference at first, the colon has been changed to a comma.
Hope this fixes it!
Try this
import smtplib
Server=smtplib.SMTP('smtp.gmail.com',587)
Server.starttls()
Server.login("EMAIL_YOUR","YOUR_PASSWORD")
msg="Hello, Its the Message!"
Server.sendmail("whom_2_send#gmail.com","your_email#gmail.com",msg)
print("email send succesfully") #Just for confirmation
Server.quit()
try this:
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(email, password, message)
server.quit()
I am trying to send an email form my server with python, I asked my server provider what port to use for this and they said "choose SMTP Ports 465 (Secure SSL/TLS outgoing server) , 25 ( Non-SSL outgoing server)." Not sure what this exactly means but currently I am using 25, here is my code
#! /usr/bin/python
import smtplib
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 25)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
I filled in my username (which is my email address right) and password,a dn the target but it is not working, when I try to navigate to the url where my py script is, it just doesn't load. I have an internet connection cause I am loving other things, and go to other pages on my server. I have also tried running with cron jobs but that also doesn't work.
The permissions on the script are 0755, is there a problem with the script?
When i ran with cron jobs here is the traceback
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 6, in <module>
server = smtplib.SMTP('smtp.gmail.com', 25)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
Thanks for the help in advance.
EDIT
here is updated error log with the port at 587
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 7, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
EDIT 2
When I had server = smtplib.SMTP('telnet smtp.gmail.com', 587)
I got this error
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 8, in <module>
server = smtplib.SMTP('telnet smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
BUG:
First of all you are using gmail server not your company server to send mail .For gmail server the output port is 587
The code:
Due to security issues gmail blocks accessing mail via code or program
But still you can use gmail to send mail via code if you do the following things
What i have done in code :
1.Added a error object to get the error message
import smtplib
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
If u ran this code u would see a error message like this stating that google is not allowing u to login via code
Things to change in gmail:
1.Login to gmail
2.Go to this link https://www.google.com/settings/security/lesssecureapps
3.Click enable then retry the code
Hopes it help :)
But there are security threats if u enable it
Updated
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login("ABC#gmail.com", "password")
mail.sendmail("ABC#gmail.com", "recivermailaddress", content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
In python, please download yagmail (disclaimer: I'm the developer):
pip install yagmail
It is then simply a matter of:
import yagmail
yag = yagmail.SMTP("you#gmail.com", "password")
yag.send("target#example.com", 'This is the subject', 'Hello!')
I guess it mostly just has to do with the fact that you gave a wrong port (smtp.gmail.com is not at 25).
On the github you can also see a common list of errors.
Can someone give me some insight as to why both this:
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
and this:
mailServer = smtplib.SMTP('smtp.gmail.com:587')
Are saying this:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\socket.py", line 571, in create_connection
raise err
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I'm in China.
I have a VPN. I tried it with and without the VPN.
I have, many times before, used this code to send e-mails from my g-mail account. Suddenly today, I got this error.
Gmail, if accessed through my chrome browser, works fine. Albeit, it's taking an unusually long time to send simple e-mails...hmmm
Try specifying a timeout (requires Python 2.6+):
smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
or try connecting via SSL instead of TLS/STARTTLS (with and without timeout):
smtplib.SMTP_SSL("smtp.gmail.com", 465)
I had a similar problem. The call to mailServer = smtplib.SMTP("smtp.gmail.com", 587) just hang there, without doing anything. I can't even quit the program. It stays in the background forever!
But adding the timeout parameter solved the problem! It returns immediately and I can sent email through gmail SMTP server successfully!
Not sure why though!
Look my friend, see the line
mailServer = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
and put YOUR_USERNAME after the 587 number, like this
mailServer = smtplib.SMTP("smtp.gmail.com", 587, "YOUR_USERNAME", timeout=120)
I can't figure out why this isn't working. I'm trying to send an email from my school email address with this code I got online. The same code works for sending from my GMail address. Does anyone know what this error means? The error occurs after waiting for about one and a half minutes.
import smtplib
FROMADDR = "FROM_EMAIL"
LOGIN = "USERNAME"
PASSWORD = "PASSWORD"
TOADDRS = ["TO_EMAIL"]
SUBJECT = "Test"
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
% (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"
server = smtplib.SMTP('OUTGOING_SMTP', 465)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
And here's the error I get:
Traceback (most recent call last):
File "emailer.py", line 13, in
server = smtplib.SMTP('OUTGOING_SMTP', 465)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in init
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 514, in create_connection
raise error, msg
socket.error: [Errno 60] Operation timed out
It's likely that your school's SMTP server does not permit outside access to port 587. Gmail does, and requires authentication to ensure that you are who you say you are (and so that spammers can't send email appearing to be from you unless they know your password). Your school may have chosen to set up their mail server so that only connections from within the school can send mail in this way.