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

后端 未结 14 1891
感情败类
感情败类 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条回答
  •  Happy的楠姐
    2020-11-22 03:33

    You need to say EHLO before just running straight into STARTTLS:

    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    

    Also you should really create From:, To: and Subject: message headers, separated from the message body by a blank line and use CRLF as EOL markers.

    E.g.

    msg = "\r\n".join([
      "From: user_me@gmail.com",
      "To: user_you@gmail.com",
      "Subject: Just a message",
      "",
      "Why, oh why"
      ])
    

提交回复
热议问题