Mailpit 是一个轻量的开源邮箱服务测试项目。能实现收发邮件功能,支持API和web管理界面。

Docker 安装 非常简单

version: '2.4'

services:
  mailpit:
    image: axllent/mailpit
    container_name: mailpit
    restart: unless-stopped
    volumes:
      - ./data:/data
      - /etc/letsencrypt/archive/mail.maifeipin.com:/etc/letsencrypt/archive/mail.maifeipin.com:ro
    ports:
      - 8025:8025
      - 1025:1025 
    environment:
      MP_MAX_MESSAGES: 5000
      MP_DATABASE: /data/mailpit.db
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1
      MP_UI_AUTH_FILE: /data/authfile  #web管理入口用户:密码 存放文件路径

测试

  1. 使用 telnet
    输入: telnet mail.maifeipin.com 1025
    回显: 220 da6c299f0693 Mailpit ESMTP Service ready

说明服务正常,再AI生成 发送的邮件格式内容:

telnet mail.maifeipin.com 1025
Trying 42.192.232.191...
Connected to mail.maifeipin.com.
Escape character is '^]'.
220 da6c299f0693 Mailpit ESMTP Service ready
Wellcome program code mail
MAIL FROM: <program@code.com>
RCPT TO: <Admin@maifeipin.com>
DATA
Subject: Program code mail

This is a test email sent through Program code.

.
QUIT500 5.5.2 Syntax error, command unrecognized
250 2.1.0 Ok
250 2.1.5 Ok
354 Start mail input; end with <CR><LF>.<CR><LF>
250 2.0.0 Ok: queued as nB8tycHeSc4zEtYR5XiUwf

221 2.0.0 da6c299f0693 Mailpit ESMTP Service closing transmission channel
Connection closed by foreign host.

  1. 使用python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 配置 SMTP 服务器
smtp_server = 'mail.maifeipin.com'  # Mailpit 服务器地址
smtp_port = 1025  # Mailpit 的 SMTP 端口

# 发件人和收件人
sender_email = 'test@example.com'
receiver_email = 'test@maifeipin.com'

# 邮件内容
subject = "Test Email from Python"
body = "This is a test email sent via Python script using smtplib."

# 创建 MIME 对象
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))

# 连接到 Mailpit SMTP 服务器并发送邮件
try:
    # 建立连接
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.set_debuglevel(1)  # 打印调试信息
    
    # 发送邮件
    server.sendmail(sender_email, receiver_email, msg.as_string())
    print(f"Email successfully sent to {receiver_email}")
    
except Exception as e:
    print(f"Error: {e}")
    
finally:
    # 关闭连接
    server.quit()

执行 python3 send_mail.py

python3 send_mail.py
send: 'ehlo [127.0.1.1]\r\n'
reply: b'250-da6c299f0693 greets [127.0.1.1]\r\n'
reply: b'250-SIZE 0\r\n'
reply: b'250-AUTH LOGIN PLAIN\r\n'
reply: b'250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: b'da6c299f0693 greets [127.0.1.1]\nSIZE 0\nAUTH LOGIN PLAIN\nENHANCEDSTATUSCODES'
send: 'mail FROM:<test@example.com> size=424\r\n'
reply: b'250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: b'2.1.0 Ok'
send: 'rcpt TO:<test@maifeipin.com>\r\n'
reply: b'250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: b'2.1.5 Ok'
send: 'data\r\n'
reply: b'354 Start mail input; end with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'Start mail input; end with <CR><LF>.<CR><LF>'
data: (354, b'Start mail input; end with <CR><LF>.<CR><LF>')
send: b'Content-Type: multipart/mixed; boundary="===============5188777605039164489=="\r\nMIME-Version: 1.0\r\nFrom: test@example.com\r\nTo: test@maifeipin.com\r\nSubject: Test Email from Python\r\n\r\n--===============5188777605039164489==\r\nContent-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\nThis is a test email sent via Python script using smtplib.\r\n--===============5188777605039164489==--\r\n.\r\n'
reply: b'250 2.0.0 Ok: queued as Q9AfHqwhvJ5D5X6MT3rM9F\r\n'
reply: retcode (250); Msg: b'2.0.0 Ok: queued as Q9AfHqwhvJ5D5X6MT3rM9F'
data: (250, b'2.0.0 Ok: queued as Q9AfHqwhvJ5D5X6MT3rM9F')
Email successfully sent to test@maifeipin.com
send: 'quit\r\n'
reply: b'221 2.0.0 da6c299f0693 Mailpit ESMTP Service closing transmission channel\r\n'
reply: retcode (221); Msg: b'2.0.0 da6c299f0693 Mailpit ESMTP Service closing transmission channel'

  1. 在web界面查看测试结果
  • 给管理Web界面配置域名(可选),用IP:8025也可以的。然后使用docker中配置的账号登录
    image-1744424873377

使用 Mailpit API 管理邮件

  1. 获取所有邮件列表    List messages    get /api/v1/messages  
    
    image-1744426001929
  2. 获取单个邮件内容 
    
       curl -X GET "https://mail.maifeipin.com/api/v1/message/Q9AfHqwhvJ5D5X6MT3rM9F" -H 'accept: application/json' -u 'user:password'
    
    返回json数据
    
         {"ID":"Q9AfHqwhvJ5D5X6MT3rM9F","MessageID":"7WuwRsFRZtuRo7sG78U2Nr@mailpit","From":{"Name":"","Address":"test@example.com"},"To":[{"Name":"","Address":"test@maifeipin.com"}],"Cc":[],"Bcc":[],"ReplyTo":[],"ReturnPath":"test@example.com","Subject":"Test Email from Python","ListUnsubscribe":{"Header":"","Links":[],"Errors":"","HeaderPost":""},"Date":"2025-04-12T02:12:44.122Z","Tags":[],"Text":"This is a test email sent via Python script using smtplib.","HTML":"","Size":674,"Inline":[],"Attachments":[]}