新的邮件模版

This commit is contained in:
Benny
2023-02-06 22:07:51 +01:00
parent cec312082a
commit 4caa1700f8
4 changed files with 206 additions and 10 deletions

View File

@@ -18,3 +18,4 @@ pymysql==1.0.2
git+https://github.com/tgbot-collection/python-akismet
openpyxl==3.1.0
zhconv==1.4.3
jinja2==3.1.2

View File

@@ -319,9 +319,13 @@ class CommentMongoResource(CommentResource, Mongo):
if user_info:
subject = "[人人影视下载分享站] 你的评论有了新的回复"
pt_content = content.split("</reply>")[-1]
body = f"{username} 您好,<br>你的评论 {parent_comment['content']} 有了新的回复:<br>{pt_content}" \
text = f"你的评论 {parent_comment['content']} 有了新的回复:<br>{pt_content}" \
f"<br>你可以<a href='{link}'>点此链接</a>查看<br><br>请勿回复此邮件"
send_mail(user_info["email"]["address"], subject, body)
context = {
"username": username,
"text": text
}
send_mail(user_info["email"]["address"], subject, context)
return returned
def delete_comment(self, comment_id):
@@ -818,10 +822,13 @@ class UserMongoResource(UserResource, Mongo):
valid_data["email"] = {"verified": False, "address": user_email}
# send email confirm
subject = "[人人影视下载分享站] 请验证你的邮箱"
body = f"{username} 您好,<br>请输入如下验证码完成你的邮箱认证。验证码有效期为24小时。<br>" \
text = f"请输入如下验证码完成你的邮箱认证。验证码有效期为24小时。<br>" \
f"如果您未有此请求,请忽略此邮件。<br><br>验证码: {verify_code}"
send_mail(user_email, subject, body)
context = {
"username": username,
"text": text
}
send_mail(user_email, subject, context)
# 发送成功才设置缓存
redis.set(timeout_key, username, ex=1800)
redis.hset(user_email, mapping={"code": verify_code, "wrong": 0})

View File

@@ -0,0 +1,178 @@
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>人人影视下载站</title>
<style>
@media only screen and (max-width: 620px) {
table.body h1 {
font-size: 28px !important;
margin-bottom: 10px !important;
}
table.body p,
table.body ul,
table.body ol,
table.body td,
table.body span,
table.body a {
font-size: 16px !important;
}
table.body .wrapper,
table.body .article {
padding: 10px !important;
}
table.body .content {
padding: 0 !important;
}
table.body .container {
padding: 0 !important;
width: 100% !important;
}
table.body .main {
border-left-width: 0 !important;
border-radius: 0 !important;
border-right-width: 0 !important;
}
table.body .btn table {
width: 100% !important;
}
table.body .btn a {
width: 100% !important;
}
table.body .img-responsive {
height: auto !important;
max-width: 100% !important;
width: auto !important;
}
}
@media all {
.ExternalClass {
width: 100%;
}
.ExternalClass,
.ExternalClass p,
.ExternalClass span,
.ExternalClass font,
.ExternalClass td,
.ExternalClass div {
line-height: 100%;
}
.apple-link a {
color: inherit !important;
font-family: inherit !important;
font-size: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
text-decoration: none !important;
}
#MessageViewBody a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}
.btn-primary table td:hover {
background-color: #34495e !important;
}
.btn-primary a:hover {
background-color: #34495e !important;
border-color: #34495e !important;
}
}
</style>
</head>
<body style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
<span class="preheader"
style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">人人影视分享站 https://yyets.dmesg.app/ </span>
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body"
style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f6f6f6; width: 100%;"
width="100%" bgcolor="#f6f6f6">
<tr>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top">&nbsp;</td>
<td class="container"
style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; max-width: 580px; padding: 10px; width: 580px; margin: 0 auto;"
width="580" valign="top">
<div class="content"
style="box-sizing: border-box; display: block; margin: 0 auto; max-width: 580px; padding: 10px;">
<!-- START CENTERED WHITE CONTAINER -->
<table role="presentation" class="main"
style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background: #ffffff; border-radius: 3px; width: 100%;"
width="100%">
<!-- START MAIN CONTENT AREA -->
<tr>
<td class="wrapper"
style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;"
valign="top">
<table role="presentation" border="0" cellpadding="0" cellspacing="0"
style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;"
width="100%">
<tr>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;"
valign="top">
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">
你好 {{ username }}</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">
{{ text }}</p>
</td>
</tr>
</table>
</td>
</tr>
<!-- END MAIN CONTENT AREA -->
</table>
<!-- END CENTERED WHITE CONTAINER -->
<!-- START FOOTER -->
<div class="footer" style="clear: both; margin-top: 10px; text-align: center; width: 100%;">
<table role="presentation" border="0" cellpadding="0" cellspacing="0"
style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;"
width="100%">
<tr>
<td class="content-block"
style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; color: #999999; font-size: 12px; text-align: center;"
valign="top" align="center">
<a href="https://yyets.dmesg.app"><span class="apple-link"
style="color: #999999; font-size: 12px; text-align: center;">
人人影视下载分享站</span></a>
<span class="apple-link"
style="color: #999999; font-size: 12px; text-align: center;">
https://yyets.dmesg.app/</span>
<br><br>本邮件由系统自动发送,请勿回复。
</td>
</tr>
</table>
</div>
<!-- END FOOTER -->
</div>
</td>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top">&nbsp;</td>
</tr>
</table>
</body>
</html>

View File

@@ -10,6 +10,7 @@ __author__ = "Benny <benny.think@gmail.com>"
import contextlib
import logging
import os
import pathlib
import smtplib
import time
from datetime import datetime
@@ -20,6 +21,9 @@ from email.utils import formataddr, parseaddr
import pytz
import requests
from akismet import Akismet
from jinja2 import Template
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def ts_date(ts=None):
@@ -33,23 +37,31 @@ def _format_addr(s):
return formataddr((Header(name, 'utf-8').encode(), addr))
def send_mail(to: str, subject: str, body: str):
def generate_body(context):
template = pathlib.Path(__file__).parent.joinpath('templates', "email_template.html")
with open(template) as f:
return Template(f.read()).render(**context)
def send_mail(to: str, subject: str, context: dict):
user = os.getenv("email_user")
password = os.getenv("email_password")
host = os.getenv("email_host") or "localhost"
port = os.getenv("email_port") or "1025" # mailhog
from_addr = os.getenv("from_addr") or "yyets@dmesg.app"
msg = MIMEText(body, 'html', 'utf-8')
msg = MIMEText(generate_body(context), 'html', 'utf-8')
msg['From'] = _format_addr('YYeTs <%s>' % from_addr)
msg['To'] = _format_addr(to)
msg['Subject'] = Header(subject, 'utf-8').encode()
logging.info("logging to mail server...")
if port == "1025":
server = smtplib.SMTP(host, int(port))
else:
server = smtplib.SMTP_SSL(host, int(port))
server.login(user, password)
logging.info("sending email to %s", to)
server.sendmail(from_addr, [to], msg.as_string())
server.quit()
@@ -104,6 +116,4 @@ class Cloudflare:
if __name__ == '__main__':
cf = Cloudflare()
cf.clear_fw()
cf.ban_new_ip("3.3.3.3")
send_mail("benny.think@gmail.com", "主题", {"username": "test123", "text": "测试内容<b>不错</b>"})