0

I am using the following script to send an email from Unix sever. I have a requirement to attach a file which is at the same location where script is available - /home/app111/attachment.csv.

Could you please help me how to send the file in attachment?

`CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
awk ' BEGIN {
print "To: XXXX@gmail.com"
print "From: YYYY@gmail.com" 
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Subject: PO file '$CUR_DATE'"
print "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
print "<br>Thanks,</br></font></body></html>"
} ' | sendmail -t`
  • Personally, I use `mutt` for this. The `-a` flag takes in a file.Something like `echo "Body of email" | mutt -s "Subject: PO file '$CUR_DATE'" -e "set content_type=text/html" -e "my_hdr FROM:YYYY@gmail.com" -a /path/to/attached/file XXXX@gmail.com ` – JNevill May 23 '17 at 19:35
  • https://stackoverflow.com/questions/17359/how-do-i-send-a-file-as-an-email-attachment-using-linux-command-line – Will May 23 '17 at 19:37

3 Answers3

1

1

Using mailx -a if mailx -a feature is supported

2

Using uuencode filenm filenm | mailx s@abc.com

3 mutt -a filenm a@abc.com

Nikhil Fadnis
  • 787
  • 5
  • 14
0

If you really want to use sendmail (and not mail or mutt), you'll have to encode your attachment in base64 and concatenate it to your message, along with boundaries and the whole nine yards. There is a great article describing exactly what you want to do here with a code example: http://backreference.org/2013/05/22/send-email-with-attachments-from-script-or-command-line/

If you're on a flavor of Unix or Linux that has mutt or mail, I would definitely recommend one of the two instead of sendmail as it will be a lot easier (and these solutions are also described in the posted article). Here is an example of how you could do it with mail:

CUR_DATE=`date +%Y/%m/%d`
echo $CUR_DATE
to="XXXX@gmail.com"
from="YYYY@gmail.com"
content_type="text/html"
file_to_attach="/home/app111/attachment.csv"
subject="PO file '$CUR_DATE'"
read -r -d '' body << 'EOF'
<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>
<br>Thanks,</br></font></body></html>
EOF
mail -A "$file_to_attach" --content-type "$content_type" -s "$subject" -r "$from" "$to" <<< "$body"
Alex B
  • 1
0

Try this:

MAILFROM="YYYY@gmail.com"
MAILTO="XXXX@gmail.com"
SUBJECT="PO file '$CUR_DATE'"
MAILPART_BODY=q1w2e3r4t5 ## Generates Unique ID
MAILPART=q1qw2ew3r4t35 ## Generates Unique ID
ATTACH="/home/app111/attachment.csv"


(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
 echo ""
 echo "--$MAILPART"
 echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
 echo ""
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/plain; charset=ISO-8859-1"
 echo "You need to enable HTML option for email"
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/html; charset=ISO-8859-1"
 echo "Content-Disposition: inline"
 echo "<html><body><font face="Times New Roman" size="10">Hi All,<br></br>
<br>Please load the attached PO file</br><br/>"
 echo "<br>Thanks,</br></font></body></html>"
 echo "--$MAILPART_BODY--"
 echo "--$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 echo ""
 uuencode $ATTACH $(basename $ATTACH)
 echo "--$MAILPART--"
 ) | sendmail -t
user3437245
  • 347
  • 1
  • 3
  • 14