Python smtplib和configparser作为函数

2024-03-28 12:46:35 发布

您现在位置:Python中文网/ 问答频道 /正文

你好我试着做一个小监控,在监控.py,它应该发送一封具有发送邮件.sende(). 但我想,我有变量的问题。你能给我一些提示吗?我做错了什么? 这就是发送邮件.py你知道吗

def sende():
    import smtplib
    import configparser
    from email.message import Message

    config = configparser.ConfigParser()
    config.read('config.ini')
    absender=(config['SMTP']['absender'])
    password=(config['SMTP']['password'])
    empfaenger=(config['SMTP']['empfaenger'])
    smtphost=(config['SMTP']['smtphost'])
    port=int(config['SMTP']['port'])

    def readLastLog():
        log=open('log', 'r')
        for line in log:
            last= line
        print (last)
        log.close()

    #check for if variables is correct
    print("Email Config:")
    print(absender, '\n',empfaenger,'\n',smtphost,'\n',port)

    server = smtplib.SMTP_SSL(host=smtphost, port=port)

    #do I need realy need this?
    #server.esmtp_features['auth'] = 'LOGIN PLAIN'

    nachricht = Message()
    nachricht.set_payload(readLastLog())
    nachricht["Subject"] = "Kritische Warnung - Hardlimit erreicht"
    nachricht["FROM"] = absender
    nachricht["To"] = empfaenger

    server.login(absender, password)
    server.sendmail(absender, empfaenger, nachricht.as_string())
    server.quit()

sende()

错误:

Traceback (most recent call last):
  File "./sendmail.py", line 42, in <module>
    sende()
  File "./sendmail.py", line 38, in sende
    server.login(absender, password)
  File "/usr/lib/python3.5/smtplib.py", line 729, in login
    raise last_exception
  File "/usr/lib/python3.5/smtplib.py", line 720, in login
    initial_response_ok=initial_response_ok)
  File "/usr/lib/python3.5/smtplib.py", line 641, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Error: authentication failed: authentication failure')

Tags: inpyconfigserverportlinepasswordsmtp
2条回答

你没有描述出哪里出了问题,所以只是猜测。但是函数readLastLog是错误的:

  • 它只打印到stdout,不返回任何内容—因此实际上返回None
  • 它稍后用于初始化只提供nachricht.set_payload(None)的有效负载

其他一些即时改进:

  • 在另一个函数中声明一个函数并没有错,而且有真正的用例,但我真的想知道为什么readLastLog是在sende体中定义的。。。你知道吗
  • 当您构建了一个带有To:From:头的消息时,您可以使用send_message函数:

    ...
    server.send_message(nachricht)
    server.quit()
    

所以我改变了readLastLog的返回值,做了一个单独的函数, 不管怎样,这还没有解决问题,所以这里的问题可能是我的@网站.de邮件。 与其他电子邮件一起更改为自己的邮件服务器。现在我确实收到电子邮件了。 抱歉耽误了你的时间。你知道吗

def readLastLog():
    log=open('log', 'r')
    for line in log:
        lastlog= line
    print (lastlog)
    log.close()
    return lastlog

def sende():
    import smtplib
    import configparser
    from email.message import Message

    config = configparser.ConfigParser()
    config.read('config.ini')
    absender=(config['SMTP']['absender'])
    password=(config['SMTP']['password'])
    empfaenger=(config['SMTP']['empfaenger'])
    smtphost=(config['SMTP']['smtphost'])
    port=int(config['SMTP']['port'])


    #check for if variables is correct
    print("Email Config:")
    print(absender, '\n',empfaenger,'\n',smtphost,'\n',port)

    server = smtplib.SMTP_SSL(host=smtphost, port=port)

    #do I need realy need this?
    #server.esmtp_features['auth'] = 'LOGIN PLAIN'

    nachricht = Message()
    nachricht.set_payload(readLastLog())
    nachricht["Subject"] = "Kritische Warnung - Hardlimit erreicht"
    nachricht["FROM"] = absender
    nachricht["To"] = empfaenger

    server.login(absender, password)
    server.sendmail(absender, empfaenger, nachricht.as_string())
    server.quit()

相关问题 更多 >