smtp-email-sender
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSMTP 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,需要:
- 启用两步验证
- 创建应用专用密码(App Password)
- 访问:https://myaccount.google.com/apppasswords
- 选择"邮件"和应用名称
- 复制生成的 16 位密码
If using Gmail, you need to:
- Enable two-step verification
- Create an App Password
- Visit: https://myaccount.google.com/apppasswords
- Select "Mail" and the app name
- Copy the generated 16-digit password
2. Outlook/Hotmail 用户
2. Outlook/Hotmail Users
- 启用两步验证
- 创建应用密码:https://account.microsoft.com/security
- 或使用普通密码(如果允许)
- Enable two-step verification
- Create an app password: https://account.microsoft.com/security
- 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
在 文件中添加以下环境变量:
.envbash
undefinedAdd the following environment variables to the file:
.envbash
undefinedSMTP 配置
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.pybash
python scripts/send_email.py \
--to recipient@example.com \
--subject "邮件主题" \
--body "邮件正文"Call the script:
send_email.pybash
python scripts/send_email.py \
--to recipient@example.com \
--subject "Email Subject" \
--body "Email Content"完整参数
Full Parameters
| 参数 | 必需 | 说明 |
|---|---|---|
| 是 | 收件人邮箱(多个用逗号分隔) |
| 是 | 邮件主题 |
| 是 | 邮件正文 |
| 否 | 抄送邮箱(多个用逗号分隔) |
| 否 | 密送邮箱(多个用逗号分隔) |
| 否 | 附件路径(多个用逗号分隔) |
| 否 | 正文是否为 HTML 格式(默认 false) |
| 否 | 发件人显示名称 |
| Parameter | Required | Description |
|---|---|---|
| Yes | Recipient email address (separate multiple with commas) |
| Yes | Email subject |
| Yes | Email body |
| No | CC recipient email address (separate multiple with commas) |
| No | BCC recipient email address (separate multiple with commas) |
| No | Attachment path (separate multiple with commas) |
| No | Whether the body is in HTML format (default false) |
| No | Sender 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=trueSMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USE_TLS=trueOutlook/Hotmail
Outlook/Hotmail
SMTP_SERVER=smtp-mail.outlook.com
SMTP_PORT=587
SMTP_USE_TLS=trueSMTP_SERVER=smtp-mail.outlook.com
SMTP_PORT=587
SMTP_USE_TLS=trueQQ 邮箱
QQ Mail
SMTP_SERVER=smtp.qq.com
SMTP_PORT=587
SMTP_USE_TLS=trueSMTP_SERVER=smtp.qq.com
SMTP_PORT=587
SMTP_USE_TLS=true163 邮箱
163 Mail
SMTP_SERVER=smtp.163.com
SMTP_PORT=587
SMTP_USE_TLS=trueSMTP_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=trueSMTP_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
- 永远不要在代码中硬编码密码
- 使用环境变量或加密的配置文件
- 定期更换应用专用密码
- 不要在公共网络使用 SMTP 发送敏感信息
- Never hardcode passwords in your code
- Use environment variables or encrypted configuration files
- Regularly update your App Password
- Do not send sensitive information via SMTP on public networks
故障排除
Troubleshooting
运行测试脚本验证配置:
bash
python scripts/test_smtp.py如果测试失败,检查:
- 文件配置是否正确
.env - 网络连接是否正常
- 邮箱账号密码是否正确
- 防火墙是否阻止 SMTP 端口
Run the test script to verify the configuration:
bash
python scripts/test_smtp.pyIf the test fails, check:
- Whether the configuration in the file is correct
.env - Whether the network connection is normal
- Whether the email account and password are correct
- Whether the firewall blocks the SMTP port