5

I have a shell script that uses sendmail function to send email the code is as follows

mailalert(){
sendmail -F Sender-Name -it <<END_MESSAGE
To: Recipient@example.com
Subject: Subject

Message
END_MESSAGE
}

It gets executed whenever I call this function. Now I have a text file which I want to send using sendmail as attachment or as message in the email it sends. How can I do that? I have tried alot of tricks but nothing seems to work. Please Help.

Tarun
  • 4,245
  • 13
  • 50
  • 74

5 Answers5

3

Type uuencode /path/filename.txt | sendmail -s "subject" user@domain in your terminal to send mail.

  • Replace "path" with the actual directory path in which the file to attach is located.
  • Replace "filename.ext" with the actual file name and extension.
  • Replace "subject" with the subject line you want the email to have.
  • Replace "user@domain" with the recipient's email address.

this is the actual process to send mail with attachment.

add uuencode /path/filename.txt before sendmail command in your script. I mean modify it as

mailalert(){
uuencode /path/filename.txt
sendmail -F Sender-Name -it <<END_MESSAGE
To: Recipient@example.com
Subject: Subject

Message
END_MESSAGE
}

hope that can help you.

Raja G
  • 102,391
  • 106
  • 255
  • 328
  • Thanks for the reply. I tried it but it gives the error uuencode: not found. – Tarun Oct 09 '13 at 13:54
  • 1
    och! ok sudo apt-get install sharutils then try again.@Tarun – Raja G Oct 09 '13 at 13:57
  • I tried the approach but when executing the script it gives the message begin 644 /path/filename.txt and the cursor keeps on blinking. – Tarun Oct 09 '13 at 14:21
  • @Tarun just look at here https://www.google.co.in/search?client=ubuntu&channel=fs&q=send+mail+attachments+begin+644&ie=utf-8&oe=utf-8&gws_rd=cr&ei=9XZVUs24Hs2Trgeg24HAAg – Raja G Oct 09 '13 at 15:36
2

I have created below script to attach a CSV File. The File is getting generated, but its truncating the header row /column name of CSV incorrectly and also there is one more file thats getting attached with the email, namely 'ATT0001.txt' with every email. Anything wrong that you could found out here?

SCRIPT

(
echo "From:"$1;
echo "To:"$2;
echo "Subject:"$3;
echo "MIME-Version: 1.0";
echo "Content-Type:multipart/mixed; boundary=\"B835649000072104Jul07\"";

echo "--B835649000072104Jul07";
echo "Content-Type: text/html; charset=\"UTF-8\"";
echo "Content-Transfer-Encoding: 7bit";
echo "Content-Disposition: inline";
echo "";
echo "$4";

echo "--B835649000072104Jul07";
echo "Content-Type: text/csv";
echo "Content-Transfer-Encoding: base64";
echo "Content-Disposition: attachment; filename=\"$5\"";
base64 "$5"

echo "--B835649000072104Jul07";
) | sendmail -t
Félicien
  • 1,183
1

Rather strange but I used a different approach as while using uuencode it started executing but the cursor stuck at begin 644 /path/to/file so I used cat to send my file in the message body.

file=/path/to/file
mailalert(){
sendmail -F Sender-Name -it <<END_MESSAGE
To: Recipient@example.com
Subject: Subject

$(cat $file)
END_MESSAGE
}

The above code worked perfectly but when I saw the message in my web browser it was fine. But when I saw it in Thunderbird it was not shown correctly. It was like kind of encoded.

So, I'm keeping this question open until I dont find the right solution for now.

Tarun
  • 4,245
  • 13
  • 50
  • 74
1

When sending mail, even from the command line, it is best to use a program which was designed for that purpose, rather than calling sendmail directly. A good all-around command-line e-mail client is mutt; in particular it has a command-line flag to attach files, which avoids the cumbersome use of uuencode:

echo Test | mutt -s Test -a image.jpg -- firas@fkraiem.org

As usual, see man mutt for usage information.

fkraiem
  • 12,555
  • 4
  • 35
  • 40
0

After searching through numerous useless MAN pages and googling I finally came to this solution:
File to be sent: test.csv

On Ubuntu you will have uuencode (AIX), provided by the package sharutils, which you may need to install (the equivalent on Redhat is mutt).

uuencode test.csv test.csv | mail -v -s "Subject test" -r no-reply@mydomain.co.uk recepient@mydomain.co.uk

The biggest problem was to combine sendmail and attachment and at the same time setting the subject. Also on AIX I do not have the -a attachment option, like the LINUX Man page suggests.

You can also use mailx instead of mail with exactly the same parameters.

Zanna
  • 70,465
  • 1
    mutt is not "the equivalent" of uuencode. Also, mailx should always be preferred to mail, as the latter is non-standard. – fkraiem Dec 01 '17 at 11:10