Hi,

That long code also uses XS and you could do it shorter using something like:

use Mail::Builder::Simple;
use File::Spec::Functions qw/catdir/;

my $mail = Mail::Builder::Simple->new(
 from => ['[email protected]', 'example.com'],
 reply => '[email protected]',
 subject => 'The subject',
 htmltext => ['template.tmpl', ':TT'],
 plaintext => ['text_template.tmpl', ':TT'],
 sender => 'WordPine::Email',
 template_args => {
   COMPILE_DIR => catdir($ENV{APP_ROOT}, 'tmp'),
   INCLUDE_PATH => catdir($ENV{APP_ROOT}, 'templates'),
   COMPILE_EXT => '.ttc',
 },
);

foreach my $email(@emails) {
 $mail->send(to => $email);
}

The headers and the body are automaticly encoded to UTF-8, you can also add attachments and inline images easy and you can also use other templating systems than Template-Toolkit, like HTML::Template for creating the body parts or the attachments.

Octavian

----- Original Message ----- From: "James.Q.L" <[email protected]>
To: "CGI Application" <[email protected]>
Sent: Sunday, February 01, 2009 8:22 AM
Subject: Re: [cgiapp] Best email module?


--- Lyle <[email protected]> wrote:

Michael Peters wrote:
> Stewart Heckenberg wrote:
>> I like MIME::Lite -- has a very simple attachment interface :)
>
> MIME::Lite is what I've used in the past, although the Perl Email
> folks don't recommend it. Apparently it's very crufty on the insides.
> I think they recommend Email::MIME instead.

Email::MIME looks good, but Email::Stuff seems like an easier interface
to it.

I notice that Email::Send now says to use Email::Sender instead. Anyone
got any experience with this?


I used it a while ago. It did took me some time and some sample codes to figure out how to use bunch of Email::* modules to do what I want. I hope the code speak for itself. :)

use Email::Address;
use Email::MessageID;
use Email::Send;
use Email::Simple;
#use Email::MIME::CreateHTML;
use Email::MIME::Creator;
use Encode qw( encode );
use Template;
use HTML::FormatText::WithLinks;
use File::Spec::Functions qw/catdir/;
use Carp;


$Email::Send::Sendmail::SENDMAIL = '/usr/sbin/sendmail';
my $Sender = Email::Send->new( { mailer => 'Sendmail' } );
my $from_address = Email::Address->new( 'example.com', '[email protected]' )->format();

our $TEMPLATE = Template->new(
   COMPILE_DIR  => catdir( $ENV{APP_ROOT}, 'tmp' ),
   INCLUDE_PATH => catdir( $ENV{APP_ROOT}, 'templates' ),
   COMPILE_EXT  => '.ttc',
);

sub send_email {
   my $class = shift;
   my $p     = shift;

die unless defined $p->{to};
die unless defined $p->{subject};
die unless defined $p->{template};
die unless defined $p->{params};

   my $html_body = _htmlBody( $p );
   my $text_body = _textBody( $html_body );

   # multipart message
   # put the best alternative at the last.
   my @parts = (
     Email::MIME->create(
         attributes => {
             content_type => "text/plain",
#              disposition  => "attachment",
             charset      => "UTF-8",
         },
         body => encode( 'utf8', $text_body ),
     ),
     Email::MIME->create(
         attributes => {
             content_type => "text/html",
#              disposition  => "attachment",
             charset      => "UTF-8",
         },
         body => encode( 'utf8', $html_body ),
     ),
   );

   my %headers =
           ( From         => $p->{from} || $from_address,
             'Reply-To'   => $p->{from} || $from_address,
             To           => $p->{to},
             Subject      => $p->{subject},
             'Message-ID' => q{<} . Email::MessageID->new() . q{>},
             'Content-Transfer-Encoding' => '7bit',
             'X-Sender'                  => 'WordPine::Email',
   );
   # for sending html/plain alternatives email:
   #    need three Email::MIME objects.
   #    One is the top-level multi/aalt part.
   #    the other two are the html/plain alternatives
   my $email = Email::MIME->create(
     header => [ %headers ],
     parts  => [ @parts ],
     attributes => { content_type => 'multipart/alternative', },
   );

   my $rv = $Sender->send( $email );
   warn $rv unless $rv;
}

sub _htmlBody {
   my $p = shift;
   my $tmpl_file = "Email/".  $p->{template} . ".tmpl";
   my $html;
$TEMPLATE->process( $tmpl_file, $p->{params}, \$html ) || croak $TEMPLATE->error();
   return $html;
}

sub _textBody {
   my $html = shift;
   return HTML::FormatText::WithLinks->new()->parse($html);
}

1;


Qiang(James)




#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################



#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################

Reply via email to