python email 模块 运维用处

2019-03-27  本文已影响0人  弗兰克万岁

主要用于检测服务器状况并发送确认信息给自己

import smtplib
from email.mime.text import MIMEText

sender = 'xxxx@qq.com'   #发送人邮箱
passwd = 'lkugmgywydhfff' #发送人邮箱授权码
receivers = 'xxxx@qq.com' #收件人邮箱

subject = 'python发邮件测试' #主题
content = '这是我使用python smtplib模块和email模块自动发送的邮件'    #正文

msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['TO'] = receivers

try:
    s = smtplib.SMTP_SSL('smtp.qq.com',465)
    s.login(sender,passwd)
    s.sendmail(sender,receivers,msg.as_string())
    print('发送成功')

except Exception:
    print('发送失败')
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = 'xxxx@qq.com' #发件邮箱
passwd = 'lkugmgywydhfff' #发送人邮箱授权码
receivers = 'xxxx@qq.com'   #收件邮箱

subject = 'python发邮Html邮件测试' #主题

content = """         #内容,HTML格式
<p>Python 邮件发送测试...</p>
<p><a href="http://www.baidu.com">这是一个链接</a></p>
"""

msg = MIMEText(content,'html','utf-8')
# msg['Subject'] = subject
msg['Subject'] = Header(subject,'utf-8')
# msg['From'] = sender
msg['From'] = Header('大傻子','utf-8')
# msg['To'] = receivers
msg['To'] = Header('二愣子','utf-8')
try:
    s = smtplib.SMTP_SSL('smtp.qq.com',465)
    s.login(sender,passwd)
    s.sendmail(sender,receivers,msg.as_string())
    print('Send Success')

except:
    print('Send Failure')
上一篇 下一篇

猜你喜欢

热点阅读