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 ...
随机推荐
- RESTful API设计方法
1.如果已经开始逐步的接触到了RESTful API设计方法的朋友,首先要对HTTP/HTTPS有一个大致的了解,虽然本身和RESTful API没有什么关系.但是对于增加网站的安全性还是十分重要的, ...
- Codeforces 789A Anastasia and pebbles( 水 )
链接:传送门 题意:这个人每次都去公园捡石子,她有两个口袋,每个口袋最多装 k 个石子,公园有 n 种石子,每种石子 w[i] 个,询问最少几天能将石子全部捡完 思路:排个序,尽量每天都多装,如果 k ...
- BZOJ 2333 [SCOI2011]棘手的操作 (可并堆)
码农题.. 很显然除了两个全局操作都能用可并堆完成 全局最大值用个multiset记录,每次合并时搞一搞就行了 注意使用multiset删除元素时 如果直接delete一个值,会把和这个值相同的所有元 ...
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存 参考:<linux编程从入门到精通>,<Linux C程序设计大全>,<unix环境高级编程> ...
- Strtus配置Tomcat出现问题2
启动myeclipse的tomcat6.0有如下提示:The APR based Apache Tomcat Native library which allows optimal performan ...
- 工具-VS2015前端开发工具简介
每个涉及的工具库都给出了入门介绍.基本概念,以及在VS和ASP.NET中的用法.这个白皮书完全就是一个非常难得的前端开发入门手册. 具体涉及到的工具库有: 流行的JS任务执行器:Grunt和Gulp. ...
- socketpair和pipe的区别
http://blog.csdn.net/bingqingsuimeng/article/details/9055499 管道pipe是半双工的,pipe两次才能实现全双工,使得代码复杂.socket ...
- 【Swift】学习笔记(六)——函数
函数 懂编程语言的来说这个是最主要的了,不论什么语言都有函数这个概念.函数就是完毕特定任务的独立代码块. 函数怎么创建: 1.创建一个无參无返回值的函数(实际上全部的函数都有返回值,这个函数返回vo ...
- 0x20 搜索
这里基本就是入门吧. 可达性统计 用bitset搞的判重,发现这东西是真好用哈,空间还小 #include<cstdio> #include<iostream> #includ ...
- ssh tunnel 上网
用DNS隧道实现免费上网 大多数机场.酒店之类场所,当你输入一个网址比如www.google.com时,会弹出一个页面要你输入帐号密码才能上网.这个时候DNS能正确解析,但是上网要付费认证. 可以通过 ...