Unable to send mail using smtplib in python - python

I am running the below code in Ubuntu OS. When I run this code in stand alone machine, I am not getting any error.
But when I connect the remote machine and run the same code, I am getting the following error message.
I have also modified the port value and try multiple times but the issue is not fixing. I am wondering where i need to check to resolve the issue.
import smtplib
message = ' '
sender = ' '
receivers = ' '
SUBJECT = ' '
TEXT = ' '
sender = 'sender1#abc.com'
receivers = ['rec1#abc.com','rec2#abc.com']
# prepare message
SUBJECT = "Test mail "
TEXT = """Message line1 \n
Message line2 \n """
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
try:
smtpObj = smtplib.SMTP(<Value>)
smtpObj.sendmail(sender, receivers, message)
smtpObj.close()
except smtplib.SMTPConnectError:
print "Error: unable to send email"
except smtplib.SMTPException:
print "Error: unable to send email"
Error message:
Traceback (most recent call last):
File "sampa.py", line 23, in <module>
smtpObj = smtplib.SMTP(<Value>)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused

try:
smtpObj = smtplib.SMTP(smtp_server, port)
smtpObj.ehlo() # Can be omitted
smtpObj.starttls(context=context) # Secure the connection
smtpObj.ehlo() # Can be omitted
smtpObj.login(sender, password)
smtpObj.sendmail(sender, receivers, email_text)

Related

Sending an email using smtplib via python

I am using mailtrap as the smtp server to send emails. However, when I send an email, I receive an error. The log in credentials are correct and I changed the emails for privacy concerns. Note, I am running this on a pycom device directly connected to my laptop.
def sendEmail():
sender = "Private Person <from#mail.com>"
receiver = "A Test User <to#mail.com>"
port = 2525
smtp_server = "smtp.mailtrap.io"
login = "username" # paste your login generated by Mailtrap
password = "password" # paste your password generated by Mailtrap
message = """
Subject: Hi Mailtrap
To: {receiver}
From: {sender}
This is a test e-mail message."""
try:
with smtplib.SMTP(smtp_server, port) as server:
server.login(login, password)
server.sendmail(sender, receiver, message)
print('Sent')
except smtplib.SMTPServerDisconnected:
print('Failed to connect to the server. Wrong user/password?')
except smtplib.SMTPException as e:
print('SMTP error occurred: ' + str(e))
Error Message:
Traceback (most recent call last):
File "<stdin>", line 147, in <module>
File "<stdin>", line 114, in sendEmail
File "<stdin>", line 106, in sendEmail
File "/flash/lib/smtplib.py", line 84, in __init__
File "/flash/lib/smtplib.py", line 110, in connect
AttributeError: 'module' object has no attribute 'IPPROTO_SEC'
>
Line 106 refers to server.login(login, password) and line 114 is the line right after print('SMTP error occurred: ' + str(e))
I have taken the snippet from the smptlib which is causing problems
def connect(self, host, port=0, source_address=None):
if source_address:
self.source_address = source_address
if not port and (host.find(':') == host.rfind(':')):
i = host.rfind(':')
if i >= 0:
host, port = host[:i], host[i + 1:]
try:
port = int(port)
except ValueError:
raise OSError("non numeric port")
if not port:
port = self.default_port
if self.debuglevel > 0:
print('connect:', (host, port), file=stderr)
if self.tls:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
self.sock = ssl.wrap_socket(ss)
else:
self.sock = socket.socket()
self.sock.settimeout(self.timeout)
try:
self.sock.connect(socket.getaddrinfo(host, port)[0][4])
except:
self.close()
(code, msg) = self.getreply()
if self.debuglevel > 0:
print("connect:", msg, file=stderr)
return (code, msg)
Specifically ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
Any help would be greatly appreciated, thanks :)

Python Email Module Does Not Work [duplicate]

I am using this code taken from here:
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
I put my gmail mail account in the sender and in the reciver but I get this error:
Traceback (most recent call last):
File "C:/Python27/smtpExample.py", line 24, in <module>
server = smtplib.SMTP('localhost')
File "C:\Python27\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
error: [Errno 10061] No connection could be made because the target machine actively refused it
You're not running SMTP server on your machine.
see: No connection could be made because the target machine actively refused it

Send email from server python

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.

Python smtlib raising error when trying to send e-mail

I copied this code right from the smtplib docs over here
import smtplib
def prompt(prompt):
return input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while True:
try:
line = input()
except EOFError:
break
if not line:
break
msg = msg + line
print("Message length is", len(msg))
server = smtplib.SMTP('192.168.2.4')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
However this error is raised:
Traceback (most recent call last):
File "C:\Python34\geenemail.py", line 24, in <module>
server = smtplib.SMTP('192.168.2.4')
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)
ConnectionRefusedError: [WinError 10061] Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
I looked for similar problems but couldn't find a lot, only found someone who said the firewall might be the problem --> I turned Avast/firewall off, got a bluescreen from some reason, restarted my PC, Avast/firewall was still off but still it raised the same error.
I have also tried giving a port value but I still get the same error. What might be the problem?
The most likely reason for this error is that there is no SMTP server running on the computer you are trying to connect to.
server = smtplib.SMTP('192.168.2.4')
'192.168.2.4' should be the address of the SMTP server you are trying to use to send the email.

Emailing smtp with Python error

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.

Categories

Resources