How to send an email with Gmail as provider using Python?

后端 未结 14 1697
感情败类
感情败类 2020-11-22 03:29

I am trying to send email (Gmail) using python, but I am getting following error.

Traceback (most recent call last):  
File \"emailSend.py\", line 14, in <         


        
14条回答
  •  误落风尘
    2020-11-22 03:44

    You can find it here: http://jayrambhia.com/blog/send-emails-using-python

    smtp_host = 'smtp.gmail.com'
    smtp_port = 587
    server = smtplib.SMTP()
    server.connect(smtp_host,smtp_port)
    server.ehlo()
    server.starttls()
    server.login(user,passw)
    fromaddr = raw_input('Send mail by the name of: ')
    tolist = raw_input('To: ').split()
    sub = raw_input('Subject: ')
    
    msg = email.MIMEMultipart.MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = email.Utils.COMMASPACE.join(tolist)
    msg['Subject'] = sub  
    msg.attach(MIMEText(raw_input('Body: ')))
    msg.attach(MIMEText('\nsent via python', 'plain'))
    server.sendmail(user,tolist,msg.as_string())
    

提交回复
热议问题