Reputation: 1
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: [email protected]"
print "From: [email protected]"
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`
Upvotes: 0
Views: 5449
Reputation: 347
Try this:
MAILFROM="[email protected]"
MAILTO="[email protected]"
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
Upvotes: 0
Reputation: 1
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="[email protected]"
from="[email protected]"
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"
Upvotes: 0
Reputation: 797
1
Using mailx -a
if mailx -a feature is supported
2
Using uuencode filenm filenm | mailx [email protected]
3
mutt -a filenm [email protected]
Upvotes: 1