5

I'm trying to make an email script in python. Here's what I have (from pythonlibrary.org):

#! /usr/bin/env python

import smtplib
import string

SUBJECT = "An email!"
TO = "[email protected]"
FROM = "[email protected]"
text = "This text is the contents of an email!"
BODY = string.join((
        "From: %s" % FROM,
        "To: %s" % TO,
        "Subject: %s" % SUBJECT ,
        "",
        text
        ), "\r\n")
server = smtplib.SMTP('smtp.gmail.com')
server.login('[email protected]', 'mypassword') # Not very secure, I know, but this email is dedicated to this script
server.sendmail(FROM, [TO], BODY)
server.quit()

I get smtplib.SMTPException: SMTP AUTH extension not supported by server. Is this is so, then why does smtp.gmail.com respond at all? Is this a problem with Gmail, or my script, or something else?

Error message:

Traceback (most recent call last):
  File "/Users/student/Desktop/mail.py", line 18, in <module>
    server.login('*******@gmail.com', '**************')
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 552, in login
smtplib.SMTPException: SMTP AUTH extension not supported by server.

3 Answers 3

7

You need to contact the gmail mail server on the submission port (587), not the default 25:

server = smtplib.SMTP('smtp.gmail.com', 587)

You also need to use server.starttls() before logging in (so that your password is not sent in the clear!). This is from a script I have and it works for me:

server = smtplib.SMTP()
server.connect("smtp.gmail.com", "submission")
server.starttls()
server.ehlo()
server.login(user, password)
Sign up to request clarification or add additional context in comments.

5 Comments

That's not working, either. Same error, line number, etc. I'll but the full (censored) error in the question.
very nice, worked like a charm! Just to be clear, with server.starttls(), this is relatively secure as long as nobody gets ahold of my .py file?
Yes, that is essentially equivalent to using SSL.
Ok. Also, just one more thing, is there a way to make it appear sent from a name ("John Doe", not "[email protected]"), as gmail.com does in it's mail client?
Sure, just use something like FROM = "John Doe <[email protected]>"
3

Here's how to send e-mail using gmail in Python:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from email.header    import Header
from email.mime.text import MIMEText
from getpass         import getpass
from smtplib         import SMTP_SSL

login, password = '[email protected]', getpass('Gmail password:')

# create message
msg = MIMEText('message body…', _charset='utf-8')
msg['Subject'] = Header('subject…', 'utf-8')
msg['From'] = login
msg['To'] = login

# send it via gmail
s = SMTP_SSL('smtp.gmail.com', 465, timeout=10)
s.set_debuglevel(1)
try:
    s.login(login, password)
    s.sendmail(msg['From'], msg['To'], msg.as_string())
finally:
    s.quit()

Comments

2

I found I had to do an ehlo() and a starttls() before sending mail via gmail:

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(SERVER_EMAIL,EMAIL_HOST_PASSWORD)

It shouldn't make a difference with the login, but I use a MIMEMultipart from email.mime.multipart for the body, with something like:

msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = mFrom
msg['To'] = mTo
if textBody:
   part1 = MIMEText(textBody, 'plain')
   msg.attach(part1)
if htmlBody:
   part2 = MIMEText(htmlBody, 'html')
   msg.attach(part2)
BODY = msg.as_string()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.