监控文件并想用 `mail` 命令发送电子邮件

Monitoring a file and wanting to send an email with the `mail` command

我将使用 [ -s 文件名 ] 选项监视一个文件,当该文件获取数据时,将适当的信息通过电子邮件发送给我。我搜索了该网站并提出了一些选项,但没有为我点击。

我正在尝试执行以下操作来简单地测试 "mail" 我也尝试过 mailx。用户没有收到电子邮件,mailmailx.

提供的输出完全相同

最终,我将使用 mail 或 mailx 命令编辑原始 bash 脚本,假设我可以让它工作。

这就是我正在做的和输出的 命令行 returns 当我点击时,输入。

感谢您对此的任何意见,我非常感谢。


[user@somehost ~]$ echo "TEST" | mail -s subject user@mail.com

[user@somehost ~]$ send-mail: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
send-mail: warning: inet_protocols: configuring for IPv4 support only
postdrop: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
postdrop: warning: inet_protocols: configuring for IPv4 support only

最好将 inotifywait 用于您想要的活动(也许 CLOSE_WRITE?)。

您需要使用 SMTP 服务器配置您的邮件程序,并且可能(希望)一些凭据。

我通常使用 heirloom-mailx 包中的 mailx,脚本如下:

#!/bin/false

function send_email {
  # args are: subject [to] [from_name] [from_address]
  #   subject is required
  #   to and from_* are optional, defaults below

  # stdin takes the message body... don't forget

  # this function uses mailx from heirloom-mailx

  local SMTP_SRVR="${YOUR_SERVER}"
  local SMTP_USER="${YOUR_USERNAME}"
  local SMTP_PASS="${YOUR_PASSWORD}"

  local DEFAULT_TO="${YOUR_RECIPIENT}"
  local DEFAULT_FROM_NAME="${YOUR_SENDER_NAME}"
  local DEFAULT_FROM_ADDR="${YOUR_SENDER_EMAIL}"

  if [ $# -lt 1 ]; then
    echo "${FUNCNAME}(): missing subject (arg 1)..." >&2
    return 1
  fi
  local SUBJECT=""
  shift

  if [ $# -lt 1 ]; then
    local TO="${DEFAULT_TO}"
  else
    local TO=""
    shift
  fi

  if [ $# -lt 1 ]; then
    local FROM="${DEFAULT_FROM_NAME}"
  else
    local FROM=""
    shift
  fi

  if [ $# -lt 1 ]; then
    FROM="${FROM} <${DEFAULT_FROM_ADDR}>"
  else
    FROM="${FROM} <>"
    shift
  fi

  mailx -s"${SUBJECT}" -r"${FROM}" -Ssmtp="${SMTP_SRVR}" -Ssmtp-auth-user="${SMTP_USER}" -Ssmtp-auth-password="${SMTP_PASS}" "${TO}"
  return $?
}

然后您可以从另一个脚本中使用上述内容(例如:my_send_email.inc),如下所示:

#!/bin/bash

source my_send_email.inc

echo "Testing" | send_email "${MY_EMAIL}"