[cgiapp] Best email module?

James.Q.L shijialeee at yahoo.com
Sun Feb 1 01:22:44 EST 2009


--- Lyle <webmaster at cosmicperl.com> 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', 'do-not-reply at example.com' )->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)


      


More information about the cgiapp mailing list