【问题标题】:Is it possible to send mails by bash script via smtp?是否可以通过 smtp 通过 bash 脚本发送邮件?
【发布时间】:2012-04-17 10:17:09
【问题描述】:

我有后缀+dovecot。我想制作可以为此使用 SMTP 的 bash 脚本。我不想使用 sendmail。

有可能吗?可能有人有一些代码示例?

【问题讨论】:

标签: bash smtp postfix-mta


【解决方案1】:

男孩,当那个手套被扔出时,它总是bashes 我就在头顶! :-)

#!/bin/sh

function checkStatus {
  expect=250
  if [ $# -eq 3 ] ; then
    expect="${3}"
  fi
  if [ $1 -ne $expect ] ; then
    echo "Error: ${2}"
    exit
  fi
}

MyHost=`hostname`

read -p "Enter your mail host: " MailHost
MailPort=25

read -p "From: " FromAddr

read -p "To: " ToAddr

read -p "Subject: " Subject

read -p "Message: " Message

exec 3<>/dev/tcp/${MailHost}/${MailPort}

read -u 3 sts line
checkStatus "${sts}" "${line}" 220

echo "HELO ${MyHost}" >&3

read -u 3 sts line
checkStatus "$sts" "$line"

echo "MAIL FROM: ${FromAddr}" >&3

read -u 3 sts line
checkStatus "$sts" "$line"

echo "RCPT TO: ${ToAddr}" >&3

read -u 3 sts line
checkStatus "$sts" "$line"

echo "DATA" >&3

read -u 3 sts line
checkStatus "$sts" "$line" 354

echo "Subject: ${Subject}" >&3
echo "${Message}" >&3
echo "." >&3

read -u 3 sts line
checkStatus "$sts" "$line"

【讨论】:

  • 这个好像是我找的。谢谢。
  • 很高兴我可以为你演示这个!
  • 邮件客户端中的to是空的
【解决方案2】:

刚刚发现了这个小巧但很棒的实用程序sendemail(不是sendmail)。语法太简单了,无法解释。

例子:

SERVER="smtp.company.com"
FROM="sender@company.com"
TO="recepient@company.com"
SUBJ="Some subject"
MESSAGE="Some message"
CHARSET="utf-8"

sendemail -f $FROM -t $TO -u $SUBJ -s $SERVER -m $MESSAGE -v -o message-charset=$CHARSET

更多信息可通过帮助或作者网站获得: https://github.com/mogaal/sendemail.

【讨论】:

    【解决方案3】:

    使用 gmail 测试,目前可以使用。

    #!/bin/bash
    # Use "host -t mx yourispdomain" to find out yourispmailserver
    exec 1<>/dev/tcp/yourispmailserver/25
    a=$(cat <<"MAILEND"
    HELO local.domain.name
    MAIL FROM: <me@local.domain.name>
    RCPT TO: <you@local.domain.name>
    DATA
    From: me@local.domain.name
    To: you@local.domain.name
    Subject: test
    send your orders for pizza to the administrator.
    .
    QUIT
    .
    MAILEND
    )
    IFS='
    '
    declare -a b=($a)
    for x in "${b[@]}"
     do
       echo $x
       sleep 1
     done
    

    【讨论】:

      【解决方案4】:

      当您说您不想使用 sendmail 时,我不清楚。可能你不想使用 sendmail 进程。

      Postfix 有一个名为“sendmail”的可执行文件,你可能想使用它,因为我想不出你为什么不应该使用它。

      #/bin/bash
      
      FROM='from@test.com'
      TO='to@test.com'
      SUBJECT='This is a test message'
      
      BODY="This is a test mail message body.
      Hi there.
      "
      
      printf "From: <%s>\nTo: <%s>\nSubject: %s\n\n%s" "$FROM" "$TO" "$SUBJECT" "$BODY" | sendmail -f "$FROM"
      

      【讨论】:

      • 我同意,可能我没有说清楚。我的意思是,我想知道 bash 脚本使用 smtp auth 连接到远程 smtp 服务器并发送邮件的可能性。
      【解决方案5】:

      您可以使用 SSMTP。也许这个也有帮助:

      http://tecadmin.net/send-email-smtp-server-linux-command-line-ssmtp/

      【讨论】:

      【解决方案6】:
      • 安装sSMTP,例如:

        apt-get install ssmtp

      • 配置ssmtp:

        sudo nano /etc/ssmtp/ssmtp.conf

        · 服务器:mailhub=smtp.1und1.de:587

        · 主机名:hostname=subdomain.domain.com

        · 用户:AuthUser=user@domain.com

        · 传球:AuthPass=your_password

      然后在你的 sh 文件中,做你需要做的事情并将其通过管道发送到邮件中,例如:

      #!/bin/bash du -sh | mail -s "Disk usage report" user@domain.com

      #!/bin/bash echo "Today's DB backup is ok." | mail -s "DB daily backup alert" user@domain.com

      【讨论】:

      • 收到错误:sSMTP[19315]: 554 Message rejected: Email address is not verified. The following identities failed the check in region US-EAST-1
      【解决方案7】:

      您希望bash 直接与 SMTP 服务器通信吗?这不会真的发生。 技术上可能使用 bash 中提供的网络通信支持,但实际上你不想走这条路。

      这意味着您真正需要的是调用一个外部程序来为您处理 SMTP。通常,这将是 sendmail,但如果您想避免这种情况,还有很多其他选择,包括:

      这两者都可以处理与远程 SMTP 服务器的通信,而不涉及 sendmail。

      【讨论】:

      • 人们经常这样做是因为sendmail、msmtp。后缀等可能未安装和配置
      【解决方案8】:

      我喜欢 dldnh 的回答!

      通过您的专用 smtp 中继发送管理警报的完美解决方案(我知道您有一个)。将 smtp/Postfix/etc 放在多台服务器上是没有意义的,对吧?

      我确实有一个建议:在您的消息文本之前发送一个空行,让中继知道没有更多的标题出现。没有空行,您的消息文本可能无法发送;例如,如果消息的第一行中有一个冒号。

      这里是我冒昧的小改写:

      #!/bin/bash
      MyHost=$(hostname)
      MailHost="mail"
      MailPort=25
      
      function checkStatus {
        read -u 3 sts line
        expect=250
        if [ $# -eq 1 ] ; then
          expect="${1}"
        fi
        if [ $sts -ne $expect ] ; then
          echo "Error: ${line}"
          exit
        fi
      }
      
      FromAddr="$(whoami)@${MyHost}"
      ToAddr="admin@SomeDomain.com"
      Subject="Status Alert"
      Message='Msg: hello bozo!!'
      
      # Brilliant!!
      exec 3<>/dev/tcp/${MailHost}/${MailPort} ; checkStatus 220
      
      echo "HELO ${MyHost}" >&3 ; checkStatus
      echo "MAIL FROM: ${FromAddr}" >&3 ; checkStatus
      echo "RCPT TO: ${ToAddr}" >&3 ; checkStatus
      echo "DATA" >&3 ; checkStatus 354
      echo "Subject: ${Subject}" >&3
      
      # Insert one blank line here to let the relay know you are done sending headers
      # Otherwise a colon ":" in the message text will result in no text being sent.
      echo "" >&3
      
      # Send the message text and close
      echo "${Message}" >&3
      echo "." >&3 ; checkStatus
      

      【讨论】:

        【解决方案9】:

        还想在这里提出另一个解决方案,如果您无法访问 sendmail 等,这很有用。大多数 linux 服务器都有 telnet,所以我们可以使用它。

        function send_email{
        
        echo "EHLO my_mail_server.example.com"   # replace this with your mail server domain
        sleep 1
        echo "MAIL FROM: my_from_address@example.com
        sleep 1
        echo "RCPT TO: my_to_address@example.com
        sleep 1
        echo "DATA
        sleep 1
        echo "Subject: My Subject Line"
        sleep 1
        echo "To: my_to_address@example.com"
        sleep 1
        echo "From: my_from_address@example.com"
        sleep 1
        echo "This is the body of my email. Put whatever in here."
        sleep 1
        echo "." # signifies the end of data entry, mail will be queued to send.
        }
        
        send_email | telnet 1.2.3.4 25 # call the function and pass it into telnet command.
        

        【讨论】:

          猜你喜欢
          • 2014-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-30
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多