The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CAM::EmailTemplate - Template-based email message sender

LICENSE

Copyright 2005 Clotho Advanced Media, Inc., <cpan@clotho.com>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

There are many, many other templating and emailing modules on CPAN. Unless you have a specific reason for using this one, you may have better searching for a different one.

This module is a bit clumsy in the way it sends email (relying on Sendmail), and doesn't thoroughly ensure that the outgoing emails are valid, but it is very handy in its integration with a templating engine.

SYNOPSIS

use CAM::EmailTemplate;

my $template = new CAM::EmailTemplate($filename);
$template->setParams(recipient => 'user@foo.com',
                     bar => 'baz', kelp => 'green');
if ($template->send()) {
   print 'Sent.';
} else {
   print 'Doh!  ' . $template->{sendError};
}

DESCRIPTION

CAM::EmailTemplate extends CAM::Template for sending template-based email messages. The mechanism for sending is 'sendmail -i -t' so this module requires that the computer is a Unixish machine set up for sending. Many simple but handy sanity tests are performed by the send() function.

The template itself must contain all of the requisite mail header info, including 'To:' and 'From:' lines. Read the EXAMPLES section below to see demos of what this looks like.

INSTANCE METHODS

setEnvelopSender ADDRESS

Changed the sender as reported by sendmail to the remote host. Note that this may be visible to the end recipient.

send

Fill the template and send it out. If there is an error (badly formatted message, sendmail error, etc), this function returns undef. In this case, an explanatory string for the error can be obtained from the $template->{sendError} property.

deliver MSG

Delivers the message. This function assumes that the message is properly formatted.

This function should ONLY be called from with the send() method. It is provided here so that it can be overridden by subclasses.

It should return an array of two values: either (true, undef) or (false, errormessage) indicating success or failure.

This particular implementation relies on the existance of a sendmail binary on the host machine.

EXAMPLES

Here is an example template, formatted for consumption by sendmail:

To: ::recipient::
From: "Emailer Script" <emailer@somehost.clotho.com>
Subject: A sample template
MIME-Version: 1.0
Content-Type: text/plain
X-Sender: CAM::EmailTemplate

This is a sample CAM::EmailTemplate file.  The blank line between
the header and the body is crucial.  The 'To:', 'From:' and
'Subject:' lines are required.  The others are optional.

Although this example is indented in the documentation, the real
template should have no indentation in the mail header.

Best wishes,
Chris

Here is another example, with both HTML and plain text versions of the message:

To: "::firstname:: ::lastname::" <::recipient::>
From: "::myName::" <::myEmailAddr::>
Subject: ::subject::
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="----_=_AnotherMIMEPiece"

This message is in MIME format. You will only see this message if
your mail program does not speak MIME.

------_=_AnotherMIMEPiece
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is a sample CAM::EmailTemplate message.  This part is
_plain_text_.  Generally, you should have the same message in both
parts, but this demo breaks that convention.

::myName::
------_=_AnotherMIMEPiece
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is a sample CAM::EmailTemplate message.  This part is <u>html</u>.
Generally, you should have the same message in both parts, but this
demo breaks that convention  slightly.<br><br>::myName::
------_=_AnotherMIMEPiece--

Note that MIME messages are split by the 'boundary' string which can be anything unique. The final boundary should be suffixed with '--', as shown above.

AUTHOR

Clotho Advanced Media Inc., cpan@clotho.com

Primary developer: Chris Dolan