3

I am fairly new to python so I need lots of help with stuff :) I am having issues with this:

elif(command == "email" or command == "Email"):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    euser = input("Enter Gmail Username: ")
    epass = input("Enter Gmail Password: ")
    server.login(euser, epass)
    msginput = input("Enter Message: ")
    msg = ("\n"+msginput)
    sourcemail = input("Enter Your Email: ")
    targetemail = input("Enter Your Recipient: ")
    server.sendmail(sourcemail, targetemail, msg)

All of this may be wrong. However, after I enter my gmail username and password, it gives this:

Traceback (most recent call last):
  File "/Users/19austinh/Google Drive/Other/Python/Login.py", line 145, in <module>
    server.login(euser, epass)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/smtplib.py", line 595, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.

I don't know how to fix that. Also, is my script correct? If not, please tell me what to fix. Any answers are greatly appreciated :)

Edit:

Also, how do you make a subject input? So do I just add:

subjectinput = input("Enter Subject: ") 

and

smtplib.subject = ("\n"+subjectinput)

or something? Because it does not send the email when I did that.

Dark Leviathan
  • 489
  • 7
  • 19

1 Answers1

1

According to Gmail APIs documentation:

The outgoing SMTP server, smtp.gmail.com, requires TLS. Use port 465, or port 587 if your client begins with plain text before issuing the STARTTLS command.

Call starttls before login:

smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.starttls() # <---------
smtp.login(username, password)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Cool! Works! Then how do you make a subject input? – Dark Leviathan Mar 01 '14 at 10:30
  • @HelloWorld, You need to add `Subject` header. (Header lines and body are separated by a blank line) For example: `smtp.sendmail(sender, recipient, 'Subject: title\r\n\r\nbody of the mail')` – falsetru Mar 01 '14 at 10:37
  • so do I add: "subjectinput = input("Enter Subject: ")" and "smtplib.subject = ("\n"+subjectinput)" or something? Because it does not send the email. – Dark Leviathan Mar 01 '14 at 10:44
  • @HelloWorld, `subjectinput = input("Enter Subject: ")` and `server.sendmail(sourcemail, targetmail, 'Subject: {}\r\n\r\n{}'.format(subjectinput, msg))` – falsetru Mar 01 '14 at 10:53