Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emailing smtp with Python error

Tags:

python

email

smtp

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
like image 266
jakecar Avatar asked May 07 '26 22:05

jakecar


1 Answers

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.

like image 111
Greg Hewgill Avatar answered May 09 '26 11:05

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!