0

I am trying to send email via Python but I am having an error.

This is my code:

import smtplib

content = 'example bla bla'
mail = smtplib.SMTP('localhost')
mail.ehlo()
mail.starttls()
mail.login('from@example.com','password')
mail.sendmail('from@example.com','to@example.com',content)
mail.close()

When I run the file, this error shows up:

Traceback (most recent call last):
  File "SendEmail.py", line 11, in <module>
    mail.starttls()
  File "/usr/lib/python2.7/smtplib.py", line 637, in starttls
    raise SMTPException("STARTTLS extension not supported by server.")
smtplib.SMTPException: STARTTLS extension not supported by server.

There are posts about other people with the same problem but usually is just the order of the commands that is wrong (like this one) and apparently mine is correct.

If I change mail = smtplib.SMTP('localhost') to mail = smtplib.SMTP('smtp.gmail.com', 587) it works well. That makes me think that the problem may be in the configuration of the "localhost" but if I open http://localhost in the browser the page "It works" is presented so I think the localhost is well configured.

Does anyone know where can be the problem?

undisp
  • 711
  • 3
  • 11
  • 34
  • Opening `http://localhost` doesn't mean much in the context of email, since it uses `http` whereas email uses `smtp`. So, no, the local host is probably not properly configured, and the issue is that `STARTTLS` is not supported. – Thomas Orozco May 13 '15 at 16:30
  • Well, thanks for the answer. I will try to configure smtp. – undisp May 13 '15 at 16:34

2 Answers2

2

Try commenting out mail.starttls(). i had this problem and it works in my code.

import smtplib
import string
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

smtpObj = smtplib.SMTP('mailrelay.address.com', 587) #email relay address
smtpObj.ehlo()
#smtpObj.starttls() #commented this out as was causing issues
smtpObj.login('domain\username', 'pwrd123')
Manjunath Ballur
  • 6,287
  • 3
  • 37
  • 48
MarkCr
  • 21
  • 2
0

You probably don't need to login to the SMTP server running on 'localhost' ('localhost' is conventionally the same computer your program is running on.)

Try this:

import smtplib

content = 'example bla bla'
mail = smtplib.SMTP('localhost')
mail.ehlo()
mail.sendmail('from@example.com','to@example.com',content)
mail.close()
Robᵩ
  • 163,533
  • 20
  • 239
  • 308