#!/bin/bash
ENV=$1
TARGET_VERSION=$2
TO=$3
# SMTP服务器设置
SMTP_SERVER="邮箱服务地址"
SMTP_PORT="25"
# 邮件信息
FROM="jenkins@y.com"
SUBJECT="Deployment Status Notification"
BODY=$ENV"发布完成,版本 :"$TARGET_VERSION
# 临时文件存储邮件内容
EMAIL_CONTENT_FILE=$(mktemp)
# 构建邮件内容
cat <<EOF > $EMAIL_CONTENT_FILE
From: $FROM
To: $TO
Subject: $SUBJECT
$BODY
EOF
# 使用curl发送邮件
curl -v --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
--mail-from "$FROM" \
--mail-rcpt "$TO" \
--upload-file $EMAIL_CONTENT_FILE
# 删除临时文件
rm -f $EMAIL_CONTENT_FILE
有3个参数,我写成动态的了。不要可以拿掉。
#!/bin/bash
TARGET_VERSION=$2
# SMTP服务器设置
SMTP_SERVER="服务地址"
SMTP_PORT="25"
# 邮件信息
FROM="jenkins.fsts@y.com"
TO="b@y.com"
SUBJECT="Deployment Status Notification"
BODY="发布完成,发布版本 :"$TARGET_VERSION
# 临时文件存储邮件内容
EMAIL_CONTENT_FILE=$(mktemp)
# 构建邮件内容
cat <<EOF > $EMAIL_CONTENT_FILE
From: $FROM
To: $TO
Subject: $SUBJECT
$BODY
EOF
# 使用curl发送邮件
curl -v --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
--mail-from "$FROM" \
--mail-rcpt "$TO" \
--upload-file $EMAIL_CONTENT_FILE
# 删除临时文件
rm -f $EMAIL_CONTENT_FILE
多人情况
#!/bin/bash
ENV=$1
TARGET_VERSION=$2
TO_EMAILS=$3 # 多个收件人用逗号分隔,例如 "email1@example.com,email2@example.com"
# 检查必需的参数
if [ -z "$ENV" ] || [ -z "$TARGET_VERSION" ] || [ -z "$TO_EMAILS" ]; then
echo "Usage: $0 <environment> <target-version> <to-emails>"
exit 1
fi
# 将逗号分隔的收件人地址转换为空格分隔
TO_EMAIL_LIST=$(echo $TO_EMAILS | tr ',' ' ')
# SMTP服务器设置
SMTP_SERVER="邮件服务"
SMTP_PORT="25"
# 邮件信息
FROM="fsts.jenkins@disney.com"
SUBJECT="Deployment Status Notification"
BODY="$ENV 发布完成,版本 :$TARGET_VERSION"
# 临时文件存储邮件内容
EMAIL_CONTENT_FILE=$(mktemp)
# 构建邮件内容
cat <<EOF > $EMAIL_CONTENT_FILE
From: $FROM
To: $TO_EMAILS
Subject: $SUBJECT
$BODY
EOF
# 使用curl发送邮件
if curl --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
--mail-from "$FROM" \
$(for EMAIL in $TO_EMAIL_LIST; do echo "--mail-rcpt $EMAIL "; done) \
--upload-file "$EMAIL_CONTENT_FILE" > /dev/null 2> curl_email.log; then
echo "Email sent successfully."
else
echo "Failed to send email. Check curl_email.log for details."
fi
# 删除临时文件
rm -f "$EMAIL_CONTENT_FILE"