Python smtplib 和 configparser 作为函数

Python smtplib and configparser as function

你好我尝试做一个小的监控,在monitoring.py,它应该发送一个带有函数sendmail.sende()的邮件。但我认为,我遇到了变量问题。你能给我一些提示吗,请问我做错了什么? 这是 sendmail.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')

您没有描述问题所在,所以只是猜测。但是函数readLastLog是错误的:

  • 它只打印到 stdout 而不会 return 任何东西 - 所以实际上 returns None
  • 稍后用于初始化仅提供 nachricht.set_payload(None)
  • 的有效载荷

其他一些即时改进:

  • 在另一个函数中声明一个函数并没有错并且有实际用例,但在这里我真的很想知道为什么 readLastLog 定义在 sende body...
  • 因为您已经用 To:From: header 构建了一个消息,您可以使用 send_message 函数:

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

所以我用 return 更改了 readLastLog,并制作了一个单独的函数, 无论如何,还没有修复脚本,所以这里的问题可能是我的@web.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()