python 邮件发送

python 邮件发送

import smtplib
import time
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from activeMq.log import log_mail
from activeMq.monitor.acars_analysis.enterprise_wechat import wechat

log = log_mail.get_log()

def send_email(filename):
    # 发送邮箱服务器
    smtpserver = 'redair.icoremail.net'
    # 发送邮箱用户名密码

    #邮箱密码会过期,3个月过期一次
    user = '******@qq.comn'
    password = '*****'
    # 发送和接收邮箱
    receives = ['******@qq.comn']

    msg = MIMEMultipart()  # 创建一个带附件的实例
    subject = Header("欢迎光临", 'utf-8').encode()
    msg["Subject"] = subject  # 指定邮件主题
    msg["From"] = user  # 邮件发送人
    msg["To"] = ','.join(receives)  # 邮件接收人,如果存在多个收件人,可用join连接
    msg.attach(MIMEText('附件为,请查收!,',
                        _subtype='html', _charset='utf-8'))#邮件内容
    part = MIMEApplication(open(filename, 'rb').read())

    #截取文件名
    file = filename.split('/')[-1]
    part.add_header('Content-Disposition', 'attachment', filename=file)
    msg.attach(part)
    nowTime = time.strftime('%Y-%m-%d %H:%M:%S')


    try:
        s = smtplib.SMTP(smtpserver)  # 连接smtp邮件服务器,端口默认是25
        s.login(user, password)  # 登陆服务器
        s.sendmail(user, receives, msg.as_string())  # 发送邮件
        log.info("%s,文件%s发送到邮件%s:" % (nowTime, file, receives))
        print("%s,文件%s发送到邮件%s:" % (nowTime, file, receives))
        s.close()

    except Exception as e:
        print(nowTime, "邮件发送失败:" + str(e))
        log.info("%s,文件%s发送到邮件%s失败,异常为%s:" % (nowTime, file, receives, str(e)))
        msg = nowTime + "给PTMD.AIRTRAVEL@Honeywell.com邮件发送失败,异常为" + str(e)
        wechat.wechat_msg(msg).person()

if __name__ == '__main__':
    send_email('E:/RedAir/code/redair/Acars/Acars_monitor/activeMq/acarsXLS/acars 20200718172955_20200718112955.xls')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪异的bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值