简体   繁体   English

从 python 脚本发送电子邮件

[英]Sending emails from a python script

I am working on writing a python test using unittest and selenium webdriver to test if our server is down, and to send an email to notify me if it is not.我正在使用 unittest 和 selenium webdriver 编写一个 python 测试来测试我们的服务器是否关闭,如果没有,则发送电子邮件通知我。 I am currently working on implementing just the email functionality.我目前正在努力实现电子邮件功能。 This is my current code.这是我目前的代码。 When run, the program seems to run, but never ends, and never sends the email.运行时,程序似乎在运行,但永远不会结束,也永远不会发送电子邮件。 (ie in the command line the program seems as though it is running as it doesn't give any errors and you have to escape the "running" program before you can enter another command). (即,在命令行中,程序似乎正在运行,因为它没有给出任何错误,您必须在输入另一个命令之前退出“正在运行”的程序)。 My current code is:我目前的代码是:

 #tests for if server is up and notifies if it is not
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import os
    import time
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    from selenium.webdriver.common.action_chains import ActionChains
    from urllib.request import urlopen
    from html.parser import HTMLParser
    import smtplib
    
    
    class PythonOrgSearch(unittest.TestCase):
    
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.login("email@gmail.com", "password")
            msg = "Testing if server is down" 
            server.sendmail("email@gmail.com", "email@gmail.com", msg)
    
    if __name__ == "__main__":
        unittest.main()

I am unsure why this does not work, and would appreciate any insight.我不确定为什么这不起作用,并希望得到任何见解。 Thank you!谢谢!

Edit编辑

When changing the code as suggested, I got the following error:按照建议更改代码时,出现以下错误:

Traceback (most recent call last):
  File "testServerIsUp.py", line 14, in <module>
    class PythonOrgSearch(unittest.TestCase):
  File "testServerIsUp.py", line 18, in PythonOrgSearch
    server.starttls() #and this method to begin encryption of messages
  File "C:\Users\663255\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 751, in starttls
    "STARTTLS extension not supported by server.")
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.

You need to start a conversation with the mail server and enable encryption:您需要与邮件服务器开始对话并启用加密:

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls() #and this method to begin encryption of messages
server.login("email@gmail.com", "password")
msg = "Testing if server is down" 
server.sendmail("email@gmail.com", "email@gmail.com", msg)

Since the smtplib.SMTP() call was not successful, you can try SMTP_SSL on port 465:由于smtplib.SMTP()调用未成功,您可以在端口 465 上尝试SMTP_SSL

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("email@gmail.com", "password")
msg = "Testing if server is down" 
server.sendmail("email@gmail.com", "email@gmail.com", msg)

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

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