Activator

 view release on metacpan or  search on metacpan

lib/Activator/Emailer.pm  view on Meta::CPAN

L<CONFIGURATION>), then:

  use Activator::Emailer;
  my $tt_vars = { data => 'any data your template needs',
                  other_data => $my_data,
                };
  my $mailer = Activator::Emailer->new(
     To          => 'person@test.com',
     Cc          => 'other@test.com, other2@test.com'
     Subject     => 'Test Subject',
  );
  $mailer->send( $tt_vars );

Reuse the mailer object to send another mail to someone else using a different
body template, plus an attachment:

  $mailer->set_cc( '' );
  $mailer->set_to( 'someone_else@other-domain.com' );
  $mailer->set_html_body( '/path/to/html_body.tt' );
  $tt_vars = $my_alternate_data_hashref;
  $mailer->attach( Type        => 'application/pdf',
                   Path        => 'path/to/doc.pdf',
                   Filename    => 'doc.pdf',
                   Disposition => 'attachment' );
  $mailer->send( $tt_vars );

=head1 DESCRIPTION

C<Activator::Emailer> is a simple wrapper to L<Mime::Lite>,
L<Template::Toolkit> and L<Email::Send> that uses your project's
L<Activator::Registry> to facilitate easy sending of multipart
text/html email from any module in your project. Emailer can talk to
any MTAs that L<Email::Send> can.

=head2 Full Example

  use Activator::Emailer;
  my $tt_vars = { data => $my_data,
                  other_data => 'any data your template needs'
                };
  my $mailer = Activator::Emailer->new(
     From        => 'no-reply@test.com',
     To          => 'person@test.com',
     Cc          => 'other@test.com, other2@test.com'
     Subject     => 'Test Subject',
     html_header => '/path/to/html_header.tt',
     html_body   => '/path/to/html_body.tt',
     html_footer => '/path/to/html_footer.tt',
     email_wrap  => '/path/to/email/wrapper/template',
     mailer_type => 'Gmail',
     mailer_args => [ username => 'user@gmail.com',
                      password => '123456'
                    ],
  );
  $mailer->attach( Type        => 'application/pdf',
                   Path        => 'path/to/doc.pdf',
                   Filename    => 'invoice.pdf',
                   Disposition => 'attachment'
  $mailer->send( $tt_vars );

The next section shows how to simplify this.

=head1 CONFIGURATION

As is seen in the previous section, a lot of information is needed to
send an email. Fortunately, most of the information is reusable. You
can utilize L<Activator::Registry> to simplify creation of emails:

  'Activator::Registry':
    'Activator::Emailer': 
      From: noreply@domain.com
      mailer_type: Gmail     # any of the send methods Email::Send supports
      mailer_args:           # any of the args required by your Email::Send::<TYPE>
        - username: <username>
        - password: <password>
      html_header: /fully/qualified/path/to/header/tt/template
      html_footer: relative/path/to/footer/tt/template
      html_body:   relative/path/to/body/tt/template
      email_wrap:  /path/to/email/wrapper/template
      tt_options:
        INCLUDE_PATH: /path/to/tt/templates

In the simplist case, you can now send as such:

  my $mailer = Activator::Emailer->new(
     To          => 'person@test.com',
     Subject     => 'Test Subject',
  );
  $mailer->send( { data => $my_data } );


=head1 TEMPLATES SETUP

You must create 4 template for emails to work. Each template has a
variable C<Activator_Emailer_format> available so you can do HTML or text
specific template blocks. Note that it is suggested that you utilize
the TT chomping close tag (C<-%]>) to maintain format.

=head2 File 1: html_header

This is the most basic header, but you can add as much HTML as you
like, including limited style and script tags:

 <html>
 <body>

=head2 File 2: html_body

Put whatever html you like in this section.

 <h1>Body</h1>
 <p>This is only an example</p>
 [% IF Activator_Emailer_format == 'text' -%]
 ========================================
 [% ELSE -%]
 <hr>
 [% END -%]

=head2 File 3: html_footer

This is the most basic footer, but you can add as much HTML as you like:

 </body>
 </html>

=head2 File 4: email_wrap.tt

Copy this verbatim, trim the leading space:

  [% USE HTML.Strip -%]
  [% BLOCK html_header -%]
  [% INCLUDE html_header %]
  [% END -%]
  [% BLOCK html_footer %]
  [% INCLUDE html_footer %]
  [% END -%]
  [% BLOCK body -%]
  [% INCLUDE html_body %]
  [% END -%]
  [% IF format == 'text' -%]
  [% FILTER html_strip emit_spaces = 0 -%]
  [% INCLUDE body %]
  [% END -%]
  [% ELSE -%]
  [% INCLUDE html_header -%]
  [% INCLUDE body %]
  [% INCLUDE html_footer -%]
  [% END -%]

Note that you can put the files anywhere. See L<CONFIGURATION> for more details.

=head1 METHODS

=head2 C<new( %args )>

Create an C<Activator::Emailer> object. Valid C<%args> are described below.

=over

=item *

The following are sent directly to L<MIME::Lite>, and likewise
injected directly into the mail header ( hence the capitalization )

 * From - A single email address
 * To - A single email address
 * Subject - A string
 * Cc - A string consisting of a comma separated list of email addresses



( run in 1.698 second using v1.01-cache-2.11-cpan-39bf76dae61 )