使用Python发送邮件时的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
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 <<< 并把它打开。现在你可以运行这个脚本了。
0
我试着手动安装 _ssl.so 文件,但没成功。
后来我把版本从 2.7.6 升级到了 2.7.7,现在我可以导入 SSL 了。