#!/usr/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Email::Valid;
use MIME::Lite;
use strict;

print header;
print start_html("Results");
print h2("Results");

# first be sure they entered a valid email address.
unless (Email::Valid->address(param('email'))) {
   &dienice("Please enter a valid e-mail address.");
}
print qq(<p>Sending mail, please wait...</p>\n);

# now create the MIME::Lite object
my $msg = MIME::Lite->new(
   From    => 'nullbox@cgi101.com',
   To      => param('email'),
   Subject => 'Fish Pic',
   Type    => 'multipart/mixed');

# add content using the attach method:

$msg->attach( Type => 'TEXT',
              Data => qq(Here is the photo you requested. Visit http://www.cgi101.com/book/ch14/ for more information.) );

$msg->attach( Type => 'image/jpg',
              Path => 'photo.jpg' );

# still have to set this even though we aren't using sendmail
# directly - MIME::Lite is, though, and will complain without this
$ENV{PATH} = '/usr/sbin';

# finally, send the message.
$msg->send('sendmail');

print qq(<p>Your message has been sent. Thank you!</p>);
print end_html;

sub dienice {
    my ($msg) = @_;
    print "<h2>Error</h2>\n";
    print "$msg<p>\n";
    exit;
}