Ubuntu – sending file using sendmail

emailscriptssendmail

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.

Best Answer

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.

Related Question