smtp-email-sender

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SMTP Email Sender

SMTP Email Sender

通过 SMTP 协议发送邮件,支持 Gmail、Outlook、企业邮箱等。
Send emails via the SMTP protocol, supporting Gmail, Outlook, corporate mailboxes, and more.

前置要求

Prerequisites

1. Gmail 用户

1. Gmail Users

如果使用 Gmail,需要:
  1. 启用两步验证
  2. 创建应用专用密码(App Password)
If using Gmail, you need to:
  1. Enable two-step verification
  2. Create an App Password

2. Outlook/Hotmail 用户

2. Outlook/Hotmail Users

  1. 启用两步验证
  2. 创建应用密码:https://account.microsoft.com/security
  3. 或使用普通密码(如果允许)
  1. Enable two-step verification
  2. Create an app password: https://account.microsoft.com/security
  3. Or use your regular password (if allowed)

3. 企业邮箱用户

3. Corporate Mailbox Users

联系 IT 部门获取:
  • SMTP 服务器地址
  • SMTP 端口(通常 587 或 465)
  • 是否需要 SSL/TLS
Contact your IT department to obtain:
  • SMTP server address
  • SMTP port (usually 587 or 465)
  • Whether SSL/TLS is required

配置

Configuration

.env
文件中添加以下环境变量:
bash
undefined
Add the following environment variables to the
.env
file:
bash
undefined

SMTP 配置

SMTP Configuration

SMTP_SERVER=smtp.gmail.com SMTP_PORT=587 SMTP_USERNAME=your_email@gmail.com SMTP_PASSWORD=your_app_password # Gmail 使用应用专用密码 SMTP_USE_TLS=true

或者首次使用时运行配置脚本。
SMTP_SERVER=smtp.gmail.com SMTP_PORT=587 SMTP_USERNAME=your_email@gmail.com SMTP_PASSWORD=your_app_password # Use App Password for Gmail SMTP_USE_TLS=true

Or run the configuration script on first use.

使用方法

Usage

基本用法

Basic Usage

调用
send_email.py
脚本:
bash
python scripts/send_email.py \
  --to recipient@example.com \
  --subject "邮件主题" \
  --body "邮件正文"
Call the
send_email.py
script:
bash
python scripts/send_email.py \
  --to recipient@example.com \
  --subject "Email Subject" \
  --body "Email Content"

完整参数

Full Parameters

参数必需说明
--to
收件人邮箱(多个用逗号分隔)
--subject
邮件主题
--body
邮件正文
--cc
抄送邮箱(多个用逗号分隔)
--bcc
密送邮箱(多个用逗号分隔)
--attachment
附件路径(多个用逗号分隔)
--is_html
正文是否为 HTML 格式(默认 false)
--from_name
发件人显示名称
ParameterRequiredDescription
--to
YesRecipient email address (separate multiple with commas)
--subject
YesEmail subject
--body
YesEmail body
--cc
NoCC recipient email address (separate multiple with commas)
--bcc
NoBCC recipient email address (separate multiple with commas)
--attachment
NoAttachment path (separate multiple with commas)
--is_html
NoWhether the body is in HTML format (default false)
--from_name
NoSender display name

示例

Examples

发送简单邮件
bash
python scripts/send_email.py \
  --to friend@example.com \
  --subject "周末聚会" \
  --body "这周末有空吗?一起吃饭吧!"
发送 HTML 邮件带附件
bash
python scripts/send_email.py \
  --to boss@company.com \
  --subject "项目报告" \
  --body "<h1>项目进度报告</h1><p>详见附件...</p>" \
  --is_html true \
  --attachment "report.pdf,chart.xlsx" \
  --from_name "张三"
发送给多人
bash
python scripts/send_email.py \
  --to "alice@example.com,bob@example.com" \
  --cc "manager@example.com" \
  --subject "会议纪要" \
  --body "今天的会议纪要如下..."
Send a Simple Email:
bash
python scripts/send_email.py \
  --to friend@example.com \
  --subject "Weekend Gathering" \
  --body "Are you free this weekend? Let's have dinner together!"
Send HTML Email with Attachments:
bash
python scripts/send_email.py \
  --to boss@company.com \
  --subject "Project Report" \
  --body "<h1>Project Progress Report</h1><p>Please see attachments for details...</p>" \
  --is_html true \
  --attachment "report.pdf,chart.xlsx" \
  --from_name "Zhang San"
Send to Multiple Recipients:
bash
python scripts/send_email.py \
  --to "alice@example.com,bob@example.com" \
  --cc "manager@example.com" \
  --subject "Meeting Minutes" \
  --body "Below are the minutes from today's meeting..."

支持的 SMTP 配置

Supported SMTP Configurations

Gmail

Gmail

SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USE_TLS=true
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USE_TLS=true

Outlook/Hotmail

Outlook/Hotmail

SMTP_SERVER=smtp-mail.outlook.com
SMTP_PORT=587
SMTP_USE_TLS=true
SMTP_SERVER=smtp-mail.outlook.com
SMTP_PORT=587
SMTP_USE_TLS=true

QQ 邮箱

QQ Mail

SMTP_SERVER=smtp.qq.com
SMTP_PORT=587
SMTP_USE_TLS=true
SMTP_SERVER=smtp.qq.com
SMTP_PORT=587
SMTP_USE_TLS=true

163 邮箱

163 Mail

SMTP_SERVER=smtp.163.com
SMTP_PORT=587
SMTP_USE_TLS=true
SMTP_SERVER=smtp.163.com
SMTP_PORT=587
SMTP_USE_TLS=true

企业邮箱(示例)

Corporate Mailbox (Example)

SMTP_SERVER=smtp.company.com
SMTP_PORT=587
SMTP_USE_TLS=true
SMTP_SERVER=smtp.company.com
SMTP_PORT=587
SMTP_USE_TLS=true

常见问题

FAQs

1. 认证失败

1. Authentication Failure

Gmail
  • 确保启用了两步验证
  • 使用应用专用密码,不是普通密码
  • 检查是否开启了"不够安全的应用"访问(不推荐)
Outlook
  • 检查是否需要应用密码
  • 确认 SMTP 地址正确
Gmail:
  • Ensure two-step verification is enabled
  • Use the App Password, not your regular password
  • Check if "Less secure app access" is enabled (not recommended)
Outlook:
  • Check if an app password is required
  • Confirm the SMTP address is correct

2. 连接超时

2. Connection Timeout

  • 检查防火墙设置
  • 尝试端口 465(SSL)代替 587(TLS)
  • 确认 SMTP 服务器地址正确
  • Check firewall settings
  • Try port 465 (SSL) instead of 587 (TLS)
  • Confirm the SMTP server address is correct

3. 附件太大

3. Attachment Too Large

  • Gmail 限制 25MB
  • Outlook 限制 20MB
  • 大文件建议使用云盘链接
  • Gmail limits attachments to 25MB
  • Outlook limits attachments to 20MB
  • For large files, it's recommended to use cloud storage links

安全建议

Security Recommendations

  1. 永远不要在代码中硬编码密码
  2. 使用环境变量或加密的配置文件
  3. 定期更换应用专用密码
  4. 不要在公共网络使用 SMTP 发送敏感信息
  1. Never hardcode passwords in your code
  2. Use environment variables or encrypted configuration files
  3. Regularly update your App Password
  4. Do not send sensitive information via SMTP on public networks

故障排除

Troubleshooting

运行测试脚本验证配置:
bash
python scripts/test_smtp.py
如果测试失败,检查:
  1. .env
    文件配置是否正确
  2. 网络连接是否正常
  3. 邮箱账号密码是否正确
  4. 防火墙是否阻止 SMTP 端口
Run the test script to verify the configuration:
bash
python scripts/test_smtp.py
If the test fails, check:
  1. Whether the configuration in the
    .env
    file is correct
  2. Whether the network connection is normal
  3. Whether the email account and password are correct
  4. Whether the firewall blocks the SMTP port