可以发送邮件的python脚本

脚本内容如下,很短。

#!/usr/bin/python
#coding: utf-8  
import smtplib  
import sys
from email.mime.text import MIMEText  
from email.header import Header  
from email.Utils import COMMASPACE
  
receiver = sys.argv[1]
subject = sys.argv[2]
mailbody =  sys.argv[3]
smtpserver = 'smtp.exmail.qq.com'  
username = 'username'  
password = 'password'  
sender = username
  
msg = MIMEText(mailbody,'html','utf-8')#中文需参数‘utf-8’,单字节字符不需要  
msg['Subject'] = Header(subject, 'utf-8')  
msg['From'] = username
msg['To'] = receiver
  
smtp = smtplib.SMTP()  
smtp.connect(smtpserver)  
smtp.login(username, password)  
smtp.starttls()
smtp.sendmail(msg['From'], msg['To'], msg.as_string())  
smtp.quit() 

 

自动发邮件的python的脚本,支持用户名密码的
你需要填上smtp的服务器,用户名密码。就ok了,邮箱账号是我申请的,密码如下
smtpserver = 'smtp.exmail.qq.com'
username = 'test1@dawnedu.com'
password = '123456Aa'

完整脚本如下

[root@linux-node1 tools]# cat pymail.py 
#!/usr/bin/python
#coding: utf-8  
import smtplib  
import sys
from email.mime.text import MIMEText  
from email.header import Header  
from email.Utils import COMMASPACE
  
receiver = sys.argv[1]
subject = sys.argv[2]
mailbody =  sys.argv[3]
smtpserver = 'smtp.exmail.qq.com'  
username = 'test1@dawnedu.com'  
password = '123456Aa'  
sender = username
  
msg = MIMEText(mailbody,'html','utf-8')#中文需参数‘utf-8’,单字节字符不需要  
msg['Subject'] = Header(subject, 'utf-8')  
msg['From'] = username
msg['To'] = receiver
  
smtp = smtplib.SMTP()  
smtp.connect(smtpserver)  
smtp.login(username, password)  
smtp.starttls()
smtp.sendmail(msg['From'], msg['To'], msg.as_string())  
smtp.quit() 
[root@linux-node1 tools]# 

 

运行时需要传递3个参数

另外,这个zabbix脚本要接收3个参数
$1,$2,$3
$1是邮件地址
$2 邮件主题
$3 邮件内容

[root@linux-node1 tools]# python pymail.py zhangyingxiang@dawnedu.com 市中心那边  要求关停机器
[root@linux-node1 tools]# python pymail.py 525031638@qq.com 市中心那边  要求关停机.

   

 测试中文不乱码

 

posted on 2019-03-01 21:53  nmap  阅读(148)  评论(0编辑  收藏  举报

导航