faker smtp server
import os
import asyncio
import logging
import base64
from email import message_from_bytes
from email.message import Message
from datetime import datetime import aiosmtpd
from aiosmtpd.controller import Controller
from aiosmtpd.smtp import SMTP as Server, syntax
from jinja2 import Template mail_path = "mails"
hostname = "0.0.0.0"
port = 8025 html = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>email</title>
</head>
<body>
<div><span>发件人: </span><span>{{ from_addr|e }}</span></div>
<div><span>收件人: </span><span>{{ to_addr|e }}</span></div>
<div><span>主题: </span><span>{{ subject }}</span></div>
<div>
{{ payload }}
</div>
</body>
</html>
""" class ExampleHandler:
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
envelope.rcpt_tos.append(address)
return "250 OK" async def handle_DATA(self, server, session, envelope: aiosmtpd.smtp.Envelope):
message: Message = message_from_bytes(envelope.content)
message_info = await self.parse_message(message)
template = Template(html)
if not os.path.exists(mail_path):
os.makedirs(mail_path)
with open(os.path.join(mail_path, f"mail_{datetime.now().strftime('%Y-%m-%d-%H_%M_%S_%f')[:-3]}.html"), "w") as f:
f.write(template.render(message_info))
return "250 Message accepted for delivery" def get(self, message, item):
value = message.get(item)
try:
value = self.to_true_str(value)
except Exception:
pass
return value async def parse_message(self, message: Message):
self.charset = message.get_content_charset() or "utf-8"
payload = message.get_payload()
subject = self.get(message, "Subject")
from_addr = self.get(message, "From")
to_addr = self.get(message, "To")
try:
if isinstance(payload, (list, tuple)):
payload = self.parse_payload(payload)
except Exception:
pass
return {"subject": subject, "payload": payload, "from_addr": from_addr, "to_addr": to_addr} def parse_payload(self, payload):
# todo 暂时不处理附件的问题,目前仅处理 text/html 与 text/plain 共存的情况
data = None
for item in payload:
if isinstance(item, Message):
data = item.get_payload()
if item.get_content_type == "text/html":
break try:
# 测试发现 html 有概率是转 base64
data = self.to_true_str(data)
except Exception:
pass return data def to_true_str(self, raw: str, charset=None):
if raw.startswith("=?"):
tmp_list = raw.split("?")
if len(tmp_list) > 2:
raw = tmp_list[-2]
charset = tmp_list[1]
else:
charset = self.charset
return base64.b64decode(raw).decode(charset) async def handle_EHLO(self, *args, **kwargs):
return """\
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail
250-STARTTLS
250-SMTPUTF8
250 8BITMIME""" class MyServer(Server): @syntax("AUTH PLAIN")
@asyncio.coroutine
def smtp_AUTH(self, PLAIN, *args, **kwargs):
yield from self.push("235 auth successfully") @syntax("EHLO hostname")
async def smtp_EHLO(self, hostname):
status = await self._call_handler_hook("EHLO", hostname)
self.session.host_name = hostname
await self.push(status) class MyController(Controller):
def factory(self):
return MyServer(self.handler) async def amain(loop):
controller = MyController(ExampleHandler(), hostname=hostname, port=port)
controller.start() if __name__ == "__main__":
logging.basicConfig(level=logging.ERROR)
loop = asyncio.get_event_loop()
loop.create_task(amain(loop=loop))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
faker smtp server的更多相关文章
- SSRS1:配置SMTP Server发送mail
为了使用SSRS发送mail,必须为Reporting service配置SMTP Server. 1,在Reporting Service Configuration Manager中配置Email ...
- Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference
Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify ...
- Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email
Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email. (文档 I ...
- phpmailer的SMTP ERROR: Failed to connect to server: 10
请问,我在win7上学习使用phpmailer时,出现这种错误怎么处理啊? SMTP ERROR: Failed to connect to server: (0) SMTP connect() fa ...
- Reporting Service 配置SMTP和设置订阅出现的异常
SSRS能够按照schedule,以mail的形式发送report,这是通过设置subscription report来实现的. 1,发送mail需要在SSRS中配置SMTP Server,如果没有R ...
- P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1
P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1 May ...
- How to Use Telnet to Test SMTP Communication
Topic Last Modified: 2005-05-24 Telnet is an extremely useful tool for troubleshooting issues relate ...
- 在CI中集成phpmailer,方便使用SMTP发送邮件
直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功 ...
- Understanding and Managing SMTP Virtual Servers
Simple Mail Transfer Protocol (SMTP) Service Overview The Simple Mail Transfer Protocol (SMTP) servi ...
随机推荐
- JavaScript正则表达式总结
同学们,今天给大家带来一篇正则表达式的总结,老规矩,先说下我们为什么要用正则?正则要用在什么地方? (个人理解的,大神轻喷) 正则很多时候要用于字符串操作,比如我们要把一个字符串里面的空格删除啊,替换 ...
- 深入理解Java中的锁
转载:https://www.jianshu.com/p/2eb5ad8da4dc Java中的锁 常见的锁有synchronized.volatile.偏向锁.轻量级锁.重量级锁 1.synchro ...
- Python-基础-day3
基础数据类型 1.什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1 ...
- 深入理解B/S与C/S架构
首先来介绍一下B/S与C/S架构 C/S架构简要介绍 在了解什么是B/S架构之前,我们有必要了解一下什么是C/S架构: C/S架构是第一种比较早的软件架构,主要用于局域网内.也叫 客户机/服务器模式. ...
- 如何在GitHub部署自己的个人网站
前段时间在B站学习了怎样做一个静态网页(up主是 表严肃 ),朋友问我他能不能访问我的这个静态网页,我说还没把它挂到网上.今天看见一篇文章说可以在GitHub部署自己的个人网站,心血来潮,想做一个玩 ...
- NYIST 119 士兵杀敌(三)
士兵杀敌(三)时间限制:2000 ms | 内存限制:65535 KB难度:5 描述南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比较,计算出 ...
- CF789A. Anastasia and pebbles
/* CF789A. Anastasia and pebbles http://codeforces.com/contest/789/problem/A 水题 题意:有两个背包,每次分别可取k个物品, ...
- telnet允许root用户登录
默认情况下,linux不允许root用户以telnet方式登录linux主机,若要允许root用户登录,可采取以下3种方法之一: 1.修改login文件 redhat中对于远程登录的限制体现在/ ...
- [SharePoint2010开发入门经典]12、SPS2010安全管理
本章概要: 1.SPS中的用户授权 2.理解服务器场解决方案和沙箱解决方案的不同 3.理解表单验证和声明验证
- POJ 1777
一道好题. 由算术基本定理,知: 那么,对于上式的每个因子值只能是2^M的形式.取第一个式子为例,通过分解因式出(1+p^2)=2^k知,a只能为1. 于是对于p只能是梅森素数.而且每个梅森素数只能出 ...