2025-07-17 10:45:47
17 07 2025
摘要:在Linux服务器使用curl通过qq邮件服务器发送邮件

1、创建一个send_email.sh脚本

   #!/bin/bash

   if [ ${#} -lt 3 ]
   then
      echo 请输入3个参数,第一个参数接收者email,第二个参数主题(标题),第三个参数发送的内容
      exit 1
   fi
   toemail="$1"
   subject="$2"
   content="$3"

   from=zhifu@love2where.com
   tmpfile=$(mktemp) #创建一个临时文件来存放发送的内容
   cat >$tmpfile<<EOF
   From: love2where <${from}>
   To: $toemail <${toemail}>
   Subject: ${subject}
   Content-Type: text/html; charset=utf-8

   ${content}
   EOF

   #通过curl发送邮件

   

   curl --ssl-reqd -s --tlsv1.3 --user 'zhifu@love2where.com:密码' --url smtp://smtp.exmail.qq.com:587 --mail-from 'zhifu@love2where.com' --mail-rcpt "${toemail}" --upload-file $tmpfile

   #参数说明:

   #--url: SMTP 服务器地址(Gmail 使用 smtps://smtp.gmail.com:465

   #--user: 邮箱地址 + 授权码(非登录密码)

   #--mail-from: 发件人邮箱

   #--mail-rcpt: 收件人邮箱

   #--upload-file: 包含邮件内容的文件


   rm -rf $tmpfile #删除发送的文件

 

2、赋予文件可执行权限

   chmod 750 send_email.sh

3、执行测试

   ./send_email.sh "接收者email" "主题" "发送内容"

延伸阅读
  1. 上一篇:JDK
  2. 下一篇:Nginx
发表评论