sending file using sendmail

55,200

Solution 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: [email protected]
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.

Solution 2

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: [email protected]
Subject: Subject

Message
END_MESSAGE
}

hope that can help you.

Solution 3

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

Solution 4

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 -- [email protected]

As usual, see man mutt for usage information.

Share:
55,200

Related videos on Youtube

chadwicke619
Author by

chadwicke619

Updated on September 18, 2022

Comments

  • chadwicke619
    chadwicke619 over 1 year

    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: [email protected]
    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.

  • chadwicke619
    chadwicke619 over 10 years
    Thanks for the reply. I tried it but it gives the error uuencode: not found.
  • mx7
    mx7 over 10 years
    och! ok sudo apt-get install sharutils then try again.@Tarun
  • chadwicke619
    chadwicke619 over 10 years
    I tried the approach but when executing the script it gives the message begin 644 /path/filename.txt and the cursor keeps on blinking.
  • mx7
    mx7 over 10 years
    @Tarun just look at here google.co.in/…
  • fkraiem
    fkraiem over 6 years
    mutt is not "the equivalent" of uuencode. Also, mailx should always be preferred to mail, as the latter is non-standard.
  • Hussain K
    Hussain K almost 4 years
    Your code doesn't work for me:-(