使用Python发邮件

本文介绍了使用Python的yagmail库和标准库email发送邮件的方法。yagmail提供简洁的API,而email库则更稳定。示例展示了如何创建带附件的邮件,设置抄送和密送,并通过SMTP发送。邮件内容包括文本和Excel附件,支持163邮箱和Outlook邮箱的SMTP配置。
摘要由CSDN通过智能技术生成

一、Yagmail

yagmail 是一个第三方库,相比于标准库来说,会比较简洁:

import yagmail
ya = yagmail.SMTP(user = 'sender_email', password = 'sender_pwd',
host =' smtp.partner.outlook.cn',port =25, smtp_starttls = True, smtp_ssl = False)
#prepareing ur content
#using the method 'send' ,要发送给多个人 用list
ya.send(to = 'receiver_email', cc = ''cc_email',bcc = 'bcc_email',
subject = 'ur subject', contents = cont,attachments = att)

二、email

虽然三方库很好用,但是有时候会出一些bug;所以标准库更加稳定:

#1.导入相关的包
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

#创建一个带附件的邮件消息对象
msg = MIMEMultipart()

#创建文本内容
content = '附件中有本月数据请查收'
text_content = MIMEText(content, 'plain', 'utf-8')
# 将文本内容添加到邮件消息对象中
msg.attach(text_content)

#读取文件并将文件作为附件添加到邮件消息对象中
with open('data.xlsx','rb') as f:
	file = MIMEApplication(f.read())
	file.add_header('Content-Disposition','attachment',filename=('gbk','','data.xlsx'))
	msg.attach(file)

#创建SMTP对象
server= smtplib.SMTP('smtp.163.com')
    # 如果使用世纪互联运营的outlook邮箱,它使用的是SMTP TLS加密
    # smtper = SMTP('smtp.partner.outlook.cn', 587)
    # smtper.starttls()
sender = 'example@163.com'  # 发件人
receivers = ['example@qq.com', 'example@icloud.com']  # 收件人
cc = ['example@qq.com', 'example@icloud.com']  # 抄送人
bcc = ['example@qq.com', 'example@icloud.com']  # 密送人 
msg['From'] = sender
msg['To'] = ';'.join(receivers)
msg['Cc'] = ';'.join(cc)
msg['Bcc'] = ';'.join(bcc)
mas['Subject'] = 'test'
server.login(sender,'password')
server.sendmail(sender,receiver+cc+bcc,msg['Subject'],msg.as_string())
server.quit() #断开smtp连接
print('successful')


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值