发送带有python SSL错误的电子邮件

时间:2014-06-15 12:10:10

标签: python python-2.7 ssl

我正在尝试使用python发送电子邮件,但我收到了SSL错误,我不知道为什么。

   File "./test.py", line 735, in <module>
     mail()   File "./test.py", line 721, in mail
     server.starttls()   File "/usr/local/lib/python2.7/smtplib.py", line 641, in starttls
     raise RuntimeError("No SSL support included in this Python") RuntimeError: No SSL support included in this Python

如何添加SSL支持?

它只是一个小功能来测试我是否可以使用python发送电子邮件

def mail():
    import smtplib
    global log_location
    #inFile_list = open(log_location, 'r')
    #msg = inFile_list.readlines()
    #inFile_list.close()

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    #Next, log in to the server
    server.login("my username", "my password")

    #Send the mail
    msg = "\nHello!" # The /n separates the message from the headers
    server.sendmail("mymail@gmail.com", "mymail@gmail.com", msg)
    server.quit()

2 个答案:

答案 0 :(得分:0)

尝试安装_ssl.so手册,但它没有用。

相反,我已经从2.7.6升级到2.7.7,现在我可以导入SSL。

答案 1 :(得分:0)

import smtplib
import config


def send_email(subject,msg):
    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(config.EMAIL_ADDRESS, config.PASSWORD)
        message = 'subject: {}\n\n{}'.format(subject,msg)
        server.sendmail(config.EMAIL_ADDRESS,config.EMAIL_ADDRESS, message)
        server.quit()
        print('all good')
    except:
        print('email failed to send')
subject = "TEST SUBJECT"
msg = "hello"
send_email(subject,msg)

这对我有用,但是您必须创建一个名为config.py的新文件,然后放入

EMAIL_ADDRESS = "email"
PASSWORD = "pwd"

最后一步是转到此链接>>> https://myaccount.google.com/lesssecureapps <<<并将其打开。现在您可以运行脚本了