Linux shell 发送电子邮件

Jackey Linux 189 次浏览 , , 没有评论

新建文件mail.sh,输入内容:

#!/bin/bash

# 设置SMTP服务器地址、端口号、登录信息等参数

export EMAIL="810706080@qq.com"
export PASSWORD="123"
export RECIPIENTS=("xx@qq.com" "xx@aliyun.com")
export SUBJECT="Subject of the email"
export BODY="This is the body of the email"

# shellcheck disable=SC1060
# shellcheck disable=SC1073
# shellcheck disable=SC2068
for mail in ${RECIPIENTS[@]}; do
    curl -s --url "smtps://smtp.qiye.aliyun.com:465" --ssl-reqd \
      --mail-from "$EMAIL" \
      --mail-rcpt "$mail" \
      --user "$EMAIL:$PASSWORD" \
      --upload-file <(echo -e "From: $EMAIL\nTo: $mail\nSubject: $SUBJECT\n\n$BODY")
done

运行方式:bash mail.sh

如果需要发送html内容,需要添加:Content-Type: text/html 参数

完整示例:

#!/bin/bash

tableHead="<html><body><table border cellpadding=\"10\" cellspacing=\"0\" width=\"200\"><tr align=\"center\"><th>version</th><th>percent</th></tr>"
tableContent="<tr align=\"center\"><td>8.21.0</td><td>42.00%</td></tr>"
tableEnd="</table></body></html>"

# 设置SMTP服务器地址、端口号、登录信息等参数

export EMAIL="810706080@qq.com"
export PASSWORD="123"
export RECIPIENTS=("xx@qq.com")
export SUBJECT="Subject of the email"
export BODY=$tableHead$tableContent$tableEnd

# shellcheck disable=SC1060
# shellcheck disable=SC1073
# shellcheck disable=SC2068
for mail in ${RECIPIENTS[@]}; do
  curl -s --url "smtps://smtp.qiye.aliyun.com:465" --ssl-reqd \
        --mail-from "$EMAIL" \
        --mail-rcpt "$mail" \
        --user "$EMAIL:$PASSWORD" \
        --upload-file <(echo -e "From: $EMAIL\nTo: $mail\nSubject: $SUBJECT\nContent-Type: text/html\n\n$BODY")
done

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Go