[英]Python Can't Send Emails to Gmail
我有一个 python 脚本来发送电子邮件。 我利用它向我的电子邮件和电话发送警报。 但是由于某种原因,发送到 Gmail 帐户时未收到电子邮件。 我在我的代码和其他示例之间看到的最大区别是我不能使用 smtp.gmail.com。 这是我的电子邮件/文本功能:
import smtplib
sender = 'xxxx@company.com'
smtpObj = smtplib.SMTP('smtp.company.com')
def text_func ( subject,text_message ):
text_receivers = ['##########@vtext.com',
'xxxxxxxx@company.com',
'xxxxxxxx@gmail.com'
]
text_message = """Subject: %s
%s
""" % (subject, text_message)
smtpObj.sendmail(sender, text_receivers, text_message)
上面的代码适用于向我的手机和我的工作电子邮件发送短信,但不适用于 Gmail。 我检查并确保电子邮件没有出现在垃圾邮件文件夹中。 谷歌似乎完全阻止了它。 有任何想法吗?
这可能不正确,因为我对smtplib
的短信方面没有太多经验,但看起来您需要启动 TLS 连接。 我会将您的脚本更改为以下内容:
import smtplib
sender = 'xxxx@company.com'
smtpObj = smtplib.SMTP('smtp.company.com')
smtpObj.starttls()
def text_func ( subject,text_message ):
text_receivers = ['##########@vtext.com',
'xxxxxxxx@company.com',
'xxxxxxxx@gmail.com'
]
text_message = """Subject: %s\n\n%s""" % (subject, text_message)
smtpObj.sendmail(sender, text_receivers, text_message)
我也面临同样的问题。 当我从 webmail 检查我的收件箱时,我发现该电子邮件被 gmail 拒绝。 通过向电子邮件添加“发件人”标题解决了该问题。
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender_email = "test@example.com"
receiver_email = "example@gmail.com"
password = "pass1234"
host = "example.com"
port = 465
message = MIMEMultipart("alternative")
message["Subject"] = "Email Subject"
message["From"] = sender_email
message["To"] = receiver_email
text = "This is the email Body"
message.attach(MIMEText(text, "plain"))
context = ssl.create_default_context()
with smtplib.SMTP_SSL(host, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)
您可能需要更改 Gmail 设置以允许安全性较低的应用进入您的帐户。 请参阅下面来自谷歌帐户专家尼克的链接。
https://support.google.com/accounts/answer/6010255?hl=en
选项 2:更改设置以允许安全性较低的应用进入您的帐户。 我们不推荐此选项,因为它可以让他人更轻松地侵入您的帐户。 如果您仍想允许访问,请按照以下步骤操作: 转到您的 Google 帐户的安全性较低的应用部分。 打开允许不太安全的应用程序。 如果您没有看到此设置,则您的管理员可能已关闭安全性较低的应用帐户访问权限。
步骤1:
发送电子邮件:
import smtplib
gmail_user = 'you@gmail.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['me@gmail.com', 'bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, what's up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print 'Email sent!'
except:
print 'Something went wrong...'
找到了解决办法。
import smtplib
from email.mime.text import MIMEText
sender = 'xxxx@company.com'
smtpObj = smtplib.SMTP('smtp.company.com')
def text_func ( subject,text_message ):
text_receivers = ['##########@vtext.com',
'xxxxxxxx@company.com',
'xxxxxxxx@gmail.com'
]
cont = '''\
<html>
<head></head>
<body>
<p>{0}</p>
</body>
</html>
'''.format(text_message)
msgTo = ""
for person in text_receivers:
msgTo += "{0} ".format(person)
msg = MIMEText(cont, 'html')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = msgTo
smtpObj.sendmail(sender, text_receivers, msg.as_string())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.