0

I need to send mail in unix with 2 attachments and the subject in html format.

i did the following, it helps in getting the attachment but not the body content in the mail

#!/bin/bash
from="b@b.com"
to="A@A.com"
subject="Status"
boundary="ZZ_/afg6432dfgkl.94531q"
body="mail_body.txt"
attachments=( "FiC.txt" "FiE.txt")

# Build headers
{

printf '%s\n' "From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"

--${boundary}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

cat $body
"

for file in "${attachments[@]}"; do

  [ ! -f "$file" ] && echo "Warning: attachment $file not found, skipping" >&2 && continue

 #mimetype=$(get_mimetype "$file") 

  printf '%s\n' "--${boundary}
Content-Type: text/html
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
"

  base64 "$file"
  echo
done

printf '%s\n' "--${boundary}--"

} | /usr/sbin/sendmail -t -oi 

mail_body.txt's contents:

   echo "<html>
        <head>
        <title> Status</title>
        </head>
        "

Can help in getting html content printed in body part of mail?

5
  • Your described output doesn't match the actual result of running that script. Replace /usr/sbin/sendmail -t -oi with nl and try it yourself
    – roaima
    Mar 31 '20 at 17:18
  • what does nl stand for
    – rose
    Mar 31 '20 at 17:20
  • man nl and read up on it
    – roaima
    Mar 31 '20 at 17:47
  • i dont want to number the lines in output. thnaks
    – rose
    Mar 31 '20 at 18:01
  • Sigh. I know you don't want to number the lines. I'm trying to get you to look at the output so you can see what's wrong. This is a fundamental method for debugging - split the problem into smaller parts. In this case I'm discarding the "send the email" part to demonstrate my first comment.
    – roaima
    Mar 31 '20 at 19:50
1
printf '%s\n' "…
cat $body
"

This prints the cat command. It does not run it.

And if you are going to use \n in the format specification, you might as well not use quoted newlines and a single massive string.

Content-Type: text/plain; charset=\"US-ASCII\"

Fairly obviously, a body part designated text/plain will not be interpreted as text/html. Although the contents of that file, as given, are not HTML in the first place.

1
  • i am sorry, i didnt get the solution
    – rose
    Apr 1 '20 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.