当前位置:网站首页>Use Python to encapsulate a tool class that sends mail regularly
Use Python to encapsulate a tool class that sends mail regularly
2022-07-28 06:00:00 【Programmer awei】
Import third-party library
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
Tool class
Create a util.py file
class Email():
def __init__(self, mail_user, mail_pwd, mail_host=''):
# No transmission will be judged automatically Can't judge the default QQ mailbox
if mail_host:
self.mail_host = mail_host
elif mail_user.endswith('163.com'):
self.mail_host = 'smtp.163.com'
elif mail_user.endswith(('sina.com', 'sina.cn')):
self.mail_host = 'smtp.163.com'
elif mail_user.endswith('qq.com'):
self.mail_host = 'smtp.qq.com'
elif mail_user.endswith('sohu.com'):
self.mail_host = 'smtp.sohu.com'
else:
self.mail_host = 'smtp.qq.com'
self.mail_user = mail_user
self.is_login = False
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pwd)
self.is_login = True
self.smtpObj = smtpObj
except Exception as e:
print(" Login failed ",e)
def send(self, title, msg, receivers: list, img=''):
"""
send out smtp Mail to recipient
:param title: Email title
:param msg: If you send pictures , Need to be in msg Embedded <img src='cid:xxx'>,xxx Path for picture
:param receivers: The receiver , You can send in multiple recipients , Pass in as a list
:param img: Picture path
:return:
"""
if self.is_login:
message = MIMEMultipart('alternative')
msg_html = MIMEText(msg, 'html', 'utf-8')
message.attach(msg_html)
message['Subject'] = title
message['From'] = self.mail_user
if img:
with open(img, "rb") as f:
msg_img = MIMEImage(f.read())
msg_img.add_header('Content-ID', img)
message.attach(msg_img)
try:
self.smtpObj.sendmail(self.mail_user, receivers, message.as_string())
except Exception as e:
print(" Failed to send mail !", e)
else:
print(' Mailbox is not logged in ')test
1、 Here we use qq Mailbox as an example , First log in to the web version qq mailbox , Click Settings , Find account , open POP3/SMTP service

2、 Get authorization code
3、 Test code
from util import Email
if __name__ == '__main__':
# The first parameter : Email account
# The second parameter : Authorization code
# The third parameter : Can not fill , Will identify according to the email account
email = Email("[email protected]","durkdfhhxxxjoeahe","smtp.qq.com")
email.send(" test ","hahaha<br><img src='cid:./qr_code.png'>",["[email protected]"],'./qr_code.png')4、 test result

Expand
Such written words are not very good , Because email and other information are written in the code , It is not convenient for centralized management and use , So the following words , We can write some mailbox information in the configuration file
边栏推荐
猜你喜欢
随机推荐
浅拷贝、深拷贝区别
第八章 聚合函数
mysql多表查询
Installation and use of flinkx
Sorting and paging, multi table query after class exercise
ES6 --- deconstruction assignment
MySQL trigger
Set scroll bar
数字藏品成文旅产业新热点
预告来袭:【豆冰冰】发.售,现.金.抽.奖等你来拿
mysql视图,存储过程与存储函数
浅谈数字藏品与实体如何相互赋能
DataX installation and use
MySQL练习题50道+答案
项目不报错,正常运行,无法请求到服务
Cookie、Session和Token的区别与联系
数据处理之增删改;约束
Time setting in curd component
第九章 子查询(重点)
Books - smart investors








