0
#!/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:
4
  • 1
    Your backtrace looks weird and incomplete -- is that the whole thing? Commented Nov 1, 2011 at 1:27
  • @Dougal I erased the rest for privacy reasons. ralu No, I'm on my university's Linux server Commented Nov 1, 2011 at 1:29
  • 1
    @user1022944 It's a little hard to tell what the problem is without seeing more of the error. Can you just replace any server names or whatnot with example.com and post that? Commented Nov 1, 2011 at 1:31
  • then replace localhost whit working smtp server. Commented Nov 1, 2011 at 1:32

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.