使用 shell 脚本发送电子邮件

使用 shell 脚本发送电子邮件

是否可以将多个电子邮件添加到单个变量,以便在运行 shell 脚本时向所有指定的电子邮件发送邮件?

答案1

假设您在脚本中使用sendmailmail(两者都需要以逗号分隔的字符串作为收件人列表),您可以连接 ID(或直接将它们写为这样的列表):

$: recipients="[email protected], [email protected], [email protected]"

或连接:

$: base_recipients="[email protected], [email protected]"
$: full_recipients="$base_recipients, [email protected]"

$: echo $full_recipients
[email protected], [email protected], [email protected]  

以下是发送具有sendmail3 个不同邮件 ID 的邮件的示例:

#!/bin/bash

recipients="[email protected], [email protected], [email protected]"
subject="Mail to you all"
from="[email protected]"

message_txt="Hi all!\n This is a message to all 3 of you!.\n cheers, Me."

/usr/sbin/sendmail "$recipients" << EOF
subject:$subject
from:$from
$message_txt
EOF

答案2

这是我使用邮件数组的代码:

MAILADDR=([email protected] [email protected] [email protected])
for i in "${MAILADDR[@]}"
    do
         echo "Mail test..." | mail -s "Mail test subject..." $i
    done

相关内容