Python发送邮件

1.邮箱设置

IMAP/SMTP服务        开启

POP3/SMTP服务        开启

2.邮箱地址

POP3服务器: pop.163.com        用于接收

SMTP服务器: smtp.163.com        用于发送

IMAP服务器: imap.163.com        接收/发送

3.引入模块

import smtplib
# 引入发送内容模块
from email.mime.text import MIMEText
# 引入发送附件模块
from email.mime.multipart import MIMEMultipart
# 邮箱地址
mail_host = 'smtp.163.com'
# 用户名 邮箱
mail_user = '******@163.com'
# 密码 授权密码 
mail_pass = '************'

# 设置发送邮箱
sender = '****@163.com'
# 接收邮件邮箱(要发送的人)
receivers = ['xxxx@qq.com']

# 设置文本
# massage = MIMEText('您好','plain','utf-8')
# MIMEMultipart 可传输附件,可传输文本
massage = MIMEMultipart()
massage['Subject'] = '每日报告'
massage['From'] = sender
massage['To'] = receivers[0]
content = MIMEText('请注意查看附件','plain','utf-8')
# 将文本加入massage
massage.attach(content)

with open('./tests/daily_report.pdf','rb') as f:
    att = MIMEText(f.read(),'base64','utf-8')
    att['Content-Type'] = 'application/octet-stream'
    att['Content-Disposition'] = 'attachment;filename="daily_report.pdf"'
    massage.attach(att)
try:
    smtpObj = smtplib.SMTP_SSL(mail_host,465)
    smtpObj.set_debuglevel(1)
    smtpObj.login(mail_user,mail_pass)
    smtpObj.sendmail(sender,receivers,massage.as_string())
    smtpObj.quit()
    print("邮件发送成功")
except Exception as e:
    print('error',e)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值