Edison上程序开发——在Edison上发送邮件通知
本帖最后由 孙毅 于 2015-11-22 13:08 编辑在Edison开发过程中经常会想要发送邮件通知用户,下面这个脚本,可以帮大家来实现这个小功能。重点是,此python脚本,没有什么依赖,直接放在Edison里面就可以执行发送邮件了。有兴趣的朋友们可以试试。
# Libraries required
import os
import smtplib
from email.mime.text import MIMEText
import getpass
# Color class for output messages
class text_colors:
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
END = '\033[0m'
#Set the local host to use
"""
If you want to send emails from a Google account
use as local host: smtp.gmail.com
If you want you send emails from a Hotmail account
use as local host: smtp.live.com
The local host will be selected properly once the user
enter the sender email.
"""
localHost = ""
def enter_credentials():
print ""
print text_colors.GREEN + "Provide the sender (password) and receiver email address." + text_colors.END
print ""
print text_colors.YELLOW + "Note: Currently the email can be sent only from gmail and hotmail accounts." + text_colors.END
while True:
print ""
emailSender = raw_input("Email sender: ")
if "@gmail." in emailSender:
localHost = "smtp.gmail.com"
break
elif "@hotmail." in emailSender or "@live." in emailSender:
localHost = "smtp.live.com"
break
else:
print ""
print text_colors.RED + "Error: The email entered is not validated. It has to be a gmail or hotmail account." + text_colors.END
password = getpass.getpass(prompt="Password: ")
emailReceiver = raw_input("Email receiver: ")
return emailSender, password, emailReceiver, localHost
def build_message():
print ""
print text_colors.GREEN + "Build the email's content." + text_colors.END
print ""
emailSubject = raw_input("Email subject: ")
emailContent = raw_input("Email content: ")
return MIMEText(emailContent), emailSubject
def send_email():
global emailSender, password, emailReceiver, localHost, message
# Try to connect to the SMTP Server
try:
print ""
print text_colors.GREEN + "Connecting to SMTP Server..." + text_colors.END
mailServer = smtplib.SMTP(localHost,587)
mailServer.starttls()
mailServer.login(emailSender,password)
except smtplib.SMTPAuthenticationError:
print ""
print text_colors.RED + "Authentication Error. Can't login." + text_colors.END
mailServer.quit()
exit()
except:
print ""
print text_colors.RED + "Error. Can't connect to the SMTP Server." + text_colors.END
exit()
else:
print ""
print text_colors.GREEN + "Login successfully." + text_colors.END
# Try to send the email
try:
print ""
print text_colors.GREEN + "Sending email..." + text_colors.END
mailServer.sendmail(emailSender,emailReceiver,message.as_string())
except:
print ""
print text_colors.RED + "Error. The email couldn't be sent." + text_colors.END
else:
print ""
print text_colors.GREEN + "Email sent successfully." + text_colors.END
finally:
mailServer.quit()
exit()
def main():
global emailSender, password, emailReceiver, localHost, message
os.system("clear")
print text_colors.CYAN + "SendSimpleText.py" + text_colors.END
# Asks for the credentials prior to send the email
emailSender, password, emailReceiver, localHost = enter_credentials()
# Asks for the email's content
message, message['Subject'] = build_message()
message['From'] = emailSender
message['To'] = emailReceiver
# Sends the email
send_email()
if __name__ == "__main__":
main()
执行的效果如下:
是不是很炫。不过后面还得继续研究如何让Edison能够发送微信,有经验的朋友可以给点提示啊。
必须是精华啊 让管理员给你个精华 大连林海 发表于 2015-11-21 14:59
必须是精华啊 让管理员给你个精华
是不是可以自己加精华啊。。。听说自己加的也算啊。。。 孙毅 发表于 2015-11-21 15:07
是不是可以自己加精华啊。。。听说自己加的也算啊。。。
恩 我是没看完 就睡了 昨天走走错道了 越来越有逼格了
大圣,怎么退出程序啊啊? 凌风清羽 发表于 2015-11-21 19:16
大圣,怎么退出程序啊啊?
因为大圣做的是DEMO 凌风清羽 发表于 2015-11-21 19:16
大圣,怎么退出程序啊啊?
晕 ctrl+c,这不是首先要尝试的方法啊?! hnyzcj 发表于 2015-11-21 19:38
因为大圣做的是DEMO
这个。。。。。已经比demo的程度好很多了。吼吼
东西收到了,全湿了。。。双11,你懂的,东西都堆在外面,而且下大雨哎。。。。。
不过,不怪你,哈哈哈 凌风清羽 发表于 2015-11-21 17:46
越来越有逼格了
恩,逼格是一直我们追求的!!! 孙毅 发表于 2015-11-21 22:24
这个。。。。。已经比demo的程度好很多了。吼吼
东西收到了,全湿了。。。双11,你懂的,东西都堆在外面 ...
好吧,不过我邮寄那天离爽11有些日子了 孙毅 发表于 2015-11-21 22:24
恩,逼格是一直我们追求的!!!
等我这段时间忙完,开始车架 有错字,“平邮”
发微信的话,主动发送不大可能,用作微信公众平台服务器还是可以的 hnyzcj 发表于 2015-11-22 07:07
好吧,不过我邮寄那天离爽11有些日子了
现在是双11退货的日子。。。。 吹口琴的钢铁侠 发表于 2015-11-22 12:57
有错字,“平邮”
发微信的话,主动发送不大可能,用作微信公众平台服务器还是可以的 ...
感谢,微信平台服务器得话,有经验吗?或者有文章推荐。
服务器我已经有了,公众平台也有了,现在就差。。。solution了。。。嘻嘻 孙毅 发表于 2015-11-22 13:06
感谢,微信平台服务器得话,有经验吗?或者有文章推荐。
服务器我已经有了,公众平台也有了,现在就差。 ...
嗯,可以用户对公众号发送消息,后台处理后和edison通信
页:
[1]