【问题标题】:Using sendmail from bash script for multiple recipients对多个收件人使用来自 bash 脚本的 sendmail
【发布时间】:2012-11-15 02:53:03
【问题描述】:

我正在 cron 中运行一个 bash 脚本,以便在满足特定条件时向多个收件人发送邮件。

我已经像这样对变量进行了编码:

subject="Subject"
from="user@domain.com"
recipients="user1@gmail.com user2@gmail.com"
mail="subject:$subject\nfrom:$from\nExample Message"

以及实际发送:

echo -e $mail | /usr/sbin/sendmail "$recipients"

问题是只有 user2@gmail.com 正在接收电子邮件。如何更改此设置,以便所有收件人都能收到电子邮件?

注意:解决方案必须使用 sendmail,我使用的是 jailshell,它似乎是唯一可用的方法

【问题讨论】:

    标签: bash sendmail


    【解决方案1】:

    尝试这样做:

    recipients="user1@gmail.com,user2@gmail.com,user3@gmail.com"
    

    另一种方法,使用 shell here-doc

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

    根据RFC 822,请务必使用空行将标题与正文分开。

    【讨论】:

      【解决方案2】:

      使用选项 -t 发送邮件。

      在你的情况下 - echo -e $mail | /usr/sbin/sendmail -t 并将您的收件人列表添加到消息本身,例如 To: someone@somewhere.com someother@nowhere.com 就在 From:..... 行之后

      -t 选项意味着 - 阅读收件人的消息。 To:、Cc: 和 Bcc: 行将被扫描以查找收件人地址。 Bcc: 行将在传输前被删除。

      【讨论】:

      • 当我通过-t 选项时,我得到sendmail: recipients with -t option not supported。有任何想法吗?谢谢。
      【解决方案3】:

      在 shell 脚本中使用 sendmail

      subject="mail subject"
      body="Hello World"
      from="me@domain.com"
      to="recipient1@domain.com,recipient2@domain.com"
      echo -e "Subject:${subject}\n${body}" | sendmail -f "${from}" -t "${to}"
      

      【讨论】:

        【解决方案4】:

        对于后缀sendmail,我添加了一个对脚本有用的行命令

        我在 RHEL(未公开的收件人)中的 sendmail 命令末尾添加默认位置的收件人时遇到问题,管道回显命令挽救了这一天。

        选项-fhttp://www.postfix.org/sendmail.1.html找到

        请注意,echo 中的语法很重要,请在尝试使用 sendmail 之前尝试回显到文件以进行检查。

        echo -e "To:receiver1@domain1, receiver2@domain2 \nSubject:Subject of email \n\nBody of email.\n" | /usr/sbin/sendmail -f sender@domain -F sendername -it

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-18
          • 2010-11-23
          • 2012-01-11
          • 1970-01-01
          • 2015-11-20
          • 2023-04-07
          • 2013-09-28
          相关资源
          最近更新 更多