SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
参考:https://www.cnblogs.com/ysocean/p/7653252.html
https://blog.csdn.net/qq_42543250/article/details/81586663
https://www.cnblogs.com/captainmeng/p/7683395.html
http://www.cnblogs.com/yufeihlf/p/5726619.html
在第三方客户端使用stmp之前需要开启邮箱中的POP3/SMTP/IMAP服务,并获取到授权码。这里使用是163邮箱
纯文本邮件
1 import smtplib 2 from email.header import Header 3 from email.mime.text import MIMEText 4 5 # 第三方 SMTP 服务 6 mail_host = "smtp.163.com" # SMTP服务器 7 mail_user = "*******@163.com" # 用户名 8 mail_pass = "*******" # 授权密码,非登录密码 9 10 sender = '********@163.com' # 发件人邮箱(最好写全, 不然会失败) 11 receivers = ['********@163.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 12 13 content = '这里是测试文本!' #测试文本 14 title = 'smtp邮件测试' # 邮件主题 15 16 #内置参数执行 17 def sendEmail(): 18 19 message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码 20 message['From'] = "{}".format(sender) 21 message['To'] = ",".join(receivers) 22 message['Subject'] = title 23 24 try: 25 smtpObj = smtplib.SMTP_SSL(mail_host) # 启用SSL发信, 端口默认是25(可以不写) 26 smtpObj.login(mail_user, mail_pass) # 登录验证 27 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 28 print("mail has been send successfully.") 29 except smtplib.SMTPException as e: 30 print(e) 31 32 #参数传入执行 33 def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content): 34 email_client = smtplib.SMTP(SMTP_host) 35 email_client.login(from_account, from_passwd) 36 # create msg 37 msg = MIMEText(content, 'plain', 'utf-8') 38 msg['Subject'] = Header(subject, 'utf-8') # subject 39 msg['From'] = from_account 40 msg['To'] = to_account 41 email_client.sendmail(from_account, to_account, msg.as_string()) 42 43 email_client.quit() 44 45 if __name__ == '__main__': 46 sendEmail() 47 48
插入图片(非附件形式)
1 # coding: utf-8 2 3 import smtplib 4 from email.mime.multipart import MIMEMultipart 5 from email.mime.text import MIMEText 6 from email.mime.base import MIMEBase 7 from email import encoders 8 9 mail_host = 'smtp.163.com' 10 receivers = ['******@163.com'] 11 receiver = ','.join(receivers) 12 sender = '******@163.com' 13 password = '*****' 14 15 msg = MIMEMultipart('alternative') 16 msg['From'] = sender 17 msg['To'] = receiver 18 msg['Subject'] = '主题' 19 #插入一个html格式 20 msg.attach(MIMEText('<html><body><h1>Hello</h1>' + 21 '<p><img src="cid:0"></p>' + 22 '</body></html>', 'html', 'utf-8')) 23 24 file_path = r'E:stmp_testimg.png' 25 with open(file_path, 'rb') as f: 26 mm = MIMEBase('image', 'png', filename='b.png') # 设置附件的MIME和文件名,这里是png类型: 27 mm.add_header('Content-Disposition', 'attachment', filename='b.jpg') #加上必要的头信息 28 mm.add_header('Content-ID', '<0>') #头信息 29 mm.add_header('X-Attachment-Id', '0') #头信息 30 mm.set_payload(f.read()) # 把附件的内容读进来 31 encoders.encode_base64(mm) 32 msg.attach(mm) 33 34 try: 35 server = smtplib.SMTP() 36 # 加上这句就可以打印出所有的log,更方便我们定位问题 37 server.set_debuglevel(1) 38 server.connect(mail_host, 25) 39 server.login(sender, password) 40 server.sendmail(sender, receiver,msg.as_string()) 41 server.close() 42 print ('Success') 43 except smtplib.SMTPException: 44 print( 'Error')
直接在HTML邮件中链接图片地址是不行的。引文大部分邮件服务商都会自动屏蔽带有外链的图片,因为不知道这些链接是否指向恶意网站。
只需要在HTML中通过引用src="cid:0"就可以把附件作为图片嵌入了。如果有多个图片,给它们依次编号,然后引用不同的cid:x即可。