简体   繁体   English

如何使用Python发送经过身份验证的电子邮件?

[英]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: 构建防弹解决方案:

    • analyze what server announces as its capabilities 分析服务器宣布其功能
    • choose the one that better meets your requirement 选择一种更符合您要求的产品
    • use that authentication method 使用该身份验证方法
    • actually send the mail 实际发送邮件

    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 使用适合服务器的自定义连接方法

    • read the documentation of the server to know what it declares to support 阅读服务器的文档以了解其声明要支持的内容
    • test it manually first through a mere telnet connection then in an interactive Python session (idle is enough here, but you can choose your prefered Python shell) 首先通过一个简单的telnet连接进行手动测试,然后在一个交互式Python会话中进行测试(此处足够空闲,但您可以选择首选的Python shell)
    • carefully program the way you have just tested - but leave relevant error messages in cases the server capabilities change... 仔细编程您刚刚测试过的方式-但是在服务器功能更改的情况下留下相关的错误消息...

    IMHO much simpler... 恕我直言,简单得多...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM