python发邮件

几种邮件发送的架子是差不多的,这里做点笔记,先写一个配置信息:

email_config = {
    'mailto_list': ['xxxx@xxxx.com', 'xxx@xxx.com'],
    'mail_host': "smtp.xxx.com",  #设置服务器
    'mail_user': "xxxx",    #用户名
    'mail_pass': "111111",   #口令
    'mail_postfix': "xxxx.com",  #发件箱的后缀
}

这里邮件服务器不一定是smtp.xxx.com样式的,具体哪台机器就写哪台,mailto_list是目标邮件列表。

1.先从普通文字邮件开始

import smtplib
from email.mime.text import MIMEText
def send_msg(content):
    mail_user = email_config['mail_user']
    mail_host = email_config['mail_host']
    mail_postfix = email_config['mail_postfix']
    mail_pass = email_config['mail_pass']
    to_list = email_config['mailto_list']

    me = "<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content, _subtype='plain', _charset='utf8')
    msg['Subject'] = '报表'
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.starttls()
        server.login(mail_user, mail_pass)
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False

要有这两个模块

import smtplib
from email.mime.text import MIMEText

邮件的内容设置

msg = MIMEText(content, _subtype='plain', _charset='utf8')

这里content就是文本内容

2.接下来发送html邮件,html其实还是属于Text,所以不用引入新的库,只是将原来的

msg = MIMEText(content, _subtype='plain', _charset='utf8')

改为

msg = MIMEText(content, _subtype='html', _charset='utf8')

content里就是我们的html代码串,以字符串形式给他,收件方拿到的就是解析后的样式。

3.然后发带图片带附件的邮件,道理差不多,要额外引入两个模块

from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

在发这种邮件的时候,文本、html、图片和其他附件可以视为同一种东西,先搞一个msg_root就当是整份邮件,然后一样一样的往msg_root上附东西。

def send_attach_mail(cls, subject, attach_list, content):
        """
        发送html形式邮件
        :param subject:
        :param content:
        :return:
        """
        mail_user = email_config['mail_user']
        mail_host = email_config['mail_host']
        mail_postfix = email_config['mail_postfix']
        mail_pass = email_config['mail_pass']
        to_list = email_config['mailto_list']

        me = "<"+mail_user+"@"+mail_postfix+">"
        msg_root = MIMEMultipart('related')
        msg_root['Subject'] = subject
        msg_root['From'] = me
        msg_root['To'] = ";".join(to_list)

        html_msg = MIMEText(content, _subtype='html', _charset='utf8')
        msg_root.attach(html_msg)

        #构造附件
        try:
            report_path = report_config.get('report_save_path')
        except Exception, e:
            print e
            return False
        for attach in attach_list:
            try:
                att = MIMEText(open(report_path+attach, 'rb').read(), 'base64', 'utf-8')
                att["Content-Type"] = 'application/octet-stream'
                att["Content-Disposition"] = 'attachment; filename="%s"' % attach
                msg_root.attach(att)
            except Exception, e:
                print e
                print 'attach_file_wrong!'
                return False

        try:
            server = smtplib.SMTP()
            server.connect(mail_host)
            server.starttls()
            server.login(mail_user, mail_pass)
            server.sendmail(me, to_list, msg_root.as_string())
            server.close()
            return True
        except Exception, e:
            print str(e)
            return False

比如这个

html_msg = MIMEText(content, _subtype='html', _charset='utf8')
msg_root.attach(html_msg)

就是附一段html。

还有这个

att = MIMEText(open(report_path+attach, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"' % attach
msg_root.attach(att)

就是先构造一个附件,来源可以是文件啊图片啊什么的,然后attach上。
在做项目的时候遇到一个问题,就是我原来的代码直接发送,在qq、360邮箱都能正常发送,换到公司自己的邮箱服务器就不行,报的是

<span style="color:#ff0000">No suitable authentication method found</span>

的错,邮箱服务器认证的问题,查了一下网上说在代码里加上

server.starttls()就解决了,确实解决了,这方面具体为什么还有待深入了解。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值