Sending simple message body + file attachment using Linux Mailx [duplicate] - shell

This question already has answers here:
How to attach a file using mail command on Linux? [duplicate]
(13 answers)
Closed 5 years ago.
I am writing a shell script to send an email using Linux Mailx, the email must contain a file attachment and a message body.
Currently sending an email with an attachment:
output.txt | mail -s "Daily Monitoring" james#dell.com
I wish to add a message body. How should i?
Linux Mailx:
mail [-eIinv] [-a header] [-b addr] [-c addr] [-s subj] to-addr

The usual way is to use uuencode for the attachments and echo for the body:
(uuencode output.txt output.txt; echo "Body of text") | mailx -s 'Subject' user#domain.com
For Solaris and AIX, you may need to put the echo statement first:
(echo "Body of text"; uuencode output.txt output.txt) | mailx -s 'Subject' user#domain.com

The best way is to use mpack!
mpack -s "Subject" -d "./body.txt" "././image.png" mailadress
mpack - subject - body - attachment - mailadress

Johnsyweb's answer didn't work for me, but it works for me with Mutt:
echo "Message body" | mutt -s "Message subject" -a myfile.txt recipient#domain.com

Try this it works for me:
(echo "Hello XYX" ; uuencode /export/home/TOTAL_SI_COUNT_10042016.csv TOTAL_SI_COUNT_10042016.csv ) | mailx -s 'Script test' abc#xde.com

On RHEL Linux, I had trouble getting my message in the body of the email instead of as an attachment . Using od -cx, I found that the body of my email contained several /r. I used a perl script to strip the /r, and the message was correctly inserted into the body of the email.
mailx -s "subject text" me#yahoo.com < 'body.txt'
The text file body.txt contained the char \r, so I used perl to strip \r.
cat body.txt | perl success.pl > body2.txt
mailx -s "subject text" me#yahoo.com < 'body2.txt'
This is success.pl
while (<STDIN>) {
my $currLine = $_;
s?\r??g;
print
}
;

You can try this:
(cat ./body.txt)|mailx -s "subject text" -a "attchement file" receiver#domain.com

Related

How to use echo inside another echo [duplicate]

This question already has answers here:
How to pipe multiple commands into a single command in the shell? (sh, bash, ...)
(3 answers)
Closed 4 years ago.
I wish to use an echo command into my bash script.
Actually I do that way =>
echo --- REPORT ---
echo - Report send # $(date +%d-%m-%y_%H:%M)
echo "echo \"Subject: BACKUP-$serv\"" | cat $cheminlogs/backup-$serv.log | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2;
echo - Report done # $(date +%d-%m-%y_%H:%M)
But that's don't work when I call my sh file, that's print the echo linke but won't send me the email into the 3rd line command
If I use my command alone that's ok
echo "Subject: BACKUP-$serv" | cat $cheminlogs/backup-$serv.log | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2;
I don't know how to use the echo in the start of a new command
Perhaps I can change my email command by invertion argument but that's send me an email without textarea cat
cat $cheminlogs/backup-$serv.log | echo "Subject: BACKUP-$serv" | /opt/zimbra/postfix/sbin/sendmail -r $mail1 $mail2;
The form you are looking for is
printf 'Subject: %s\n\n' "$subject" | cat - "$server_log" | sendmail
The - means 'read from standard input'. Note that sendmail will expect an empty line between the header and the message body.

Cshell Mutt send Email with String

I am using a .csh script on my Centos 5.5 - yes:) - with Mutt (V 1.5).
Well, I can send an Email no problem like so:
echo "" | mutt -s 'Subject' -i /tmp/body.txt 'test#test.com'
Now I want to save above parameters in a variable and then call mutt with its contents...
The variable contains the following in the end:
-s 'Subject' -i /tmp/body.txt 'test#test.com'
The question is not how to "extract" this string to execute the above command.
I have tried:
set MailString="-s 'Subject' -i /tmp/body.txt 'test#test.com'"
# (Tried these, don't work)
#echo "" | mutt `echo "$MailString"`
#echo "" | mutt `echo $MailString`
#echo "" | mutt echo $MailString`
#echo "" | mutt $MailString`
csh
hope this will help:
#!/bin/csh -f
set MailString="-s 'Subject' -i /tmp/body.txt test#test.com"
mutt ${MailString}
write MailString in ${} and it should work for you
if you would like to have it more flexible:
you can use args for the different fields :
#!/bin/csh -f
set attach_file='file.txt'
set subject='My subject'
set text_file='/tmp/body.txt'
set MailString="-a ${attach_file} -s ${subject} -i ${text_file} test#test.com"
mutt ${MailString}
you can push script's args to different field using $1,$2, etc...

Send multiple files by email and also add a body message to the email (Unix Korn Shell)

I'm trying to send multiple files by email but also include a body message in the email, I've tried couple of ways with no luck, the following code is for send multiple files:
(uuencode file1.txt file1.txt ; uuencode file2.txt file2.txt) | mailx -s "test" email#test.com
I've tried this option with no luck:
echo "This is the body message" | (uuencode file1.txt file1.txt ; uuencode file2.txt file2.txt) | mailx -s "test" email#test.com
any idea how could be the code?
Try this:
(echo "This is the body message"; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt) | mailx -s "test" email#test.com
The issue with your command is that you are piping the output of echo into the subshell and it is getting ignored as uuencode isn't reading from stdin.
You can use { ... } to avoid the subshell:
{ echo "This is the body message"; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt; } | mailx -s "test" email#test.com
If you are doing this in a script and you want it to look more readable, then:
{
echo "This is the body message"
uuencode file1.txt file1.txt
uuencode file2.txt file2.txt
} | mailx -s "test" email#test.com

sending multiple files as attachment in e-mail using mailx

I have a requiremnet to send multiple files as e-mail attachmnet in shell script. I have used below command.
(printf "%s\n" "BODY"; uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt ) | mailx -m -s "TEST" emailid#domain.com
However the number of files i want to send as an attachmnet are dynamic. So I want to assign the uuencode ... comand to a variable and then use it. I have tried below way,
$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
$ (printf "%s\n" "BODY"; $ATTACH_CMD ) | mailx -m -s "TEST" emailid#domain.com
And i am getting below error.
sh: uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt: not found.
Can any one please help me with this? Thanks in advance.
Have you tried using the below code? Not sure why it works, but maybe the below code could be used as a workaround
(printf "%s\n" "BODY"; `echo $ATTACH_CMD` ) | mailx -m -s "TEST" emailid#domain.com`?
For $ATTACH_CMD I have used echo command.
I finally found the way. eval makes the trick
eval $STR
$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
$ (printf "%s\n" "BODY"; eval $ATTACH_CMD ) | mailx -m -s "TEST" emailid#domain.com

Can't figure out how to send ^D (EOT) signal to mailx in bash script

I'm writing a bash script to send me an email automatically.
Mailx requires an EOT or ^D signal to know the message body is over and it can send.
I don't want to hit ^D on the keyboard when I run script which is what it does now.
Here is my code:
#! /bin/bash
SUBJ="Testing"
TO="test#test.com"
MSG="message.txt"
echo "I am emailing you" >> $MSG
echo "Time: `date` " >> $MSG
mail -s "$SUBJ" -q "$MSG" "$TO"
rm -f message.txt
If you do not need to add more text and just need to send the content of $MSG, you can replace
mail -s "$SUBJ" -q "$MSG" "$TO"
with
mail -s "$SUBJ" "$TO" < "$MSG"
The EOT will be implicit in the < construct. -q is indeed only used to start a message. The rest is supposed to come through stdin.
Pipe the output of a command group to mail.
#! /bin/bash
SUBJ="Testing"
TO="test#test.com"
MSG="message.txt"
{
echo "I am emailing you"
echo "Time: `date` "
} | mail -s "$SUBJ" -q "$MGS" "$TO"
rm -f message.txt

Resources