• 欢迎访问笔记阁,实用快捷文章教程,推荐使用Chrome浏览器和360极速浏览器访问本网站
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏笔记阁吧

python快速发送邮件

运维干货 feko 7个月前 (04-17) 400次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

文章目录

python发邮件

  • 测试环境:python2.7、python3.7
vi  python_sed_email.py
# -*- encoding:utf-8 -*-
import sys
import smtplib
import email
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

def email_content(platform,contents):
    mm = MIMEMultipart('related')
    subject_content = """%s""" % platform
    mm["From"] = "youremail<youremail@163.com>"
    mm["To"] = "youremail<youremail@163.com>,xxx<xxx@qq.com>" # 默认发送给自己,xxx添加其他收件人
    mm["Subject"] = Header(subject_content,'utf-8')
    with open("{}".format(contents),"r") as f:
        contents = f.read()
    body_content = """%s""" % contents
    message_text = MIMEText(body_content,"plain","utf-8")
    mm.attach(message_text)
    return mm

def email_sed(mm):
    mail_host = "smtp.163.com"
    mail_sender = "youremail@163.com"
    mail_license = "xxxxxxxxx"   # 授权码
    mail_receivers = ["youremail<youremail@163.com>","xxx<xxx@qq.com>"]  # xxx添加收件人
    stp = smtplib.SMTP()
    stp.connect(mail_host, 25)
    stp.login(mail_sender,mail_license)
    stp.sendmail(mail_sender, mail_receivers, mm.as_string())
    print("邮件发送成功")
    stp.quit()

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print('Usage:python {} "[xxx-每日备份] 邮件通知"  /root/backup_20200414.log'.format(sys.argv[0]))
        sys.exit(2)
    platform = sys.argv[1]
    contents = sys.argv[2]
    mm = email_content(platform,contents)
    email_sed(mm)

笔记阁 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python快速发送邮件
喜欢 (1)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址