Mark Kennedy
Mark Kennedy

Reputation: 1781

Python sendmail error script

#!/usr/bin/python

import smtplib

sender = '[email protected]'
receivers = ['[email protected]']

message = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

I keep getting the folllowing errors even though I imported everything. I'm using Linux, what's missing?

  File "email.py", line 3, in <module>
    import smtplib
  File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
    import email.utils
  File "/home/email.py", line 19, in <module>
    except SMTPException:

Upvotes: 0

Views: 7705

Answers (1)

Wil Cooley
Wil Cooley

Reputation: 940

The only obvious thing that should not work is that SMTPException needs to be smtplib.SMTPException (or import it for unqualified use with from smtplib import SMTPException).

Otherwise, after changing to my own (valid) addresses and my own SMTP server, your code works fine.

Upvotes: 7

Related Questions