Python gmail-smtp脚本适用于win \ 3.6,而不是linux \ 3.5.2

时间:2018-05-30 15:11:20

标签: python python-3.x gmail

以下脚本在使用Python 3.6的Win 10框中完美运行:

import smtplib,subprocess

def send_email(user, pwd, recipient, subject, body):


    FROM = user
    TO = recipient if isinstance(recipient, list) else [recipient]
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(user, pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print ('successfully sent the mail')
    except Exception as e:
                            print(e)
                            print ("failed to send mail")

send_email("user@gmail.com", "pass", "dst@email.com", "some subject", "some body")

然而,它失败了

  

“请通过网络浏览器和\ n5.7.14登录,然后再试一次”

当我尝试从Ubuntu 16.04框和Python 3.5.2运行它时。 我应该提一下,在我的Gmail帐户上启用了“允许不安全的应用程序”(没有它的Win框没有工作)。

0 个答案:

没有答案