我试图使用python发送电子邮件(Gmail),但我得到以下错误。
Traceback (most recent call last):
File "emailSend.py", line 14, in <module>
server.login(username,password)
File "/usr/lib/python2.5/smtplib.py", line 554, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
Python脚本如下所示。
import smtplib
fromaddr = 'user_me@gmail.com'
toaddrs = 'user_you@gmail.com'
msg = 'Why,Oh why!'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
你可以在这里找到它: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())
不直接相关,但仍然值得指出的是,我的包试图使发送gmail消息非常快速和轻松。它还试图维护一个错误列表,并试图立即指出解决方案。
它只需要下面的代码来完成你所写的:
import yagmail
yag = yagmail.SMTP('user_me@gmail.com')
yag.send('user_you@gmail.com', 'Why,Oh why!')
或者一句话:
yagmail.SMTP('user_me@gmail.com').send('user_you@gmail.com', 'Why,Oh why!')
关于包/安装,请查看git或pip,可用于Python 2和3。
你需要在直接运行到STARTTLS之前说EHLO:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
此外,您应该真正创建From:、To:和Subject:消息头,用空行与消息体分隔,并使用CRLF作为EOL标记。
E.g.
msg = "\r\n".join([
"From: user_me@gmail.com",
"To: user_you@gmail.com",
"Subject: Just a message",
"",
"Why, oh why"
])
注意:
为了做到这一点,你需要在gmail帐户配置中启用“允许不太安全的应用程序”选项。否则,当gmail检测到一个非谷歌应用程序试图登录你的帐户时,你会得到一个“关键安全警报”。
这是
创建Gmail APP密码!
创建之后,创建一个名为sendgmail.py的文件
然后添加以下代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By : Jeromie Kirchoff
# Created Date: Mon Aug 02 17:46:00 PDT 2018
# =============================================================================
# Imports
# =============================================================================
import smtplib
# =============================================================================
# SET EMAIL LOGIN REQUIREMENTS
# =============================================================================
gmail_user = 'THEFROM@gmail.com'
gmail_app_password = 'YOUR-GOOGLE-APPLICATION-PASSWORD!!!!'
# =============================================================================
# SET THE INFO ABOUT THE SAID EMAIL
# =============================================================================
sent_from = gmail_user
sent_to = ['THE-TO@gmail.com', 'THE-TO@gmail.com']
sent_subject = "Hey Friends!"
sent_body = ("Hey, what's up? friend!\n\n"
"I hope you have been well!\n"
"\n"
"Cheers,\n"
"Jay\n")
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(sent_to), sent_subject, sent_body)
# =============================================================================
# SEND EMAIL OR DIE TRYING!!!
# Details: http://www.samlogic.net/articles/smtp-commands-reference.htm
# =============================================================================
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_app_password)
server.sendmail(sent_from, sent_to, email_text)
server.close()
print('Email sent!')
except Exception as exception:
print("Error: %s!\n\n" % exception)
所以,如果你成功了,你会看到这样的图像:
我通过给自己和自己发送电子邮件进行测试。
注意:我在我的帐户上启用了两步验证。应用程序密码与此工作!(对于gmail SMTP设置,您必须访问https://support.google.com/accounts/answer/185833?hl=en并遵循以下步骤)
此设置对于启用了两步验证的帐户不可用。这样的帐户需要特定于应用程序的密码来访问不太安全的应用程序。
澄清
导航到https://myaccount.google.com/apppasswords,并创建一个APP密码如上所述。