Sending an email using smtplib via python - 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 :)

Related

Unable to send mail using smtplib in 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)

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

ubuntu python socket error [errno 111]

I used python to analyze the online ad system. I try to connect to the server but failed, the template of the code I run in terminal is: python client.py username portnumber,
here is the code on server side:
import sys
import time
import socket
TIMEOUT = 30
class Firefly:
def __init__(self, port):
self._port = port
def _get_the_dataz(self, s):
ret = ""
s.settimeout(TIMEOUT)
while True:
data = s.recv(4096)
if not data:
break
ret = ret + data;
return ret
def _send_command(self, command, retries=3):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.connect(('127.0.0.1', self._port))
s.connect(('localhost', self._port))
s.send(command)
ret = self._get_the_dataz(s)
s.close()
return ret
except socket.error as ex:
print "Firefly timeout, %d retries left" % retries
print str(ex)
if retries == 0:
print "Can't reach firefly on port %d!" % self._port
raise
retries -= 1
time.sleep([30, 10, 2][retries])
return self._send_command(command, retries)
def get_visual_elements(self, url):
return self._send_command('GOTO %s' % url)
It always returns like this:
[errno 111] connection refused
cannot reach server on port 80
traceback(most recent call last):
file "server.py", line 27, in _send_command
s.connect(('localhost', self._port))
file "server.py", line 224, in meth
return getattr(self._sock,name)(*args)
then I try "telnet 127.0.0.1 80",
It returns:
unable to connect to the remote host, connection refused.
What should I do?

logging.handlers.SMTPHandler raises smtplib.SMTPAuthenticationError

I tried this with Verizon and Gmail. Both servers denied authentication. Gmail emailed me that it denied a login attempt because the connection was not using "modern security".
I would like to know how I can use modern security with this logging handler.
logging.handlers.SMTPHandler(mailhost=('', 25),
fromaddr='',
toaddrs='',
subject='',
credentials=('username','password'),
secure=())
For anyone coming back to this, here is how I got the SMTPHandler working with Gmail:
eh = SMTPHandler(mailhost=('smtp.gmail.com', 587),
fromaddr=from_addr,
toaddrs=to_addrs,
subject=subject,
credentials=(username, password),
secure=())
The to_addrs variable is a string in my example. I am not sure if it can be an array or is supposed to be a space or comma-delimited string. The username variable includes the domain, like this: foo#gmail.com.
Most guides said to use port 465, here is the difference if you are curious. However, when I tried to use port 465, I got a SMTP timeout error:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/smtplib.py", line 386, in getreply
line = self.file.readline(_MAXLINE + 1)
File "/usr/local/lib/python3.5/socket.py", line 571, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/logging/handlers.py", line 972, in emit
smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
File "/usr/local/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python3.5/smtplib.py", line 337, in connect
(code, msg) = self.getreply()
File "/usr/local/lib/python3.5/smtplib.py", line 390, in getreply
+ str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out
I switched to port 587 and Google successfully authenticated. The messages were sent.
Gmail problem:
Not addressed here. See other answers about Gmail's app authentication.
Verizon problem:
Some mail submission servers may only accept SMTPS on port 465. See What is the difference between ports 465 and 587? for elaboration. The Verizon mail submission server smtp.verizon.net, is one such example.
SMTPHandler does not support SMTPS by default. You can monkeypatch the functionality.
Solution:
This forces any server to use SMTPS.
A more thorough fix would be to edit the class with a flag to enable SMTPS.
Paste the edited emit function from below, into the relevant file.
Then set it
logging.handlers.SMTPHandler.emit = emit
The default /logging/handlers.py logging.handlers.SMTPHandler.emit function
# Class SMTPHandler...
def emit(self, record):
"""
Emit a record.
Format the record and send it to the specified addressees.
"""
try:
import smtplib
from email.utils import formatdate
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port, timeout=self._timeout)
msg = self.format(record)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
self.fromaddr,
",".join(self.toaddrs),
self.getSubject(record),
formatdate(), msg)
if self.username:
if self.secure is not None:
smtp.ehlo()
smtp.starttls(*self.secure)
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
The edited emit function
def emit(self, record):
"""
Overwrite the logging.handlers.SMTPHandler.emit function with SMTP_SSL.
Emit a record.
Format the record and send it to the specified addressees.
"""
try:
import smtplib
from email.utils import formatdate
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP_SSL(self.mailhost, port, timeout=self._timeout)
msg = self.format(record)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (self.fromaddr, ", ".join(self.toaddrs), self.getSubject(record), formatdate(), msg)
if self.username:
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)

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