Mail command sending the error log file as attachment instead of in the Body in Linux - linux

I'm having an issue while sending an email from the Linux box using mail command. Instead of sending the error log as in the Body it is sending as an attachment in .bin format. Where as in few instances it is sending in the Body. Below is the log details which I'm trying to send via email as body. Is it because of Special characters like "/" in the log it is sending as .bin attachemnt. I can use sendmail inorder to fix this but we want to send it using mail command.
Caused by: org.springframework.integration.MessagingException: Failed to write to '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' while uploading the file Caused by: java.io.IOException: Failed to rename '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' to /DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz'. Server replied with: 550 'PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp': cannot rename.
2021-10-15 02:01:49,342 ERROR | dbs-intg-scheduler-18 | c.n.d.j.adapter.support.MDCFatalErrorChannelInterceptor | [DBS] Fatal Error [org.springframework.integration.MessageDeliveryException: Error handling message for file [/datapp/common/batch_datafile/parts/P-16/outComingFolder/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz]] -org.springframework.integration.MessagingException: Failed to write to '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_2021101507171
1.xml.gz.tmp' while uploading the file
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

mContent=`cat $1`
...
echo $mContent >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
At this point, the contents of $mailsub.txt are not the same as the contents of $1. The mContent variable is unquoted, therefore all sequences of whitespace (including newlines!) get replaced by a single space. So the body of the email is one long line.
I'm guessing here, but if that line is very long, I can imagine some part of the email system would change the message to move the body into an attachment.
I don't see why it would be necessary to read the input file, write it (incorrectly) to a new file, mail the contents of the new file, and delete the new file. Try this instead:
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"

Related

Shell script to send a custom message to users listed in a file?

I would like to write a shell script, that opens two text files (e.g message.txt, users.txt) and sends the message found in the first file ($1) to every single user found in the second file ($2)?
If a user is currently logged in, the message should be sent with the "write" command, else (if they are not currently logged in), it will be sent as a "mail".
I tried running this code:
#!/bin/bash
msg=`cat $1`
input=$2
while IFS= read -r usr
do
`write $usr $msg`
if [ $? -eq 1 ]
#here I tried checking if the $usr found in the $2 file is not online
`mail $usr $msg`
#The Subject part could also be a problem here
fi
done < "$input"
However, I am unsure how the mail part should be done, when running the code it even says that there is an error on line 22 (where I tried mailing the user).
You can use the following syntax to send an email via bash:
mail -s 'message subject' username#gmail.com <<< 'testing message body'
So in your case, you should add the -s parameter before the subject and you should add the body to add to your email. (you can use '' for empty body)
Another syntax to perform that:
echo "text message body" | mail -s "message subject" username#gmail.com
PS: You should not use the backticks (``) for the mail command

adding the .txt file's content in the body of the message in the mail of mailx command in linux

I have written a script where i am storing the name of the top 10 processes which are using the greatest ram of the cpu with a command:
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/etc/tmp/file.txt
Now I am attaching this file in the mailx command to send a mail with its body as content with the command:
echo -e "Warning, server memory is running low!\n\nFree memory: $free MB\n$file" | mailx -a "ALERT" -s "$subject" -r "$from" -c "$also_to" "$to"
this "subject" , "from", "also_to", "to" are some variables and have actual mailid defined in the shell script.
when I get receive the mail I get the content in a very rough order like the picture below that i have added.that mail's pic
I want this content to of the file to be in proper format or if I could just send this txt file as an attachment, I made a whole study for sending the file as an attachment using the mailx command but to no avail.
You are misusing the -a option. You are supposed to use it to add a mail header (Header: value), not the single word "ALERT" like you did.
As to how to send the file as an attachment, I usually use the -A option with no trouble (at worst, I vaguely remember that sometimes I had to specify the file encoding so that accented characters are correctly displayed, but you don't need this with your ASCII file)

How can I build a dynamic parameter list for mail command in bash linux?

I am trying to send a mail with a file names in the message body from my terminal to my gmail account. I am using mail command to do it. My requirement is that I should dynamically update the file names in the message body as the code run...I no need to send as an attachment. I am using centos7.
Below is my script:
v_cfg_email_adresse_to="abc#gmail.com"
v_cfg_email_subject="Report from December 2016"
v_tmp_email_text_name="Message Body"
v_email_main_file="test3.sh"
v_tmp_path="/home/centos/rr/"
if [ ! -z "${v_email_main_file}" ]; then
v_mailx_parameters+=( -a "${v_tmp_path}${v_email_main_file}" )
fi
v_mail_x_parameters+=( -s "${v_cfg_email_subject}" )
v_mail_x_parameters+=( "${v_cfg_email_adresse_to}" )
printf '/binmailx %s < %s\n' "${v_mail_x_parameters[*]}" "${v_tmp_email_text_name}"
/bin/mailx "${v_mailx_parameters[#]}" < "${v_email_main_file}"
If the code is not correct..Please help me..
Thanks in advance
Instead of above code i just redirected my output in the mail command using > .I works fine!!

How to check if a perforce file is opened using bash script

So I run "p4 opened the_file", it prints the status, but I cannot capture the print:
$ a=`p4 opened file1`
file1 - file(s) not opened on this client.
$ echo $a
Variable a is empty.
What I want is, I can get hold of the string "file(s) not opened on this client" and search for "not opened".
Any way to do that?
Change the first line to:
a=`p4 opened file1 2>&1`.
That redirects stderr into stdout, so your variable will capture both normal output and error output.
If you do:
p4 -s opened file1
all the server output gets sent to stdout and prefixed with a tag saying whether it's "error" or "info" (be careful, it's not always obvious where the distinction is). For your case that should get you something like:
error: file1 - file(s) not opened on this client.
exit: 0
Another fun global option (i.e. it goes before the command name, same as the "-s") is "-e", which gives you the raw error dict; you can do interesting things with this like look for a specific error code rather than grepping human-readable message strings.
if [[ -z "$(p4 -ztag opened $myfile)" ]]; then echo "Not opened"; fi
p4 -ztag opened won't return anything for a file that isn't open, so you can just test for empty output.

getting dead.letter file on Linux?

i am sending mail to list of users, i am sending mail using mailx utility
mailx -s "$SUBJECT" "$TO" < $FILE
its working fine with valid emails, but problem is that i am getting dead.letter file when i tried to send to mail id like adffadf , it string not a valid email,
expected - i want this dead.latter must not be occur even user having anything for email ID.
like abc#gmail.com, abc#def.cc, adffdfs
The man page for my mailx says a lot of things about set nosave and so on, but they don't seem to work. The only way to stop your dead.letter file growing I have found is to replace it by a link to the special file /dev/null.
rm ~/dead.letter
ln -s /dev/null ~/dead.letter

Resources