#!/usr/bin/perl -w
# ############################ send_mail_multi2.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 MIME::Lite;
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 = MIME::Lite->new;

$mail->build (
  # set the recipients (to) address [REQUIRED]
  To      => 'Test Receiver <support\@frank4dd.com>',
  # set the mail sender address [REQUIRED]
  From    => 'Test Sender <test\@frank4dd.com>',
  # set the envelope sender address [RECOMMENDED]
  Sender  => 'Test Sender <test\@frank4dd.com>',
  # set the carbon copy (cc) recipient address
  # Cc      => '',
  # set the blind carbon copy (bcc) recipient address
  # Bcc     => '',
  # set the  mail subject line
  Subject => 'Test message',
  # set the mail encoding type
  Type    => 'multipart/related'
);

# set the smtp mail server [REQUIRED]
my $mail_server = "127.0.0.1";

# ########################################################################
# 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 = "../images/nagioshowto-icon.png";

# ########################################################################
# Here we assemble the mail parts according to MIME multipart specs.
# ########################################################################
# create the first part, the HTML message
$mail->attach(
  # Set the content type for the first part
  Type        => 'text/html',
  # set the encoding
  Encoding    => '8bit',
  # Add the message itself
  Data        => $mail_content1
);

# Here we add the second e-mail part, the b64-encoded image file
$mail->attach(
  # Set the content type for the first part
  Type        => 'image/png',
  # set the encoding
  Encoding    => 'base64',
  # Define the image to be displayed automatically within the message
  Disposition => 'inline',
  # set the file name
  Filename    => 'logo.png',
  # Add the content id by which this image is referenced
  Id          => $mail_content2_id,
  # Add the picture from a local file
  Path        => $mail_content2
);

print "\n" . $mail->body_as_string . "\n";

# ########################################################################
# send the e-mail out and return verbose information. 
# $mail->send('smtp', $mail_server, AuthUser=>$user, AuthPass=>$pass);
# will attempt SASL authentication. This function uses Net::SMTP::auth().
# ########################################################################
$mail->send('smtp', $mail_server);
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;
}
