OK, now that I see my posts are getting through, I'd like to relay one of my responses
to another post made about a week ago. This code is a ready to use emailing subroutine
with support for both plaintext and html emails. It can be easily plugged in to any
script, or placed in a module. If you need support for more robust MIME emailing,
transport encoding, and attatchments, our revered $Bill Lukebert has a solution
available for download at his site.
Try customizing this generic emailing subroutine to your needs;
call it like this:
__BEGIN PERL CODE__
my($toaddress) = 'foo@bar.com'; # required argument
my($toname) = 'John Doe'; # optional argument
my($fromaddress) = 'tommy@atrixnet.com'; # required
my($fromname) = 'Tommy Boy'; # optional
my($content_type) = 'html'; # optional, defaults to plaintext
my($subject) = 'Hi John!'; # required,
# and the next is required as well
my($message) = <<PARSE_TO_END_OF_MESSAGE;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content='text/html; charset="UTF-8"' />
<title> $subject </title>
</head><body>
Dear $toname,
<blockquote>
How are you today?
This message is in <strong>HTML</strong> format!
<br>Can you believe that?!?
Wait 'till Jane finds out what I can do!
</blockquote>
Your pal,
<a href="mailto:$fromaddress">Tommy</a>
</body>
</html>
PARSE_TO_END_OF_MESSAGE
# now call the subroutine with our arguments:
&email( 'to' => $toaddress,
'toname' => $toname,
'from' => $fromaddress,
'fromname' => $fromname,
'subject' => $subject,
'message' => $message,
'content' => $content_type );
sub email {
my $args = { @_ }; # ref-hashify passed args
my $email = ''; # initialize var which will hold email messg
# verify the integrity of caller's request:
# -------------------------------------------------------
# fail without the following arguments...
# -------------------------------------------------------
# fail without a specified sender
die('missing sender')
if (not exists $args->{from})
or (length($args->{from}) == 0);
# fail without specified recipient
die('missing recipient')
if (not exists $args->{to})
or (length($args->{to}) == 0);
# fail without specified subject
die('missing subject')
if (not exists $args->{subject})
or (length($args->{subject}) == 0);
# fail if no message was specified
if (
(not exists $args->{message}) or
(length($args->{message}) == 0)
)
{ die('no message for this email was provided!'); }
# -------------------------------------------------------
# Use default settings without the following arguments...
# -------------------------------------------------------
# default to use sendmail if no mail app specified
$args->{app} = '/usr/lib/sendmail'
if (not exists $args->{app})
or (length($args->{app}) == 0);
# default to text content type for our email if none specified
$args->{contype} = 'plain'
if (not exists $args->{content})
or (length($args->{content}) == 0);
# default to sender's email address if no name is provided
$args->{from_name} = $args->{from}
if (not exists $args->{from_name})
or (length($args->{from_name}) == 0);
# default to recipient's email address if no name is provided
$args->{to_name} = $args->{to}
if (not exists $args->{to_name})
or (length($args->{to_name}) == 0);
# default to an empty string if no reply-to address is provided
if ( (not exists $args->{reply_to}) or
(length($args->{reply_to}) == 0)
) { $args->{reply_to} = '';
$args->{reply_to_name} = '';
}
$args->{reply_to_name} = $args->{reply_to}
if (not exists $args->{reply_to_name});
# otherwise, cat the header label onto the front of the address
if ($args->{reply_to} ne '') {
$args->{reply_to} = 'Reply-To: \"'
.$args->{reply_to_name}
.'\" <'.$args->{reply_to}.">\n";
}
$args->{origin} = $ENV{'HTTP_HOST'} || 'localhost';
# we'll store the MIME headers for our email in $mailheaders
my $mailheaders = <<END_OF_HEADERS;
To: \"$args->{to_name}\" \<$args->{to}\>
From: \"$args->{from_name}\" \<$args->{from}\>
Subject: $args->{subject}
$args->{reply_to}MIME-Version: 1.0
Content-Type: text/$args->{content}; charset=us-ascii
Sender: Perlmailer\@$args->{origin}
X-Mailer: Perlmailer version 1.0
Status: RO\n\n
END_OF_HEADERS
# here we will set the value to the $email variable
$email = $args->{message};
# done with variable initiation and data verification,
# now we'll try to send the email...
# safely open a pipe to mail program or fail with an error message
open(MAIL,"|$args->{app} -oi -t")
or die(qq[
Failed while trying to open a pipe to $args->{app}.
The system returned the following errors:\n
$!\n\n] );
# print out email and close the pipe
print MAIL $mailheaders.$email and close MAIL
or warn('Could not close pipe to'.$args->{app});
return 1;
}
__END__
- Tommy Butler
Atrixnet Web Development º ° º Tomorrow is Now.
web http://Atrixnet.com
email mailto:tommy@Atrixnet.com
tel 8 1 7 . 4 6 8 . 7 7 1 6
fax 8 0 0 . 3 0 7 . 8 1 0 5
6711 Forest Park Drive º ° º Arlington, TX 76001