java中的接口是类吗
371
2022-09-04
Python--发送邮件(python发送邮件脚本)
Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。
Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
发送纯文本内容
???? 代码如下:
# 引入相应的模块import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.multipart import MIMEBasefrom email import encodersfrom email.utils import parseaddr, formataddrclass SendMail(object): def __init__(self): self.fromUser = "xxxxxx@qq.com" self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码 self.smtpAddr = "smtp.qq.com" # 构造邮件结构 # toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容 def mailStructure(self, toAddrs, subject, msg): # 邮件对象: mailMsg = MIMEMultipart() mailMsg['Subject'] = ("Hello <%s>" % subject) mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser) mailMsg['To'] = ','.join(toAddrs) # 邮件正文是MIMEText : mailMsg.attach(MIMEText(msg, 'html', 'utf-8')) return mailMsg.as_string() # 发送邮件 def sendMail(self, toAddrs, subject, msg): mailMsg_as_string = self.mailStructure(toAddrs, subject, msg) # 连接服务器发送邮件 try: server = smtplib.SMTP_SSL(self.smtpAddr, 465) server.connect(self.smtpAddr) # 连接smtp服务器 server.login(self.fromUser, self.userPasswd) # 登录邮箱 server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件 server.quit() except Exception: print("Error: unable to send email")a = SendMail()a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
发送附件图片
???? 代码如下:
如果Email中要加上附件怎么办?带附件的邮件可以看做包含若干部分的邮件:文本和各个附件本身,所以,可以构造一个MIMEMultipart对象代表邮件本身,然后往里面加上一个MIMEText作为邮件正文,再继续往里面加上表示附件的MIMEBase对象即可:
其实就是打开图片并引入(加上这段内容)
with open(r"C:\Users\YJ\Desktop\favicon.png", "rb") as f: # 设置附件的MIME和文件名,这里是png类型: mime = MIMEBase("image", "png", filename="test.png") # 加上必要的头信息: mime.add_header('Content-Disposition', 'attachment', filename='test.png') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 把附件的内容读进来: mime.set_payload(f.read()) # 用Base64编码: encoders.encode_base64(mime) # 添加到MIMEMultipart: mailMsg.attach(mime)
发送附件图片 (完整代码如下:)
# 引入相应的模块import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.multipart import MIMEBasefrom email import encodersfrom email.utils import parseaddr, formataddrclass SendMail(object): def __init__(self): self.fromUser = "xxxxxx@qq.com" self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码 self.smtpAddr = "smtp.qq.com" # 构造邮件结构 # toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容 def mailStructure(self, toAddrs, subject, msg): # 邮件对象: mailMsg = MIMEMultipart() mailMsg['Subject'] = ("Hello <%s>" % subject) mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser) mailMsg['To'] = ','.join(toAddrs) # 邮件正文是MIMEText : mailMsg.attach(MIMEText(msg, 'html', 'utf-8')) # 发送图片附件, 需要用到附件对象MIMEBase对象, 需要引入from email.mime.multipart import MIMEBase # 添加附件就是加上一个MIMEBase,从本地读取一个图片: with open(r"C:\Users\YJ\Desktop\favicon.png", "rb") as f: # 设置附件的MIME和文件名,这里是png类型: mime = MIMEBase("image", "png", filename="test.png") # 加上必要的头信息: mime.add_header('Content-Disposition', 'attachment', filename='test.png') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 把附件的内容读进来: mime.set_payload(f.read()) # 用Base64编码: encoders.encode_base64(mime) # 添加到MIMEMultipart: mailMsg.attach(mime) return mailMsg.as_string() # 发送邮件 def sendMail(self, toAddrs, subject, msg): mailMsg_as_string = self.mailStructure(toAddrs, subject, msg) # 连接服务器发送邮件 try: server = smtplib.SMTP_SSL(self.smtpAddr, 465) server.connect(self.smtpAddr) # 连接smtp服务器 server.login(self.fromUser, self.userPasswd) # 登录邮箱 server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件 server.quit() except Exception: print("Error: unable to send email")a = SendMail()a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
如果要把一个图片嵌入到邮件正文中怎么做?直接在HTML邮件中链接图片地址行不行?答案是,大部分邮件服务商都会自动屏蔽带有外链的图片,因为不知道这些链接是否指向恶意网站。
要把图片嵌入到邮件正文中,我们只需按照发送附件的方式,先把邮件作为附件添加进去,然后,在HTML中通过引用src="cid:0"就可以把附件作为图片嵌入了。如果有多个图片,给它们依次编号,然后引用不同的cid:x即可。
其实就是将MIMEText 格式改变一下
mailMsg.attach(MIMEText('
将附件图片包含在邮件内容中(完整代码如下:)
# 引入相应的模块import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.multipart import MIMEBasefrom email import encodersfrom email.utils import parseaddr, formataddrclass SendMail(object): def __init__(self): self.fromUser = "xxxxxx@qq.com" self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码 self.smtpAddr = "smtp.qq.com" # 构造邮件结构 # toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容 def mailStructure(self, toAddrs, subject, msg): # 邮件对象: mailMsg = MIMEMultipart() mailMsg['Subject'] = ("Hello <%s>" % subject) mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser) mailMsg['To'] = ','.join(toAddrs) # 邮件正文是MIMEText : # 加载图片并引入到邮件内容中来 mailMsg.attach(MIMEText('
发送纯文本附件
???? 代码如下:
# 引入相应的模块import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.multipart import MIMEBasefrom email import encodersfrom email.utils import parseaddr, formataddrclass SendMail(object): def __init__(self): self.fromUser = "xxxxxx@qq.com" self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码 self.smtpAddr = "smtp.qq.com" # 构造邮件结构 # toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容 def mailStructure(self, toAddrs, subject, msg): # 邮件对象: mailMsg = MIMEMultipart() mailMsg['Subject'] = ("Hello <%s>" % subject) mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser) mailMsg['To'] = ','.join(toAddrs) # 邮件正文是MIMEText : mailMsg.attach(MIMEText(msg, 'html', 'utf-8')) # 发送文件附件, 需要用到附件对象MIMEBase对象, 需要引入from email.mime.multipart import MIMEBase # 添加附件就是加上一个MIMEBase,从本地读取一个文件: with open(r"C:\Users\YJ\Desktop\aaa.txt", "rb") as f: # 设置附件的MIME和文件名,这里是png类型: mime = MIMEBase("txta", "txt", filename="test.txt") # 加上必要的头信息: mime.add_header('Content-Disposition', 'attachment', filename='test.txt') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 把附件的内容读进来: mime.set_payload(f.read()) # 用Base64编码: encoders.encode_base64(mime) # 添加到MIMEMultipart: mailMsg.attach(mime) return mailMsg.as_string() # 发送邮件 def sendMail(self, toAddrs, subject, msg): mailMsg_as_string = self.mailStructure(toAddrs, subject, msg) # 连接服务器发送邮件 try: server = smtplib.SMTP_SSL(self.smtpAddr, 465) server.connect(self.smtpAddr) # 连接smtp服务器 server.login(self.fromUser, self.userPasswd) # 登录邮箱 server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件 server.quit() except Exception: print("Error: unable to send email")a = SendMail()a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
发送excel表格附件
???? 代码如下:
人生是条无名的河,是浅是深都要过; 人生是杯无色的茶,是苦是甜都要喝; 人生是首无畏的歌,是高是低都要唱。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~