python发送邮件

我们在开发程序的时候,有时候需要开发一些自动化的任务,执行完之后,将结果自动的发送一份邮件,python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下:

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 # 构造邮件内容
 4 email_host = 'smtp.163.com'  # 邮箱smtp地址
 5 email_user = 'xxxxxxx@163.com'  # 发送者账号
 6 email_pwd = 'xxxxxxx'  # 发送者密码
 7 maillist = 'xxxxxxx@qq.com'
 8 # 收件人邮箱,多个账号的话,用英文逗号隔开
 9 me = email_user
10 msg = MIMEText('hello')  # 邮件内容
11 # mintest实例化了,实例化给msg
12 msg['Subject'] = '我是潘美丽'  # 邮件主题
13 msg['From'] = me  # 发送者账号
14 msg['To'] = maillist  # 接收者账号列表
15 smtp = smtplib.SMTP(email_host, port=25)  # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
16 smtp.login(email_user, email_pwd)  # 发送者的邮箱账号,密码
17 smtp.sendmail(me,maillist,msg.as_string())
18 # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
19 smtp.quit()  # 发送完毕后退出smtp
20 print('email send success.')

下面是发送带附件的邮件:

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 # 构造邮件内容
 4 from email.mime.multipart import MIMEMultipart
 5 # 发带附件的邮件用的
 6 email_host = 'smtp.163.com'  # 邮箱地址
 7 email_user = 'xxxxxxx@163.com'  # 发送者账号
 8 email_pwd = 'xxxxxxx'  # 发送者密码
 9 maillist = 'xxxxxxx@qq.com'
10 # 收件人邮箱,多个账号的话,用英文逗号隔开
11 new_masg=MIMEMultipart()
12 # 构建了一个能发附件的对象
13 new_masg.attach(MIMEText('此邮件中有附件'))
14 # 邮件内容
15 
16 me = email_user
17 # msg = MIMEText('hello')  # 邮件内容
18 # mintest实例化了,实例化给msg
19 new_masg['Subject'] = '我是潘美丽'  # 邮件主题
20 new_masg['From'] = me  # 发送者账号
21 new_masg['To'] = maillist  # 接收者账号列表
22 att=MIMEText(open('a.txt').read())
23 att["Content-Type"] = 'application/octet-stream'
24 #发送附件就得这么写
25 att["Content-Disposition"] = 'attachment; filename="a.txt"'
26 new_masg.attach(att)
27 smtp = smtplib.SMTP(email_host, port=25)  # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
28 smtp.login(email_user, email_pwd)  # 发送者的邮箱账号,密码
29 smtp.sendmail(me,maillist,new_masg.as_string())
30 # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
31 smtp.quit()  # 发送完毕后退出smtp
32 print('email send success.')

当然,我们可以封装成一个函数,使用的时候,直接调用函数,传入邮箱账号密码,收件人,发件人,标题,附件和内容即可。

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 # 构造邮件内容
 4 from email.mime.multipart import MIMEMultipart
 5 class SendMail(object):
 6     def __init__(self,username,passwd,recv,content,title,file=None,
 7                  email_host='smtp.163.com',port=25):
 8         self.username = username     # 发送者账号
 9         self.passwd = passwd         # 发送者密码
10         self.recv = recv             # 接收邮件账号
11         self.title = title           # 邮件主题
12         self.file = file             # 附件
13         self.content = content       # 邮件内容
14         self.email_host = email_host     #smtp
15         self.port = port
16     def send_mail(self):
17         # 一下是发送附件内容
18         msg=MIMEMultipart()
19         if self.file:
20             att = MIMEText(open(self.file).read())          # 附件有可能不存在,所以需要try一下
21             att["Content-Type"] = 'application/octet-stream'
22             # 发送附件就得这么写
23             att["Content-Disposition"] = 'attachment; filename="%s"'%self.file
24 
25             msg.attach(att)
26         # 以下是写正文
27         msg.attach(MIMEText(self.content))
28         msg['Subject'] = self.title # 邮件主题
29         msg['From'] = self.username  # 发送者账号
30         msg['To'] = self.recv  # 接收者账号列表
31         self.smtp=smtplib.SMTP(self.email_host,port=self.port)
32         # 发送邮件服务的对象
33         self.smtp.login(self.username,self.passwd)
34         self.smtp.sendmail(self.username,self.recv,msg.as_string())
35     def __del__(self):
36         self.smtp.quit()
37 
38 m=SendMail(
39     username='xxxxxx@163.com',passwd='xxxxxxx',recv='xxxxxx@qq.com',
40     title='这个是类发的邮件',content='潘阳是大美女',email_host='smtp.163.com')
41 m.send_mail()

这个类中没有做异常处理,小伙伴们可以自己做一下异常处理.

原文地址:https://www.cnblogs.com/panpan0301/p/7215743.html