首页 > 解决方案 > Mail 命令将错误日志文件作为附件发送,而不是在 Linux 的正文中发送

问题描述

我在使用 mail 命令从 Linux 机器发送电子邮件时遇到问题。它不是像在正文中那样发送错误日志,而是以 .bin 格式作为附件发送。在少数情况下,它发送的是身体。以下是我尝试通过电子邮件作为正文发送的日志详细信息。是因为它作为.bin附件发送的日志中的“/”等特殊字符吗?我可以使用 sendmail 来解决这个问题,但我们想使用 mail 命令发送它。

原因:org.springframework.integration.MessagingException:上传文件时无法写入'/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' 原因:java.io.IOException:失败将“/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp”重命名为/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz。服务器回复:550'PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp':无法重命名。2021-10-15 02:01:49,342 错误 | dbs-intg-scheduler-18 | cndjadapter.support.MDCFatalErrorChannelInterceptor | [DBS] 致命错误 [org.springframework.integration.MessageDeliveryException:

mContent=`cat $1`
msgimp=$2
mailsub=$3
monitor=$4
logname=$5
echo  >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
echo $mContent  >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt


mail -s "[Prod] [$monitor] [$msgimp] [$mailsub] found in $logname log" -r "DBS Production 
Alert <noreply@*******.com>" alert.*********.com < 
/datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt

rm -f /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt

标签: linuxshellmailx

解决方案


mContent=`cat $1`
...
echo $mContent  >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt

此时, 的内容与 的$mailsub.txt内容不一样$1。mContent 变量是 unquoted 因此所有空格序列(包括换行符!)都被一个空格替换。所以电子邮件的正文是一长串。

我在这里猜测,但如果该行很长,我可以想象电子邮件系统的某些部分会更改消息以将正文移动到附件中。

我不明白为什么需要读取输入文件,将其(错误地)写入新文件,邮寄新文件的内容,然后删除新文件。试试这个:

mData=$1
subject=$(printf "[Prod] [%s] [%s] [%s] found in %s log" "$4" "$2" "$3" "$5")
from="DBS Production Alert <noreply@*******.com>"
to="alert.*********.com"

mail -s "$subject" -r "$from" "$to" < "$mData" 

推荐阅读