Simple email alert function

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib #Send mail, simply encapsulate smtp
from email.mime.text import MIMEText #Construct mail
from email.header import Header #Mail header

sender = . . . @qq.com '#sender
receivers = ['. . . . @net-east.com'] # To receive emails, it can be set to your QQ mailbox or other mailboxes
Cc=[sender]# Cc to yourself

# Three parameters: the first is the text content, the second is plain Text format, the third utf-8 encoding is set
message = MIMEText('Python mail sending test...', 'plain', 'utf-8')
message['From'] = Header("Rookie Tutorial", ' utf-8') # header and encoding for sender
message['To'] = Header("Test", 'utf-8') # header and encoding for receiver

subject = 'Python SMTP mail test' #Mail subject
message['Subject'] = Header(subject, 'utf-8') #Mail subject encapsulation
s=smtplib.
s.connect("smtp.qq.com") #Connect to the server
s.login("[email protected]", "...authorization code") #Login server

try: #Handler exception
    s.sendmail( sender, receivers, message.as_string()) #Use the sendmial method to send mail
    print ("mail sent successfully")
except smtplib.SMTPException:
    print ("Error: Unable to send mail")

 

For detailed tutorials, please refer to: http://www.runoob.com/python/python-email.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324964888&siteId=291194637