我有一个Python脚本来发送电子邮件。我利用此功能将警报发送到我的电子邮件和电话。但是由于某些原因,发送到gmail帐户时未收到电子邮件。我的代码与其他示例之间的最大区别是我无法使用smtp.gmail.com。这是我的电子邮件/文本功能:
import smtplib
sender = '[email protected]'
smtpObj = smtplib.SMTP('smtp.company.com')
def text_func ( subject,text_message ):
text_receivers = ['##########@vtext.com',
'[email protected]',
'[email protected]'
]
text_message = """Subject: %s
%s
""" % (subject, text_message)
smtpObj.sendmail(sender, text_receivers, text_message)
上面的代码适用于向我的手机和我的工作电子邮件发送短信,但不能发送给Gmail。我检查并确保电子邮件没有显示在垃圾邮件文件夹中。Google似乎完全阻止了它。有任何想法吗?
找到了解决方案。
import smtplib
from email.mime.text import MIMEText
sender = '[email protected]'
smtpObj = smtplib.SMTP('smtp.company.com')
def text_func ( subject,text_message ):
text_receivers = ['##########@vtext.com',
'[email protected]',
'[email protected]'
]
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())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句