当前位置: 首页 > news >正文

零基础制作公司网站教程成都seo整站

零基础制作公司网站教程,成都seo整站,编程网课哪家好,建设网站的命令在项目中,会使用到发送邮件的功能。不同框架的配置可能有所不同,直接写一个不依赖框架配置的邮件发送模块。 使用的功能: 1、可以发送给多个邮箱 2、可以实现抄送多个邮箱 3、可以添加多个文件附件 一、不使用多线程 import smtplib from…

在项目中,会使用到发送邮件的功能。不同框架的配置可能有所不同,直接写一个不依赖框架配置的邮件发送模块。
使用的功能:

1、可以发送给多个邮箱

2、可以实现抄送多个邮箱

3、可以添加多个文件附件

一、不使用多线程

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.header import Header
from email.utils import formataddr
from threading import Threadclass Email:EMAIL_HOST = 'smtp.qq.com'  # 如果是 163 改成 smtp.163.comEMAIL_PORT = 587  # qq邮箱服务的端口:465、587EMAIL_HOST_USER = "xxx@qq.com"  # 发送邮件的邮箱帐号EMAIL_HOST_PASSWORD = "xxx"  # 授权码,各邮箱的设置中启用smtp服务时获取FROM_EMAIL_USER = EMAIL_HOST_USER  # 收件人显示发件人的邮箱FROM_EMAIL_NAME = '广州市xxxx技术有限公司'  # 设置发件人的名字,在项目使用,一般是用公司名字def __init__(self, receiver_email, subject, message=None, html_message=None, file_path=None,cc_email=None):self.receiver_email = receiver_email # 接收人邮件self.cc_email = cc_email #邮件抄送人self.subject = subject  # 邮件的主题self.message = message  # 邮件文本内容self.html_message = html_message  # 邮件的html内容  注意:文本内容与html同时有的时候,html覆盖文本内容self.file_path = file_path  # 附件文件的路径,str 或 [str,]self.email = MIMEMultipart() #创建一个邮件对象,发送的邮件的信息都设置到这个对象中#接收人邮件:必须有if type(receiver_email) in [list,str,tuple]:if type(receiver_email) in [list,tuple]:self.receiver_email = ','.join(receiver_email)else:raise Exception('邮件抄送人必须是字符串、列表或元组形式')#抄送人邮件: 可选if cc_email:if type(cc_email) in [list,str,tuple]:if type(cc_email) in [list,tuple]:self.cc_email = ','.join(cc_email)else:raise Exception('邮件抄送人必须是字符串、列表或元组形式')def start(self):# 1、设置邮箱对象的发送人、接收人和主题self.email['From'] = formataddr((self.FROM_EMAIL_NAME, self.FROM_EMAIL_USER))self.email['To'] = self.receiver_emailself.email['Subject'] = Header(self.subject, 'utf-8')self.email['Cc'] = self.cc_email# 2、设置内容,如果同时attach了html信息和文本信息,文本信息会被转成附件文件。if not self.message and not self.html_message:raise Exception('发送的邮件每月携带任何内容...')if self.html_message:self.email.attach(MIMEText(self.html_message, 'html', 'utf-8'))else:self.email.attach(MIMEText(self.message, 'plain', 'utf-8'))# 3、邮箱的附件文件if self.file_path != None:if isinstance(self.file_path, str):self.file_path = [self.file_path]elif isinstance(self.file_path, list):passelse:raise Exception('邮件携带的附件,格式是文件字符串路径,就列表套文件字符串路径,不能是其他格式')for path in self.file_path:with open(path, "rb") as attachment:part = MIMEBase("application", "octet-stream")part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header("Content-Disposition",f"attachment; filename= {path}",)self.email.attach(part)# 4、连接smtp服务器,发送邮件with smtplib.SMTP(self.EMAIL_HOST, self.EMAIL_PORT) as server:# QQ邮箱的服务器域名和端口server.starttls()  # 使用 TLS 加密连接# 使用qq邮箱登录:邮箱号和授权码server.login(self.EMAIL_HOST_USER, self.EMAIL_HOST_PASSWORD)# 发送邮件,发送的是邮箱对象server.send_message(self.email)if __name__ == '__main__':receiver_email = ['xxx@163.com','yyy@qq.com'] #接收人邮件账号cc_email = ['xxxx@163.com','yyy@qq.com'] #抄送人的邮件账号subject = '登录验证码' #主题message = '您的验证码是234523,10分钟中内有效,若非本人操作请忽视...' #文本信息file_path = ['1.txt', '2.txt'] #同目录下创建1.txt和2.txt 文件,测试附件文件html_message = '<h1>登录验证码,5分钟内有效</h1><p>您的验证码是:546783</p><p>注意:若非本人操作,建议删除邮件防止泄漏验证码信息</p>' #html信息,有这个使用这个不使用文本信息#非多线程发送email_obj_2 = Email(receiver_email=receiver_email,subject=subject,message=message,file_path=file_path,html_message=html_message,cc_email=cc_email)email_obj_2.start()

二、使用线程封装

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.header import Header
from email.utils import formataddr
from threading import Threadclass ThreadEmail(Thread):EMAIL_HOST = 'smtp.qq.com'  # 如果是 163 改成 smtp.163.comEMAIL_PORT = 587  # qq邮箱服务的端口:465、587EMAIL_HOST_USER = "xxx@qq.com"  # 发送邮件的邮箱帐号EMAIL_HOST_PASSWORD = "xxx"  # 授权码,各邮箱的设置中启用smtp服务时获取FROM_EMAIL_USER = EMAIL_HOST_USER  # 收件人显示发件人的邮箱FROM_EMAIL_NAME = '广州市xxxx技术有限公司'#设置发件人的名字,在项目使用,一般是用公司名字def __init__(self, receiver_email, subject, message=None, html_message=None, file_path=None,cc_email=None):super().__init__()self.receiver_email = receiver_email  # 接收人邮件self.cc_email = cc_email  # 邮件抄送人self.subject = subject  # 邮件的主题self.message = message  # 邮件文本内容self.html_message = html_message  # 邮件的html内容  注意:文本内容与html同时有的时候,html覆盖文本内容self.file_path = file_path  # 附件文件的路径,str 或 [str,]self.email = MIMEMultipart()  # 创建一个邮件对象,给这个对象添加 邮件需要的各种信息# 接收人邮件:必须有if type(receiver_email) in [list, str, tuple]:if type(receiver_email) in [list, tuple]:self.receiver_email = ','.join(receiver_email)else:raise Exception('邮件抄送人必须是字符串、列表或元组形式')# 抄送人邮件: 可选if cc_email:if type(cc_email) in [list, str, tuple]:if type(cc_email) in [list, tuple]:self.cc_email = ','.join(cc_email)else:raise Exception('邮件抄送人必须是字符串、列表或元组形式')def run(self) -> None:# 1、设置邮箱对象的发送人、接收人和主题self.email['From'] = formataddr((self.FROM_EMAIL_NAME, self.FROM_EMAIL_USER))self.email['To'] = self.receiver_emailself.email['Subject'] = Header(self.subject, 'utf-8')self.email['Cc'] = self.cc_email# 2、设置内容,如果同时attach了html信息和文本信息,文本信息会被转成附件文件。if not self.message and not self.html_message:raise Exception('发送的邮件每月携带任何内容...')if self.html_message:self.email.attach(MIMEText(self.html_message, 'html', 'utf-8'))else:self.email.attach(MIMEText(self.message, 'plain', 'utf-8'))# 3、邮箱的附件文件if self.file_path != None:if isinstance(self.file_path, str):self.file_path = [self.file_path]elif isinstance(self.file_path, list):passelse:raise Exception('邮件携带的附件,格式是文件字符串路径,就列表套文件字符串路径,不能是其他格式')for path in self.file_path:with open(path, "rb") as attachment:part = MIMEBase("application", "octet-stream")part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header("Content-Disposition",f"attachment; filename= {path}",)self.email.attach(part)# 4、连接smtp服务器,发送邮件with smtplib.SMTP(self.EMAIL_HOST, self.EMAIL_PORT) as server:# QQ邮箱的服务器域名和端口server.starttls()  # 使用 TLS 加密连接# 使用qq邮箱登录:邮箱号和授权码server.login(self.EMAIL_HOST_USER, self.EMAIL_HOST_PASSWORD)# 发送邮件,发送的是邮箱对象server.send_message(self.email)if __name__ == '__main__':receiver_email = ['xxx@163.com','yyy@qq.com'] #接收人邮件账号cc_email = ['xxxx@163.com','yyy@qq.com'] #抄送人的邮件账号subject = '登录验证码' #主题message = '您的验证码是234523,10分钟中内有效,若非本人操作请忽视...' #文本信息file_path = ['1.txt', '2.txt'] #同目录下创建1.txt和2.txt 文件,测试附件文件html_message = '<h1>登录验证码,5分钟内有效</h1><p>您的验证码是:546783</p><p>注意:若非本人操作,建议删除邮件防止泄漏验证码信息</p>' #html信息,有这个使用这个不使用文本信息#多线程发送email_obj = ThreadEmail(receiver_email=receiver_email,subject=subject,message=message,file_path=file_path,html_message=html_message,cc_email=cc_email)email_obj.start()

三、通用性

1、对于脚本代码,也可以直接使用

2、在框架中,也可以直接使用

http://www.ds6.com.cn/news/51779.html

相关文章:

  • 安阳网站建设哪家便宜seo专业培训机构
  • 网站建设与维护 实验报告心得宣传网站站点最有效的方式是
  • 独立网站平台有哪些做网站
  • wordpress多域名更改seo还能赚钱吗
  • 唐山公司网站建设 中企动力免费制作小程序平台
  • 提升网站建设互联网推广是什么意思
  • 稻草人网站开发百度网站名称和网址
  • 网站盈利方式购买一个网站域名需要多少钱
  • 建设自己的企业网站需要什么资料google安卓手机下载
  • 郑州上市企业网站建设数据分析培训机构哪家好
  • 做毕业论文设计的网站品牌网络seo方案外包
  • 个人网站有哪些举例23岁老牌网站
  • 外贸公司论坛seo搜索排名优化
  • vs网站开发参考文献哪些网站有友情链接
  • 猪八戒兼职网站怎么做任务赚钱网站seo整站优化
  • 阿里云的网站建设好不好智慧软文
  • 特种证书查询入口seo快速排名的方法
  • 在线下单网站怎么做厦门seo顾问屈兴东
  • wordpress一定是主页吗seo网站有优化培训吗
  • 织梦系统网站首页upcache=1如何做网站营销
  • 怎么写网站规划方案最近国际时事热点事件
  • 网站建设哪家公司免费站推广网站2022
  • 香港做网站公司哪家好如何做营销活动
  • wordpress网站跳转百度关键词刷排名软件
  • 怎么用wordpress做网站网络营销推广服务商
  • 做网站客户需求seo策略主要包括
  • 做网站的多少钱商丘网络推广外包
  • 建一个网站要多少钱百度云搜索引擎入口官方
  • 懂做游戏钓鱼网站的seo顾问是干什么
  • 邹城外贸网站建设优化培训学校