在linux中如何实现定时发送邮件到指定邮箱,监测任务
生活随笔
收集整理的這篇文章主要介紹了
在linux中如何实现定时发送邮件到指定邮箱,监测任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #!/usr/bin/env python
2 # -*- coding=utf-8 -*-
3 import smtplib
4 from email.mime.text import MIMEText
5 import threading
6 import time, datetime
7
8 mailto_list=["lovychen@126.com"] #里面是對方的郵箱
9 #-----------QQ郵箱發送設置----------------------
10 mail_server="smtp.qq.com"#以qq郵箱為例子,里面是QQ郵箱的服務,換成其他郵箱需要更改服務
11 mail_user=""#這是QQ郵箱的賬號
12 mail_pass=""#如果是其他的可以直接填上密碼,如果用qq之類的,或者郵箱未開服務,會提醒你打開下面的鏈接
13 #QQ郵箱需要去官方打開服務:http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
14 def send_mail(to_list, sub, content):
15 msg = MIMEText(content,'plain','utf-8')
16 msg["Accept-Language"]="zh-CN"
17 msg["Accept-Charset"]="ISO-8859-1,utf-8"
18 msg['Subject'] = sub
19 msg['From'] = mail_user
20 msg['To'] = ";".join(to_list)
21 try:
22 server = smtplib.SMTP()
23 server.connect(mail_server)
24 server.starttls()
25 server.login(mail_user, mail_pass)
26 server.sendmail(mail_user, to_list, msg.as_string())
27 server.close()
28 return True
29 except Exception, e:
30 print str(e)
31 return False
32
33 def getDate():
34 return str(datetime.datetime.utcfromtimestamp(time.time())+datetime.timedelta(hours=8))
35
36 def send_warning_mail(title, info):
37 nowTime = getDate()
38 try:
39 t = threading.Thread(target=send_mail, args=(mailto_list, title, str(nowTime) + " | " + str(info)))
40 t.start()
41 except:pass
42 # send_mail(mailto_list, "mysql異常", info)
43
44 if __name__ == '__main__':
45 send_warning_mail("this is title", "
this is content")
46 # print 111
這是能夠加上附件的郵件發送:
1 #!/usr/bin/env python
2 #coding=utf-8
3 import smtplib
4 from email.MIMEText import MIMEText
5 from email.MIMEMultipart import MIMEMultipart
6 from email.MIMEBase import MIMEBase
7 from email import Encoders
8 import time
9 mail_body='hello, this is the mail content'
10 mail_from=''#發件人的郵箱
11 mail_to=['']#收件人郵箱
12 # 構造MIMEMultipart對象做為根容器
13 msg=MIMEMultipart()
14
15 # 構造MIMEText對象做為郵件顯示內容并附加到根容器
16 body=MIMEText(mail_body)
17 msg.attach(body)
18
19 # 構造MIMEBase對象做為文件附件內容并附加到根容器
20 # 等同于如下3行
21 #contype = 'application/octet-stream'
22 #maintype, subtype = contype.split('/', 1)
23 #part = MIMEBase(maintype, subtype)
24 part = MIMEBase('application', 'octet-stream')
25
26 # 讀入文件內容并格式化,此處文件為當前目錄下,也可指定目錄 例如:open(r'/tmp/123.txt','rb')
27 part.set_payload(open('123.txt','rb').read())
28 Encoders.encode_base64(part)
29 ## 設置附件頭
30 part.add_header('Content-Disposition', 'attachment; filename="herb.zip"')
31 msg.attach(part)
32
33 # 設置根容器屬性
34 msg['Subject']='this is the title'
35 msg['From']=mail_from
36 msg['To']=';'.join(mail_to)
37 msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z')
38 #如上得到了格式化后的完整文本msg.as_string()
39 #用smtp發送郵件
40 smtp=smtplib.SMTP()
41 smtp.connect('')#服務,如果是163的郵箱,就填上smtp.163.com
42 smtp.login('發件的郵箱','發件的密碼')
43 smtp.sendmail(mail_from,mail_to,msg.as_string())
44 smtp.quit()
45 print 'ok'
總結
以上是生活随笔為你收集整理的在linux中如何实现定时发送邮件到指定邮箱,监测任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下nano中复制粘贴剪切的快捷
- 下一篇: java导出word(带图片)