Sendmails via sendmail with attachments

After Send mail via python it was time to try real `sendmail`.

[ From the FAQ pages of sendmail ]
How do I create attachments with sendmail?
You don't. Sendmail is a mail transfer agent (MTA). Creating e-mail messages, including adding attachments or signatures, is the function of a mail user agent (MUA). Some popular MUAs include mutt, elm, exmh, Netscape, Eudora and Pine. Some specialized packages (metamail, some Perl modules, etc.) can also be used to create messages with attachments.

But yes there is always a way out!
"sendmail" as an MTA, it just sees data.If, we want to make "/usr/sbin/sendmail", as a MUA create a mail with attachments, the answer is no - use a MUA that can, or pre-build the mail and submit via sendmail -t

Hot Dope for simple text and html files can be seen below, the same can be used for others also by switching the content type as "Content-Type: MIMETYPE"

cat > mail << EOF
"From: $From
To: $TO
Subject: $SUBJECT
Mime-Version: 1.0
Content-Type: text/plain\n"

cat YOUR_FILE >> mail

/usr/lib/sendmail -t -oi < mail

A common perl hack, makes things more robust

#!/usr/local/bin/perl -w

use MIME::Lite;

$MAILTO="[email protected]";

#Meta Msg creation
$msg = new MIME::Lite 
   From    => "$ENV{LOGNAME}",
   To      => "$MAILTO",
   Subject => "$SUBJECT",
   Type    => 'multipart/mixed';
# Attach
attach $msg 
   Type     => 'text/plain',   
   Path     => "$PLAINFILE";
attach $msg 
   Type     => 'application/octet-stream',
   Encoding => 'base64',
   Path     => "/tmp/$BINFILE",
   Filename => "$BINFILE";

# Send message to sendmail

open (SENDMAIL, "| /usr/lib/sendmail -t -oi");
Share this