搭建网站发送电子邮件:代码发送电子邮件
smtplib 简单邮件传输协议 simble mail transfer protocol library
import smtplib 引入的包
import email 多用户邮件扩充协议
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.mime.image import MIMEImage
- from email.mime.multipart import MIMEMultipart
- #设置主机
- HOST = 'smtp.163.com'
- #设置邮件主题
- Subject = '今天是周末我好开心'
- #发件人的邮箱必须开启smtp协议
- FROM = 'carey0714@163.com'
- #设置收件人的地址 一次性可以发送多人
- To = 'lcx863880337@163.com,306323804@qq.com,carrey0714@163.com'
- #表示内嵌资源的形式把邮件发送给对方
- message = MIMEMultipart('related')
- #发送邮件主题到对方的邮箱中
- #发送文字和图片
- message_text = MIMEText('今天是星期五,很开心','plain','utf-8')
- message_text = MIMEText(<'h1 style="color:blue;font-size=100px">极品火花,失去小小女朋友</h1><img src="http://img.studyofnet.comcid:small">','html','utf-8')
- message_img = MIMETmage(open('1.jpg','rb').read()).add_header('Content-ID',small)
- message.attach(message_text)
- 发送图片的第二种方式
- message_img = MIMEText(open('1.jpg','rb').read(),'base64','utf-8')
- message_img['Content-disposition'] = 'attachment;filename = "happy.jpg"'
- message.attach(message_img)
- #发送文件
- message_xlsx = MIMEText(open('table.xls').read(),'base64','utf-8')
- meaaage_xlsx['Content-dispositon'] = 'attachment;filename= "happy.xlsx"'
- message.attach(message_xlsx)
- message['From'] = FROM
- message['To'] = TO
- message['Subject'] = subject
- #获取简单邮件传输协议的证书
- email_client = smtplib.SMTP_SSL()
- #设置发件人邮箱的域名和端口
- email_client.connect(HOST,'465')
- #密码要写邮箱的授权码
- result = email_client.connect.login(FROM,'mmmqwr678')
- print('登陆结果',result)
- #发送邮件 message是一个对象 as_string把对象转化为字符串
- email_client.sendmail(from_addr=FROM,to_addrs = To.split(','),msg=message.as_string())
- #关闭邮件到客户端
- email_client.close()
,
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com