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.
Related
I'm trying to send an email using Python and I used the material I found on the official site (https://docs.python.org/3/library/email.examples.html) but I'm getting an error when sending an email. Using the port and server works when I use Java but when using the same details in Python the code doesn't work.
The code that I am testing:
import smtplib
from email.message import EmailMessage
port = 25
server = "172.xxx.xxx.xxx"
from_add = "test#fakemail.com"
to_add = "test#fakemail.com"
message = "This is a test Email"
subject = "Test Email"
msg = EmailMessage()
msg.set_content(message)
msg["Subject"] = subject
msg["From"] = from_add
msg["To"] = to_add
#Send the message via our own SMTP server
s = smtplib.SMTP(server)
s.send_message(msg)
s.quit()
When I run my code I get the below error. I have been searching the net and all I am finding are errors concerning sockets (I tried adding which I haven't seen in any examples. My firewall admin tells me he doesn't see any traffic being blocked his end. Does anyone know if what I am coding is wrong?
Original
Traceback (most recent call last):
File "X:/test/test_email.py", line 21, in <module>
s = smtplib.SMTP(server)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 728, in create_connection
raise err
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 711, in create_connection
sock = socket(af, socktype, proto)
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 151, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [WinError 10022] An invalid argument was supplied
Process finished with exit code 1
Socket change (didn't work)
s.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sock.bind(server, port)
I found the problem. I am using Anaconda and had created a different environment. When I switched to the root environment my code worked without a problem.
I am able to send from one gmail account to another gmail account and from one outlook account to another outlook account.
But I am facing problem when I am trying to send mail from outlook to gmail.
Here is the code
import smtplib
content ='example email stuff here'
email='xyz#outlook.com'
password="password"
fromemail="xyz#outlook.com"
receive="abc#gmail.com"
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
mail.ehlo()
mail.starttls()
mail.login(email,password)
mail.sendmail(fromemail,receive,content)
mail.close()
When I am running the script I am getting the following error
Traceback (most recent call last):
File "smtp_google.py", line 7, in <module>
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
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 575, in create_connection
raise err
socket.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
Solution 1:
Try by giving timeout in:
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
as
mail=smtplib.SMTP('smtp-mail.outlook.com',587, timeout=120)
or try connecting via SSL instead of TLS/STARTTLS
mail=smtplib.SMTP_SSL('smtp-mail.outlook.com',587)
solution 2:
Your email account must allow smtp, which is not necessarily enabled by default. You can try to change the settings by following the steps given here enable smtp authentication
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 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']