用 perl 开发的命令行工具 swaks 用来发送邮件挺方便的,但是有些环境不便安装,我问 AI,结果推荐了 curl 命令。
-> % curl --help smtp
smtp: SMTP protocol
--crlf Convert LF to CRLF in upload
-F, --form <name=content> Specify multipart MIME data
--form-string <name=string> Specify multipart MIME data
-H, --header <header/@file> Pass custom header(s) to server
--login-options <options> Server login options
--mail-auth <address> Originator address of the original email
--mail-from <address> Mail from this address
--mail-rcpt <address> Mail to this address
--mail-rcpt-allowfails Allow RCPT TO command to fail
--oauth2-bearer <token> OAuth 2 Bearer Token
-X, --request <method> Specify request method to use
--ssl Try enabling TLS
--ssl-reqd Require SSL/TLS
我用 curl 发送了几封邮件,还真的挺好的,满足我的要求。
-
生成邮件内容:
export FROM=noreply@example.com export RCPT=ninedoors@126.com export USER=shanda export PASS=6404290f99f75bf182673bcbd012c121 # AI 生成验证码邮件模板 cat > /tmp/mail.html <<EOF From: $FROM To: $RCPT Subject: Your Verification Code: CODE Content-Type: text/html; charset=UTF-8 <!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><title>Verification Code</title><style>@media only screen and(max-width:600px){.container{width:100%!important}.mobile-padding{padding:10px!important}}</style></head><body style="margin: 0; padding: 10px; background-color: #f4f4f4; font-family: Arial, sans-serif;"><table role="presentation"align="center"border="0"cellpadding="0"cellspacing="0"width="100%"style="max-width: 600px; width: 100%; margin: 0 auto; background-color: #ffffff;"><tr><td class="mobile-padding"style="padding: 40px 20px;"><table role="presentation"width="100%"border="0"cellpadding="0"cellspacing="0"><tr><td style="padding-bottom: 30px;"><h1 style="color: #333333; margin: 0;">Verification Code</h1></td></tr></table><table role="presentation"width="100%"border="0"cellpadding="0"cellspacing="0"><tr><td style="padding: 20px 0;"><p style="color: #666666; line-height: 1.6; font-size: 16px;">Your verification code is:<h2 style="letter-spacing: 2px;">CODE</h2></p></td></tr></table></td></tr></table></body></html> EOF # 替换验证码内容,我不想每次发送相同内容 # sed "s/CODE/$(printf "%06d" $((RANDOM % 1000000)))/g" /tmp/mail.html > /tmp/mail2.html sed s/CODE/$(python -c "import secrets; print('%06d' % secrets.randbelow(10**6))")/g /tmp/mail.html > /tmp/mail2.html -
邮件发送:
curl smtp://smtp.engagelab.cc:2525 --mail-from nobody@whatever.com --mail-rcpt $RCPT --user "$USER:$PASS" --upload-file /tmp/mail2.html -vv -s 2>&1 | grep -Fv '] * '
遇到一个奇怪的现象:zsh 中 RANDOM 这样使用结果不会变更,没有时间研究,暂时忽略。
-> % echo $(printf "%06d" $((RANDOM % 1000000)))
018840
-> % echo $(printf "%06d" $((RANDOM % 1000000)))
018840
-> % echo $(printf "%06d" $((RANDOM % 1000000)))
018840
如果直接使用却会,不知道为什么:
for i in {1..5}; do echo $RANDOM done