undisp
undisp

Reputation: 721

Python doesn't send email: smtplib.SMTPException: STARTTLS extension not supported by server

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('[email protected]','password')
mail.sendmail('[email protected]','[email protected]',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?

Upvotes: 0

Views: 6812

Answers (2)

MarkCr
MarkCr

Reputation: 21

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')

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168616

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('[email protected]','[email protected]',content)
mail.close()

Upvotes: 0

Related Questions