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

博敏网站建设深圳专业建网站

博敏网站建设,深圳专业建网站,网站 打赏功能,wordpress上传的图片在文章目录 一、SMTP协议邮件准备二、smtplib模块1.使用smtplib封装一个邮件类2.发送邮件 补充 一、SMTP协议邮件准备 需要一个smtp服务器 二、smtplib模块 smtplib模块是python自带的模块 1.使用smtplib封装一个邮件类 import smtplib import logging # 加入日志&#xff…

文章目录

  • 一、SMTP协议邮件准备
  • 二、smtplib模块
    • 1.使用smtplib封装一个邮件类
    • 2.发送邮件
  • 补充


一、SMTP协议邮件准备

需要一个smtp服务器

二、smtplib模块

smtplib模块是python自带的模块

1.使用smtplib封装一个邮件类

import smtplib
import logging  # 加入日志,保持这个好习惯,使用前需要配置class Smtp(object):  # 类名自定义def __init__(self, sender, pwd, server_url, server_port):"""sender 登录SMTP服务器的用户名 注意除了使用用户名和密码还可使用密钥pwd 登录SMTP服务器的密码server_url SMTP服务器域名或者IPserver_port SMTP服务所开放的端口"""self.sender = senderself.smtp = smtplib.SMTP(server_url, server_port, timeout=5)  # 创建smtp对象self.smtp.starttls()  # 开始TSL加密self.smtp.login(sender, pwd)  # 使用密码或者密码获取登陆凭证def mail(self, *args):  # 发送邮件"""俩个参数依次为:收信邮箱、内容"""self.smtp.sendmail(self.sender, args[0], args[1])def stop_mail(self):  # 邮件发送完毕后需要关闭连接,释放资源"""停止smtp"""self.smtp.quit()

2.发送邮件

代码如下(示例):

import multiprocessing  # 进程模块
import smtplib
from email.header import Header 
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMETextdef send_mail(send_user, send_pwd, server_url, server_port, to_mail, msg):  # msg为smtp的html内容对象,需要完成复杂邮件内容时需要用到MIMEMultipart,此处的msg便为MIMEMultipart(复杂内容为,附件、表格、图片等)smtp_send = Smtp(send_user, send_pwd, server_url, server_port)  # 户名、密码、域名、端口smtp_send.mail(to_mail, msg.as_string()) # 收信人邮箱、bytes数据(直接放字符串内容也行,smtplib会自动转化)smtp_send.stop_mail() # 发信完毕,停止连接

补充

完成上诉代码就能发送邮件了,但STMP协议发送邮件时会阻塞主进程以及网络,所以当你要异步发送smtp协议的邮件时使用线程、协程时无法达成异步效果的,需要开启新的进程

附上完整代码以及进程发送邮件

我代码中邮箱的表格样式:
在这里插入图片描述

import logging
import multiprocessing
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMETexterror_logger = logging.getLogger('celery-error')class Smtp(object):  # 类名自定义def __init__(self, sender, pwd, server_url, server_port):"""sender登录SMTP服务器的用户名 一般为发信邮箱 注意除了使用用户名和密码还可使用密钥pwd 登录SMTP服务器的密码server_url SMTP服务器域名或者IPserver_port SMTP服务所开放的端口"""self.sender = senderself.smtp = smtplib.SMTP(server_url, server_port, timeout=5)  # 创建smtp对象self.smtp.starttls()  # 开始TSL加密self.smtp.login(sender, pwd)  # 使用密码或者密码获取登陆凭证def mail(self, *args):  # 发送邮件"""俩个参数依次为:收信邮箱、内容"""self.smtp.sendmail(self.sender, args[0], args[1])def stop_mail(self):  # 邮件发送完毕后需要关闭连接,释放资源"""停止smtp"""self.smtp.quit()def send_mail(send_user, send_pwd, server_url, server_port, to_mail, msg):  # msg为smtp的html内容对象,需要完成复杂邮件内容时需要用到MIMEMultipart,此处的msg便为MIMEMultipart(复杂内容为,附件、表格、图片等)smtp_send = Smtp(send_user, send_pwd, server_url, server_port)  # 户名、密码、域名、端口smtp_send.mail(to_mail, msg.as_string()) # 收信人邮箱、bytes数据(直接放字符串内容也行,smtplib会自动转化)smtp_send.stop_mail() # 发信完毕,停止连接def mail_to_user(**kwargs):user_obj = Users.objects.filter(username=kwargs.get('touser')).first()try:if user_obj:# 收信人邮箱send_user = 'kkk@qq.com'# 创建MIMEMultipart对象,此处我生成了一个table表格msg = MIMEMultipart()# 设置邮件主题和发件人信息msg['From'] = key_obj.accessSecretmsg['To'] = send_usermsg['Subject'] = Header("测试邮件", 'utf-8')# 创建表格内容的列表table_data = ["测试类型", "测试状态", "申请人", "2023-9-26"]# 初始化表格HTML字符串table_html = """<table><tr><th>类型</th><th>状态</th><th>申请人</th><th>预约完成日期</th></tr>"""# 构建表格内容的HTML字符串 如果此处内容时多行的可以循坏拼接这一段table_html += f"""<tr><td>{table_data[0]}</td><td>{table_data[1]}</td><td>{table_data[2]}</td><td>{table_data[3]}</td></tr>"""table_html += "</table>"# 将表格内容html加入到html代码中html_content = """<html><style type="text/css">table thead, table tr {border-top-width: 1px;border-top-style: solid;border-top-color: rgb(211, 202, 221);}table {border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: rgb(211, 202, 221);}table td, table th {padding: 5px 10px;font-size: 12px;font-family: Verdana;color: rgb(95, 74, 121);}table tr:nth-child(even) {background: rgb(223, 216, 232)}table tr:nth-child(odd) {background: #FFF}</style><body>""" + table_html + """</body></html>"""# 将表格内容作为MIMEText对象添加到邮件中table_part = MIMEText(html_content, 'html', 'utf-8')msg.attach(table_part)process = multiprocessing.Process(target=send_mail, args=("admin@qq.com", "password", "127.0.0.1", 8043, "test@qq.com", msg))process.start()except Exception as e:print(str(e))error_logger.error('邮件发送错误 ' + str(e))

文章转载自:

http://2CtNhNZP.btpLL.cn
http://pkqAUEcC.btpLL.cn
http://Y0g9piab.btpLL.cn
http://z2dCozdy.btpLL.cn
http://gJoVqMFH.btpLL.cn
http://UfFmyg1i.btpLL.cn
http://xdo0Q78n.btpLL.cn
http://oQjiYHcA.btpLL.cn
http://0e91nMuW.btpLL.cn
http://NIUB7zGp.btpLL.cn
http://gPkTqnob.btpLL.cn
http://VQtowzqA.btpLL.cn
http://mI5X0aAk.btpLL.cn
http://PrztRM3r.btpLL.cn
http://0fTKFAA5.btpLL.cn
http://70u6tnOZ.btpLL.cn
http://Fi3S1M5O.btpLL.cn
http://XtY630MN.btpLL.cn
http://WUyuD4Vv.btpLL.cn
http://0K3uP06O.btpLL.cn
http://d0vsLmkc.btpLL.cn
http://hpAC0pWf.btpLL.cn
http://eTVTVoDN.btpLL.cn
http://m1alD8Fx.btpLL.cn
http://IL3QCuy2.btpLL.cn
http://5ZvB72wf.btpLL.cn
http://1XdnpX62.btpLL.cn
http://hfqychBP.btpLL.cn
http://xGi5QEJh.btpLL.cn
http://CGizvK4T.btpLL.cn
http://www.dtcms.com/wzjs/734892.html

相关文章:

  • 安娜尔返利机器人怎么做网站网站备案中更名
  • wordpress 建站对比网站空间运行挂机宝
  • 自己做网站 有名6怎么把wordpress后台设置成中文
  • 两个网站链接如何做做网站需要的法律知识
  • 做网站设置时间网站建设中html
  • 各种网站名称大全天津网站搭建
  • 中国电力建设集团网站群wordpress用户分组
  • 唐山哪个公司可以制作网站成品网页大全下载
  • 中国万网域名查询瀑布流网站如何seo
  • 如何做网站界面免费行情软件app网站大全入口
  • fqapps com网站怎么做邯郸最新工程项目公示
  • 济宁网站建设招聘网页设计实用教程
  • 网站功能介绍wordpress 游戏 模板下载
  • 珍岛外贸网站建设wordpress 浮窗音乐
  • 做网站服务器价格多少合适扁平化设计网站代码
  • 网站建设咨询有客诚信网站建设咨询南宁网站建设南宁
  • 无锡优化网站价格学校网站建设审批
  • 做紧固件上什么网站网络促销策略
  • 基本网站建设语言移动wap站点
  • 阿里云建站百度收录吗泰州网站优化公司
  • 北京市建设工程造价管理协会网站网站推广销售腾讯会员被告怎么办
  • 老渔哥网站建设公司网站策划书是什么
  • 网站单页别人是怎么做的wordpress 媒体库设置
  • 如何让百度快速收录网站文章阿里巴巴官网首页
  • 网站正能量视频不懂我意思吧wordpress中的全站链接怎么改
  • 宁波网站建设-中国互联网站开发要注意的漏洞
  • 瑞丽住建局网站品牌建设的六个步骤
  • iis 创建网站wordpress 下载按钮
  • 网站广告下悬浮代码怎么做企业网站设计制作服务
  • 建立网站需要什么条件wordpress4.5.2主题