Recently some things need to monitor the alarm to send mail, and then find some material on the internet, they wrote a simple email script, mainly using Python's Smtplib module, to share with everyone to see:
Copy Code code as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#导入smtplib和MIMEText
Import Smtplib,sys
From Email.mime.text import Mimetext
def send_mail (sub,content):
#############
#要发给谁, 1 people here.
mailto_list=["Wangwei03@jb51.net"]
#####################
#设置服务器, username, password, and suffix of the mailbox
Mail_host= "mail.gyyx.cn"
Mail_user= "Wangwei03@jb51.net"
Mail_pass= "123456677890"
mail_postfix= "gyyx.cn"
######################
'''''
To_list: Who is it sent to?
Sub: Theme
Content: Contents
Send_mail ("aaa@126.com", "sub", "Content")
'''
Me=mail_user+ "<" +mail_user+ "@" +mail_postfix+ ">"
msg = Mimetext (content,_charset= ' GBK ')
msg[' Subject ' = Sub
msg[' from ' = Me
Msg[' to '] = ";". Join (Mailto_list)
Try
s = smtplib. SMTP ()
S.connect (Mail_host)
S.login (Mail_user,mail_pass)
S.sendmail (Me, Mailto_list, msg.as_string ())
S.close ()
Return True
Except Exception, E:
Print str (e)
Return False
if __name__ = = ' __main__ ':
If Send_mail (U ' This is the Python test mail ', u ' python Send mail '):
Print u ' send success '
Else
Print u ' send failed '