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="Smtp.qq.com"#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: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=100125614DefSend_mail (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
2#Coding=utf-8
3ImportSmtplib
4From email. MimetextImportMimetext
5From email. MimemultipartImportMimemultipart
6From email. MimebaseImportMimebase
7From emailImportEncoders
8ImportTime
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 ()
14
15#Constructs a Mimetext object as a message display and attaches to the root container
body=Mimetext (Mail_body)
17Msg.attach (body)
18
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‘)
25
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= "Herb.zip"‘)
31Msg.attach (part)
32
33#Set Root Container properties
msg[‘Subject']=‘This is the title‘
msg[‘From']=Mail_from
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 smtp.163.com
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