Edison上程序开发——在Edison上发送邮件通知

2015-11-211.4万
AI 快速预览详细 收起
本文介绍了如何在Edison上使用Python脚本发送邮件通知。该脚本无需额外依赖,支持Gmail和Hotmail账户。通过简单的步骤,您可以轻松实现这一功能。首先,输入发件人和收件人的邮箱地址及密码;然后,构建邮件内容;最后,通过SMTP服务器发送邮件。整个过程简单易懂,适合需要在Edison开发过程中发送邮件通知的用户。


在Edison开发过程中经常会想要发送邮件通知用户,下面这个脚本,可以帮大家来实现这个小功能。重点是,此python脚本,没有什么依赖,直接放在Edison里面就可以执行发送邮件了。有兴趣的朋友们可以试试。

  1. # Libraries required
  2. import os
  3. import smtplib
  4. from email.mime.text import MIMEText
  5. import getpass
  6. # Color class for output messages
  7. class text_colors:
  8.         RED         = '\033[91m'
  9.         GREEN         = '\033[92m'
  10.         YELLOW         = '\033[93m'
  11.         BLUE          = '\033[94m'
  12.         MAGENTA = '\033[95m'
  13.         CYAN         = '\033[96m'
  14.         END         = '\033[0m'
  15. #Set the local host to use
  16. """
  17. If you want to send emails from a Google account
  18. use as local host: smtp.gmail.com
  19. If you want you send emails from a Hotmail account
  20. use as local host: smtp.live.com
  21. The local host will be selected properly once the user
  22. enter the sender email.
  23. """
  24. localHost = ""
  25.         
  26. def enter_credentials():
  27.         print ""
  28.         print text_colors.GREEN + "Provide the sender (password) and receiver email address." + text_colors.END
  29.         print ""
  30.         print text_colors.YELLOW + "Note: Currently the email can be sent only from gmail and hotmail accounts." + text_colors.END
  31.         
  32.         while True:
  33.                 print ""
  34.                 emailSender = raw_input("Email sender: ")
  35.                 if "@gmail." in emailSender:
  36.                         localHost = "smtp.gmail.com"
  37.                         break
  38.                 elif "@hotmail." in emailSender or "@live." in emailSender:
  39.                         localHost = "smtp.live.com"
  40.                         break
  41.                 else:
  42.                         print ""
  43.                         print text_colors.RED + "Error: The email entered is not validated. It has to be a gmail or hotmail account." + text_colors.END
  44.                         
  45.         password = getpass.getpass(prompt="Password: ")
  46.         
  47.         emailReceiver = raw_input("Email receiver: ")
  48.         
  49.         return emailSender, password, emailReceiver, localHost
  50.         
  51. def build_message():
  52.         print ""
  53.         print text_colors.GREEN + "Build the email's content." + text_colors.END
  54.         print ""
  55.         
  56.         emailSubject = raw_input("Email subject: ")
  57.         emailContent = raw_input("Email content: ")
  58.         
  59.         return MIMEText(emailContent), emailSubject
  60. def send_email():
  61.         global emailSender, password, emailReceiver, localHost, message
  62.         
  63.         # Try to connect to the SMTP Server
  64.         try:
  65.                 print ""
  66.                 print text_colors.GREEN + "Connecting to SMTP Server..." + text_colors.END
  67.                
  68.                 mailServer = smtplib.SMTP(localHost,587)
  69.                 mailServer.starttls()
  70.                 mailServer.login(emailSender,password)
  71.                
  72.         except smtplib.SMTPAuthenticationError:
  73.                 print ""
  74.                 print text_colors.RED + "Authentication Error. Can't login." + text_colors.END
  75.                 mailServer.quit()
  76.                 exit()
  77.                
  78.         except:
  79.                 print ""
  80.                 print text_colors.RED + "Error. Can't connect to the SMTP Server." + text_colors.END
  81.                 exit()
  82.         else:
  83.                 print ""
  84.                 print text_colors.GREEN + "Login successfully." + text_colors.END
  85.         # Try to send the email
  86.         try:
  87.                 print ""
  88.                 print text_colors.GREEN + "Sending email..." + text_colors.END
  89.                 mailServer.sendmail(emailSender,emailReceiver,message.as_string())
  90.                
  91.         except:
  92.                 print ""
  93.                 print text_colors.RED + "Error. The email couldn't be sent." + text_colors.END
  94.                
  95.         else:
  96.                 print ""
  97.                 print text_colors.GREEN + "Email sent successfully." + text_colors.END
  98.                
  99.         finally:
  100.                 mailServer.quit()
  101.                 exit()
  102. def main():
  103.         global emailSender, password, emailReceiver, localHost, message
  104.         
  105.         os.system("clear")
  106.         print text_colors.CYAN + "SendSimpleText.py" + text_colors.END
  107.         
  108.         # Asks for the credentials prior to send the email
  109.         emailSender, password, emailReceiver, localHost = enter_credentials()
  110.         
  111.         # Asks for the email's content
  112.         message, message['Subject'] = build_message()
  113.         message['From'] = emailSender
  114.         message['To'] = emailReceiver
  115.         
  116.         # Sends the email
  117.         send_email()
  118. if __name__ == "__main__":
  119.       main()
复制代码

执行的效果如下:
Edison上程序开发——在Edison上发送邮件通知图1

是不是很炫。不过后面还得继续研究如何让Edison能够发送微信,有经验的朋友可以给点提示啊。


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
吹口琴的钢铁侠
吹口琴的钢铁侠
有错字,“平邮”
发微信的话,主动发送不大可能,用作微信公众平台服务器还是可以的
吹口琴的钢铁侠
吹口琴的钢铁侠回复孙毅
嗯,可以用户对公众号发送消息,后台处理后和edison通信
孙毅
孙毅
作者
回复吹口琴的钢铁侠
感谢,微信平台服务器得话,有经验吗?或者有文章推荐。
服务器我已经有了,公众平台也有了,现在就差。。。solution了。。。嘻嘻
凌风清羽
凌风清羽
大圣,怎么退出程序啊啊?
孙毅
孙毅
作者
回复hnyzcj
现在是双11退货的日子。。。。
hnyzcj
hnyzcj回复孙毅
好吧,不过我邮寄那天离爽11有些日子了
共5条回复,已加载2条,点击查看更多共5条回复,已加载全部
凌风清羽
凌风清羽
越来越有逼格了
hnyzcj
hnyzcj回复孙毅
等我这段时间忙完,开始车架
孙毅
孙毅
作者
回复凌风清羽
恩,逼格是一直我们追求的!!!
qq815076543322
qq815076543322
必须是精华啊 让管理员给你个精华
qq815076543322
qq815076543322回复孙毅
恩 我是没看完 就睡了 昨天走走错道了
孙毅
孙毅
作者
回复qq815076543322
是不是可以自己加精华啊。。。听说自己加的也算啊。。。
- 没有更多了 -