0

I am new to python. I am trying to send mails using the following Python code and I am using a code editor Jupyter. I have enabled IMAP in my gmail account and have turned on the access for less secured app. The code editor i am using is unable to compile the line

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

Can someone please tell me what I am doing wrong?

#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart  
from email.mime.text import MIMEText
fromaddr = 'abc@gmail.com' 
toaddr = 'abc@gmail.com' 
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = 'SUBJECT OF THE MAIL'
body = 'YOUR MESSAGE HERE'
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(fromaddr, 'YOUR PASSWORD')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Vega
  • 27,856
  • 27
  • 95
  • 103
john
  • 11
  • 8
  • 2
    What do you mean *"unable to compile"* - do you see an error message? What happens? Give a [mcve]. – jonrsharpe Sep 09 '17 at 10:54
  • 3
    Exactly what error message are you getting? Could you add that information to your question? – payne Sep 09 '17 at 10:54
  • Hi, following is the error File "/usr/lib64/python2.7/smtplib.py", line 622, in login raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, '5.7.14 Please log in via your web browser and\n5.7.14 – john Sep 09 '17 at 12:48
  • Possible duplicate of [Gmail SMTP debug: error "please log in via your web browser"](https://stackoverflow.com/questions/20337040/gmail-smtp-debug-error-please-log-in-via-your-web-browser) – Christian Ternus Sep 09 '17 at 14:23

0 Answers0