Regarding help in sendmail command

Hello All,

I have a query here. I want to send an email by sendmail command in BASH. So email should have body(by reading a Input_file) in it as well as it should send same Input_file as attachment too. To do so I have tried as following.

sendmail_touser() {
cat - ${Input_file_HTML} << EOF | /usr/sbin/sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: $1
Content-Type: text/html; charset=us-ascii
cat ${Input_file_HTML}
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Content-Disposition: attachment; filename: ${Input_file_HTML}
EOF
}
 

Above command is giving an email with only attachment of Input_file_HTML and it is not writing it in the body of email, could you please help/guide me on same. I am using outlook as email client.

Thanks,
R. Singh

sendmail_touser() { 
cat - ${Input_file_HTML} << EOF | /usr/sbin/sendmail -oi -t 
From: ${MAILFROM} 
To: ${MAILTO} 
Subject: $1 
Content-Type: text/html; charset=us-ascii 
cat ${Input_file_HTML} 
Content-Transfer-Encoding: 7bit 
MIME-Version: 1.0 
Content-Disposition: attachment; filename: ${Input_file_HTML} 
EOF 
}

AFAIU the colored cat is not being evaluated. If you like to have it done, you should use command expansion

cat - <<EOF
...
$( cat ${input_file_HTML} )
...
EOF

input_file_HTML should be exported too, to be available in the subshell.

My E-Mail(roundcube) client does not display the mail as probably wanted. The header fields are shown as Mailbody.

Hello stomp,

Yes, I have tried but still it is putting code as in form of text into email as follows.

Content-Transfer-Encoding: 7bit MIME-Version: 1.0 Content-Disposition: attachment; filename: 
Input_file_HTML
 

So it is taking that code as a text and printing it before Input_file_HTML's data into email.

Thanks,
R. Singh

Refer thread. If you don't have uuidgen assign a key value manually.

1 Like

Thanks a lot Yoda, I have used following and it sent email with attachment successfully too.

 export MAILPART=$(uuidgen)
export MAILPART_BODY=$(uuidgen)
{
        echo "From: from@domain.com"
        echo "To: chumma@chumma.com"
        echo "Subject: Testing kar reha waan main"
        echo "MIME-Version: 1.0"
        echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
        echo "--$MAILPART"
        echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
        echo ""
        echo "--$MAILPART_BODY"
        echo "Content-Type: text/html"
        echo "Content-Disposition: inline"
        cat $HTML_FORMAT
        echo "--$MAILPART_BODY--"
        echo ""
        echo "--$MAILPART"
        echo "Content-Type: text/plain; name=testCPUID3.txt"
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: attachment; filename=$HTML_FORMAT"
        echo ""
        base64 $HTML_FORMAT
        echo "--$MAILPART--"
} | /usr/sbin/sendmail -t
 

I really appreciate your help on this. Could you please help me in following queries.

  1. When I read man sendmail , I couldn't get these options which we have used above to fix the problem. So could you please share URL or book from where you have referred the same.
  2. If in case uuidgen is not there for someone then which kind of values we should use, could you please put more light on same.
  3. In above could what values will be used for variables named MAILPART and MAILPART_BODY as we have assigned them to uuidgen ?
  4. Also the attached $HTML_FORMAT is having username and server name in it too, how we could avoid it?

Will be grateful to you if you could help me on above points. Thank you for your help and guidance :b:

Thanks,
R. Singh

  1. Refer RFC 2045 (MIME)

  2. Refer RFC1341 (Content-Type / Boundary)

  3. Same as above

  4. I am not sure what you mean by "having username and server name, how we could avoid it?"

1 Like