#!/usr/bin/perl -w
# ############################ send_mail_multi.pl ########################
# Date    : Dec 31 2010
# Purpose : Perl example script for sending out multipart e-mails
# Author  : Frank Migge (support at frank4dd dot com)
# URL     : http://fm4dd.com/programming
# License : GPL - http://www.fsf.org/licenses/gpl.txt
# Version : 1.0 initial release
# ########################################################################
use Mail::Sendmail;
use Digest::MD5 qw(md5_hex);
use strict;
use warnings;

# ########################################################################
# define the e-mail participants, server and content
# ########################################################################
# create the main e-mail hash structure [REQUIRED]
my %mail;
# set the smtp mail server [REQUIRED]
$mail{smtp}    = "127.0.0.1";
# set the recipients (to) address [REQUIRED]
$mail{To}      = "Test Receiver <support\@frank4dd.com>";
# set the mail sender address [REQUIRED]
$mail{From}    = "Test Sender <test\@frank4dd.com>";
# set the envelope sender address [RECOMMENDED]
$mail{Sender}  = "Test Sender <test\@frank4dd.com>";
# set the carbon copy (cc) recipient address
# $mail{Cc}      = undef;
# set the blind carbon copy (bcc) recipient address
# $mail{Bcc}     = undef;
# set the  mail subject line
$mail{subject} = "Test message";

# ########################################################################
# We need to create a unique boundary string to use throughout the e-mail
# ########################################################################
my $boundary = create_boundary();
# set the mail encoding type
$mail{'content-type'} = qq(multipart/related; boundary="$boundary");

# ########################################################################
# Here is a example HTML content. We want to display a inline picture
# that will be part2 of the e-mail content. We need to reference this
# picture through a specific ID which we create with create_content_id().
# ########################################################################
my $mail_content2_id = create_content_id();
my $mail_content1  = "<html><body bgcolor=\"#669999\"><h2>Title</h2><hr>\n"
. "<img src=\"cid:$mail_content2_id\">\n"
. "<p>This is a HTML test messsage.</p></body></html>\n";

# ########################################################################
# Here is a example picture (Nagios logo) converted into a b64-encoded
# file. See http://frank4dd.com/howto/various about methods to encode
# or GIF, JPG or PNG images into base64 format.
# ########################################################################
my $mail_content2 = 
  "R0lGODlhQABAAPYAAAAAAAcHCwcJCggGBQoGCAsLCxINDRQTFBYVGRsWGBgYFhsbGyAeHCMjIyUl\n"
. "KikkJCwrLCQxKDAxLjM0MzQ1ODg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DPY2qMSZIZJCpkXLFlyAQAHJhweUGgwIwuTSTXQRU7GABABU0M9QAACwwxRFEF\n"
. "NLBBCTokOEgSHTAYFHEZ7YPMClR1AAIIBwCgQhZKSIBfTPwN4MIWTWS4kQ8LbiBFFw8y2EMWOaxw\n"
. "AgSFLdCCWd0A0QCDNHT+gYMCRnFgkooTAEHFERgEMAIVUqwwgHEz3AdACVcoQQEAExwBRQYANODD\n"
. "FDSUSIESVSTxzBIwMLkBE/4cURQEPVhTglEXRBGEPA288AIGs5lw4xEqCoBAiV9ewcNlEgxhRQuz\n"
. "SRBCUQK0cEUPFSTAAQ4zXJbCeAB2MEAFQ3RDRAcLFBACFE+gYMAAC0wAggQRsNAPjxtY4MJ1+0jR\n"
. "RAgFdKAEgDBAsMABEnTQQhI4ksCAo7mmMIQ6WQzBgw83blFSDz0kkQ4TOcBQQ09EAOHEFlDgcMQT\n"
. "T2QBGAAgSKHFETpY2s0VR/QQVBNWcOGFFk/wMAMNOTAhhUyCwJvDDkOICDzDxTwQwUQ1XXCB0z9d\n"
. "KDFBA9LCMGYBMESj0EAC7QLyLoJcUYMFEEAggQQTVDCBzUMuAkULCAxwAAQIIKBCU5yIhUMOPOgQ\n"
. "Q3g81BDeFYt0AUUOMcywQw42IM1JLgAVA7MivWzxj8G2pK322my37fbbcMct99x012333Xjnrffe\n"
. "fPftdyGBAAA7";

# ########################################################################
# Here we assemble the mail parts according to MIME multipart specs.
# We start with a message for non-MIME compliant mail clients.
# MIME-compliant clients ignore everything until the in content-type
# specified boundary string starts, indicated with two dashes '--'
# ########################################################################
$mail{body} = "This is a multi-part message in MIME format.\n"
# create the first boundary start marker for the first part
. "--$boundary\n"
# Set the content type and encoding for the first part
. "Content-Type: text/html; charset=utf-8\n"
. "Content-Transfer-Encoding: 8bit\n\n"
# Add the message itself
. "$mail_content1\n";

# Here we add the second e-mail part to be send, the b64-encoded image file
$mail{body} = $mail{body}
# create the second boundary start marker for part 2
. "--$boundary\n"
# Set the content type and encoding for the 2nd part
. "Content-Type: image/gif; name=\"logo.gif\"\n"
. "Content-Transfer-Encoding: base64\n"
# Add the content id by which this image is referenced
. "Content-ID: <$mail_content2_id>\n"
# Define the image to be displayed automatically within the message
. "Content-Disposition: inline; filename=\"logo.gif\"\n\n"
# Add the image itself
. "$mail_content2\n";

# We are finished and we add the final end boundary marker
$mail{body} = $mail{body} . "--$boundary--\n";
print "\n" . $mail{body} . "\n";

# ########################################################################
# send the e-mail out and return verbose information
# ########################################################################
sendmail(%mail) or die $Mail::Sendmail::error;
print "The sendmail log reports:\n".$Mail::Sendmail::log."\n";
exit 0;

# ########################################################################
# The content ID is needed to reference the inline image within other mail
# parts.
# ########################################################################
sub create_content_id {
  my $unique_string  = rand(100);
  $unique_string  = $unique_string . substr(md5_hex(time()),0,23);
  $unique_string  =~ s/(.{5})/$1\./g;
  $unique_string = qq(part.${unique_string}\@) . "MAIL";
  return $unique_string;
}

# ########################################################################
# create_boundary creates the MIME multipart boundary string
# ########################################################################
sub create_boundary {
  my $unique_string  = substr(md5_hex(time()),0,24);
  $unique_string     = '======' . $unique_string . '======';
  return $unique_string;
}
