[英]How to send authenticated email using Python?
I have a python function to send an email notification. 我有一个python函数来发送电子邮件通知。 I have included the login call to authenticate with my local SMTP server, however emails are being returned stating '553-mail rejected because your IP is in the PBL'.
我已经包含了用于通过本地SMTP服务器进行身份验证的登录呼叫,但是由于“您的IP位于PBL中”而返回了电子邮件,说明“ 553邮件已被拒绝”。 Further reading on https://www.spamhaus.org/pbl reveals that apparently I am not being prevented from sending email, I am simply required to authenticate first.
在https://www.spamhaus.org/pbl上的进一步阅读显示,显然我没有被阻止发送电子邮件,只需要我先进行身份验证即可。
I have tried base64 encoding to avoid sending the password as plain text without success. 我尝试使用base64编码来避免将密码作为纯文本发送而没有成功。
I have also looked at the Pysasl library, but I am a little unsure how I might use this for authenticating with my SMTP server. 我也查看了Pysasl库,但是我不太确定如何将其用于SMTP服务器的身份验证。
Would anyone have any guidance as to the correct use of either base64 encoding, the pysasl library or if a better method exists for satisfying the correct authentication requirements? 对于正确使用base64编码,pysasl库或是否存在满足正确身份验证要求的更好方法,任何人都将有任何指导吗?
My code is below. 我的代码如下。
def emailNotify(self,userid):
SMTPserver = 'localhost'
sender = '***' # blanked
username = '***' # blanked
receiver = '***' # blanked
pwd = '***' # blanked
text_subtype = 'plain'
msg = 'requested data was posted to dashboard for '+userid
subject = 'confirmation of POST\'d data for '+userid
try:
msg = MIMEText(msg, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
conn = smtplib.SMTP(SMTPserver)
conn.login(username, pwd)
try:
conn.sendmail(sender, receiver, msg.as_string())
finally:
conn.quit()
except:
print('message sending failed')
Thanks in advance. 提前致谢。
You have 2 ways of building a program for sending mail: 您有两种方法来构建用于发送邮件的程序:
build a bullet proof solution: 构建防弹解决方案:
simply that means that you have to implement code for reading and decoding what the server returns (which can be weird) and also all various authentication methods, not speaking of TLS 简而言之,这意味着您必须实现代码以读取和解码服务器返回的内容(可能很奇怪)以及所有各种身份验证方法,而不是讲TLS
use a custom connection method adapted to that server 使用适合该服务器的自定义连接方法
IMHO much simpler... 恕我直言,简单得多...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.