This article mainly introduces the simple sending email script sharing implemented by Python. This article uses the smtplib module to send emails. For more information, see monitoring and sending alerts, then I found some materials on the Internet and wrote a simple email sending script. I used the python smtplib module to share it with you:
The code is as follows:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Import smtplib and MIMEText
Import smtplib, sys
From email. mime. text import MIMEText
Def send_mail (sub, content ):
#############
# To whom, send one person here
Mailto_list = ["wangwei03@jb51.net"]
#####################
# Set the server, user name, password, and mailbox suffix
Mail_host = "mail.gyyx.cn"
Mail_user = "wangwei03@jb51.net"
Mail_pass = "123456677890"
Mail_postfix = "gyyx.cn"
######################
'''''
To_list: to whom
Sub: Topic
Content: content
Send_mail ("aaa@126.com", "sub", "content ")
'''
Me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
Msg = MIMEText (content, _ charset = 'gbk ')
Msg ['subobject'] = 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 t Exception, e:
Print str (e)
Return False
If _ name _ = '_ main __':
If send_mail (u'this is a python test mail', u'python send mail '):
Print u'sent successfully'
Else:
Print u'sending failed'