How to implement scheduled mail to a designated mailbox in Linux, monitoring tasks

Source: Internet
Author: User

1#!/usr/bin/env python2#-*-Coding=utf-8-*-3ImportSmtplib4From Email.mime.textImportMimetext5ImportThreading6ImportTime, DateTime78 mailto_list=["[Email protected]"]#It's the other person's mailbox.9#-----------QQ Mailbox Send settings----------------------Ten mail_server=""#QQ Mailbox As an example, the inside is the QQ mailbox service, replaced by other mailboxes need to change the serviceMail_user=""#This is the account of QQ mailboxmail_pass=""#If it is other can directly fill in the password, if the use of QQ, or the mailbox does not open the service, will remind you to open the following link13#QQ mailbox need to go official open service: (to_list, Sub, content):msg = Mimetext (content,‘Plain‘,‘Utf-8‘)msg["Accept-language"]="Zh-cn"msg["Accept-charset"]="Iso-8859-1,utf-8"msg[‘Subject'] =Submsg[‘From'] =Mail_usermsg[‘To'] =";". Join (To_list)21stTry:Server =Smtplib. SMTP ()23Server.connect (Mail_server)24Server.starttls ()25Server.login (Mail_user, Mail_pass)26Server.sendmail (Mail_user, To_list, msg.as_string ())27Server.close ()28ReturnTrue29ExceptException, E:30PrintSTR (e)31ReturnFalse3233DefGetDate ():34Return str (Datetime.datetime.utcfromtimestamp (Time.time ()) +datetime.timedelta (hours=8))3536DefSend_warning_mail (title, info):PNS Nowtime =GetDate ()38Try:t = Threading. Thread (Target=send_mail, args= (mailto_list, title, str (nowtime) +"|" +STR (info)))40T.start ()41ExceptPass # Send_mail (mailto_list, "MySQL exception", info)  if __name__ = = '__main__< c10> ':send_warning_mail ("Thisis the title", "\nthis is content")46 # Print 111                

This is able to add attachments to the email sent:
1#!/usr/bin/env python
4From email. MimetextImportMimetext
5From email. MimemultipartImportMimemultipart
6From email. MimebaseImportMimebase
7From emailImportEncoders
9 mail_body=‘Hello, this is the mail content‘
Ten mail_from=‘‘#Sender's mailbox
mail_to=[‘‘]#Recipient mailbox
12#Constructs a Mimemultipart object as a root container
msg=Mimemultipart ()
15#Constructs a Mimetext object as a message display and attaches to the root container
body=Mimetext (Mail_body)
17Msg.attach (body)
19#Constructs a Mimebase object as the file attachment content and attaches to the root container
20#Equivalent to the following 3 lines
21st#Contype = ' Application/octet-stream '
22#MainType, subtype = contype.split ('/', 1)
23#Part = Mimebase (MainType, subtype)
Part = Mimebase (‘Application‘,‘Octet-stream‘)
26#Read the contents of the file and format it, where the file is in the current directory, you can also specify directories such as: Open (R '/tmp/123.txt ', ' RB ')
Part.set_payload (Open (‘123.txt‘,‘Rb‘). Read ())
28Encoders.encode_base64 (part)
29## Set Attachment header
Part.add_header (‘Content-disposition‘,‘Attachment Filename= ""‘)
31Msg.attach (part)
33#Set Root Container properties
msg[‘Subject']=‘This is the title‘
Approx. msg[‘To']=‘;‘. Join (MAIL_TO)
Panax Notoginseng msg[‘Date']=time.strftime (‘%a,%d%b%Y%h:%m:%s%z‘)
38#The full text after formatting is msg.as_string ()
39#Send mail with SMTP
smtp=smtplib. SMTP ()
41 smtp.connect ( ") # service, if the mailbox is 163, fill in
42 smtp.login ( ' sender's mailbox Span style= "COLOR: #800000", " ' sender's password ' )
43 Smtp.sendmail (Mail_from,mail_to,msg.as_string ())
44 smtp.quit ()
45 print Span style= "COLOR: #800000" > ' ok "

How to implement scheduled messages to specified mailboxes in Linux, monitoring tasks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.