1. 简介:
安装 smtplib 模块:pip install smtplib
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
参数说明:
1 host: SMTP服务器主机。 你可以指定主机的ip地址或者域名,可选参数。
2 port: 如果你提供了host 参数, 你需要指定 SMTP 服务使用的端口号,一般情况下SMTP端口号为25。
3 local_hostname: 如果SMTP在你的本机上,你只需要指定服务器地址为 localhost 即可。
python SMTP对象使用sendmail方法发送邮件,语法如下:
1
|
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options] |
参数说明:
1 from_addr: 邮件发送者地址。
2 to_addrs: 字符串列表,邮件发送地址
3 msg: 发送消息内容
这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。
2. 发送普通邮件实例
QQ邮箱如何开启POP3/SMTP服务
https://jingyan.baidu.com/article/425e69e61e9178be15fc168a.html
import smtplib from email.mime.text import MIMEText # 发送普通邮件 # POP3 服务器地址:pop.qq.com # SMTP 服务器地址:smtp.qq.com class SendEmail: def __init__(self,send_user,email_host,email_port,password,user_list,title,message): # 发送邮件的用户 self.send_user = send_user # SMTP 服务器地址:smtp.qq.com self.email_host = email_host # 服务器端口 465 self.email_port = email_port # 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码 self.password = password # 接收邮件的用户 self.user_list = user_list # 邮件内容 self.message = message # 邮件标题 self.title = title def send_email(self): try: user = "菜鸟程序员" + "<" + self.send_user + ">" message = MIMEText(self.message, _subtype='plain', _charset='utf-8') message['Subject'] = self.title message['From'] = user message['To'] = ";".join(self.user_list) server = smtplib.SMTP_SSL() server.connect(self.email_host, self.email_port) server.login(self.send_user, self.password) server.sendmail(user, self.user_list, message.as_string()) server.close() print("发送邮件成功,请查收") except Exception as e: print("发送邮件失败,报错信息:",e) if __name__ == '__main__': send_user = "xxxxxxxxxxxxx@qq.com" email_host = "smtp.qq.com" email_port = 465 password = "xxxxxxxxxxxxxxxxxxxx" user_list = ["xxxxxxxxxxxx@qq.com"] title = "邮件标题" message = "邮件内容" send = SendEmail(send_user,email_host,email_port,password,user_list,title,message) send.send_email()
3.使用Python发送HTML格式的邮件
import smtplib from email.mime.text import MIMEText # 发送HTML格式邮件 # POP3 服务器地址:pop.qq.com # SMTP 服务器地址:smtp.qq.com class SendEmail: def __init__(self,send_user,email_host,email_port,password,user_list,title,message): # 发送邮件的用户 self.send_user = send_user # SMTP 服务器地址:smtp.qq.com self.email_host = email_host # 服务器端口 465 self.email_port = email_port # 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码 self.password = password # 接收邮件的用户 self.user_list = user_list # 邮件内容 self.message = message # 邮件标题 self.title = title def send_email(self): try: user = "菜鸟程序员" + "<" + self.send_user + ">" message = MIMEText(self.message, _subtype='html', _charset='utf-8') message['Subject'] = self.title message['From'] = user message['To'] = ";".join(self.user_list) server = smtplib.SMTP_SSL() server.connect(self.email_host, self.email_port) server.login(self.send_user, self.password) server.sendmail(user, self.user_list, message.as_string()) server.close() print("发送邮件成功,请查收") except Exception as e: print("发送邮件失败,报错信息:",e) if __name__ == '__main__': send_user = "xxxxxxxxxx@qq.com" email_host = "smtp.qq.com" email_port = 465 password = "xxxxxxxxxxxxx" user_list = ["xxxxxxxx@qq.com"] title = "邮件标题" message = """ <p>全是BUG</p> <p><a href="https://www.cnblogs.com/xingxingnbsp/">菜鸟程序员博客</a></p> """ send = SendEmail(send_user,email_host,email_port,password,user_list,title,message) send.send_email()
4.Python 发送带附件的邮件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # 发送带附件的邮件 # POP3 服务器地址:pop.qq.com # SMTP 服务器地址:smtp.qq.com class SendEmail: def __init__(self,send_user,email_host,email_port,password,user_list,title,message,file_path): # 发送邮件的用户 self.send_user = send_user # SMTP 服务器地址:smtp.qq.com self.email_host = email_host # 服务器端口 465 self.email_port = email_port # 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码 self.password = password # 接收邮件的用户 self.user_list = user_list # 邮件内容 self.message = message # 邮件标题 self.title = title # 附件地址 self.file_path = file_path def send_email(self): try: user = "菜鸟程序员" + "<" + self.send_user + ">" # 创建一个带附件的实例 message = MIMEMultipart() message.attach(MIMEText(self.message, 'plain', 'utf-8')) message['Subject'] = self.title message['From'] = user message['To'] = ";".join(self.user_list) for file in self.file_path: # 构造附件 att1 = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename={}'.format(file) message.attach(att1) server = smtplib.SMTP_SSL() server.connect(self.email_host, self.email_port) server.login(self.send_user, self.password) server.sendmail(user, self.user_list, message.as_string()) server.close() print("发送邮件成功,请查收") except Exception as e: print("发送邮件失败,报错信息:",e) if __name__ == '__main__': send_user = "xxxxxxxxxxx@qq.com" email_host = "smtp.qq.com" email_port = 465 password = "xxxxxxxxxxxxxx" user_list = ["xxxxxxxx@qq.com"] title = "邮件标题" message = "发送带附件的邮件" file_path = ["附件1.txt","附件2.txt"] send = SendEmail(send_user,email_host,email_port,password,user_list,title,message,file_path) send.send_email()
5.在 HTML 文本中添加图片
import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart # 发送普通邮件 # POP3 服务器地址:pop.qq.com # SMTP 服务器地址:smtp.qq.com class SendEmail: def __init__(self,send_user,email_host,email_port,password,user_list,title,message): # 发送邮件的用户 self.send_user = send_user # SMTP 服务器地址:smtp.qq.com self.email_host = email_host # 服务器端口 465 self.email_port = email_port # 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码 self.password = password # 接收邮件的用户 self.user_list = user_list # 邮件内容 self.message = message # 邮件标题 self.title = title def send_email(self): try: user = "菜鸟程序员" + "<" + self.send_user + ">" message = MIMEMultipart('related') content = MIMEText(self.message, 'html', 'utf-8') message.attach(content) message['Subject'] = self.title message['From'] = user message['To'] = ";".join(self.user_list) file = open("test.jpg", "rb") img_data = file.read() file.close() img = MIMEImage(img_data) img.add_header('Content-ID', 'image') message.attach(img) server = smtplib.SMTP_SSL() server.connect(self.email_host, self.email_port) server.login(self.send_user, self.password) server.sendmail(user, self.user_list, message.as_string()) server.close() print("发送邮件成功,请查收") except Exception as e: print("发送邮件失败,报错信息:",e) if __name__ == '__main__': send_user = "xxxxxxxx@qq.com" email_host = "smtp.qq.com" email_port = 465 password = "xxxxxxxxxxxxx" user_list = ["xxxxxxx@qq.com"] title = "邮件标题" message = """ <p>全是BUG</p> <p><a href="https://www.cnblogs.com/xingxingnbsp/">菜鸟程序员博客</a></p> <p>图片演示:</p> <p><img src="cid:image"></p> """ send = SendEmail(send_user,email_host,email_port,password,user_list,title,message) send.send_email()