python发邮件给多个人发送消息_python发送邮件(带附件)、发送给多人、抄送给多人的示例...

python发送邮件(带附件)、发送给多人、抄送给多人的示例

#!/usr/bin/env python

# -*-encoding: utf-8 -*-

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

#image包可以发送图片形式的附件

# from email.mime.image import MIMEImage

# 可以查询文件对应的'Content-Type'

# import mimetypes

# mimetypes.guess_type('c:\\users\\adminstrator\\desktop\\ceshi.xls')

asender = 'aa@aa.com'

#多个收件人用逗号隔开

areceiver = 'bb@bb.com, cc@cc.com'

acc = 'dd@dd.com, eeg@ee.com'

asubject = u'HI'

#阿里云邮箱的smtp服务器

asmtpserver = 'smtp.mxhichina.com'

ausername = 'aa@aa.com'

apassword = '123456'

#下面的to\cc\from最好写上,不然只在sendmail中,可以发送成功,但看不到发件人、收件人信息

msgroot = MIMEMultipart('related')

msgroot['Subject'] = asubject

msgroot['to'] = areceiver

msgroot['Cc'] = acc

msgroot['from']= asender

# MIMEText有三个参数,第一个对应文本内容,第二个对应文本的格式,第三个对应文本编码

thebody = MIMEText(u'Please check the attachment, thanks!', 'plain', 'utf-8')

msgroot.attach(thebody)

# 读取xls文件作为附件,open()要带参数'rb',使文件变成二进制格式,从而使'base64'编码产生作用,否则附件打开乱码

# att = MIMEText(open('C:\\ceshi.xls', 'rb').read(), 'base64', 'GB2312')

# att['Content-Type'] = 'application/vnd.ms-excel'

# att['Content-Disposition'] = 'attachment; filename ="1.xls"'

# 读取xlsx文件作为附件,open()要带参数'rb',使文件变成二进制格式,从而使'base64'编码产生作用,否则附件打开乱码

att = MIMEText(open(u'C:\\ceshi.xlsx', 'rb').read(), 'base64', 'utf-8')

att['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

#下面的filename 等号(=)后面好像不能有空格

attname ='attachment; filename ="123.xlsx"'

att['Content-Disposition'] = attname

msgroot.attach(att)

asmtp = smtplib.SMTP()

asmtp.connect(asmtpserver)

asmtp.login(ausername, apassword)

#发送给多人时,收件人应该以列表形式,areceiver.split把上面的字符串转换成列表

#只要在sendmail中写好发件人、收件人,就可以发送成功

# asmtp.sendmail(asender, areceiver.split(','), msgroot.as_string())

#发送给多人、同时抄送给多人,发送人和抄送人放在同一个列表中

asmtp.sendmail(asender, areceiver.split(',') + acc.split(','), msgroot.as_string())

asmtp.quit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值