我有一个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。我检查并确保电子邮件不会显示在垃圾邮件文件夹中。谷歌似乎完全阻止了它。有什么想法吗?
答案 0 :(得分:1)
这可能不正确,因为我对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
%s
""" % (subject, text_message)
smtpObj.sendmail(sender, text_receivers, text_message)
答案 1 :(得分:1)
找到了解决方案。
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())
答案 2 :(得分:0)
您可能需要更改您的Gmail设置,以便将不太安全的应用程序添加到您的帐户中。请参阅以下链接,来自谷歌的客户专家尼克。
https://support.google.com/accounts/answer/6010255?hl=en
选项2:更改您的设置以允许不太安全的应用进入您的应用 帐户。我们不建议使用此选项,因为它可以使其更容易 有人闯入你的帐户。如果您想允许访问 无论如何,请按照下列步骤操作:转到您的安全性较低的应用部分 Google帐户。启用允许安全性较低的应用。如果你没有看到这个 设置,您的管理员可能已关闭不太安全的应用程序 帐户访问。
答案 3 :(得分:0)
第1步:
Allowing less secure apps to access your account
发送电子邮件:
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...'
答案 4 :(得分:0)
我也面临着同样的问题。当我从网络邮件中检查收件箱时,发现该电子邮件已被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()
)