Catalyst::Plugin::Email - Send emails with Catalyst


Catalyst-Plugin-Email documentation Contained in the Catalyst-Plugin-Email distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::Email - Send emails with Catalyst

SYNOPSIS

Top

    use Catalyst 'Email';

    __PACKAGE__->config->{email} = [qw/SMTP smtp.oook.de/];

    $c->email(
        header => [
            From    => 'sri@oook.de',
            To      => 'sri@cpan.org',
            Subject => 'Hello!'
        ],
        body => 'Hello sri'
    );

DESCRIPTION

Top

Send emails with Catalyst and Email::Send and Email::MIME::Creator.

CONFIGURATION

Top

config accepts the same options as Email::Send.

To send using the system's sendmail program, set config like so:

    __PACKAGE__->config->{email} = ['Sendmail'];

To send using authenticated SMTP:

    __PACKAGE__->config->{email} = [
        'SMTP', 
        'smtp.myhost.com', 
        username => $USERNAME, 
        password => $PASSWORD, 
    ];

For different methods of sending emails, and appropriate config options, see Email::Send::NNTP, Email::Send::Qmail, Email::Send::SMTP and Email::Send::Sendmail.

METHODS

Top

email

email() accepts the same arguments as Email::MIME::Creator's create().

    $c->email(
        header => [
            To      => 'me@localhost',
            Subject => 'A TT Email',
        ],
        body => $c->subreq( '/render_email' ),
    );

To send a multipart message, include a parts argument containing an arrayref of Email::MIME objects.

    my @parts = (
        Email::MIME->create(
            attributes => {
                content_type => 'application/pdf',
                encoding     => 'quoted-printable',
                name         => 'report.pdf',
            },
            body => $FILE_DATA,
        ),
        Email::MIME->create(
            attributes => {
                content_type => 'text/plain',
                disposition  => 'attachment',
                charset      => 'US-ASCII',
            },
            body => $c->subreq( '/render_email' ),
        ),
    );

    $c->email(
        header => [
            To      => 'me@localhost',
            Subject => 'A TT Email',
        ],
        parts => \@parts,
    );

USING WITH A VIEW

Top

A common practice is to handle emails using the same template language used for HTML pages. If your view supports the 'render' method (Like the TT view does), you just set the body like this: $c->email( header => [ To => 'me@localhost', Subject => 'A TT Email', ], body => $c->view('TT')->render($c,'mytemplate.tt'), }

If your view doesn't support render, you can just forward to it, then reset the body like this:

    sub send_email : Local {
        my ( $self, $c ) = @_;  
        {
        local $c->stash->{names}   = [ qw/andyg sri mst/ ],
        local $c->stash->{template}= 'mytemplate.tt';   
        $c->forward($c->view('MyView'));
        $c->email(
            header => [
                To      => 'me@localhost',
                Subject => 'A TT Email',
            ],
            body => $c->res->body,
        );
        $c->res->body(undef);
        }
    }

And the template:

    [%- FOREACH name IN names -%]
    Hi, [% name %]!
    [%- END -%]

    --
    Regards,
    Us

Output:

    Hi, andyg!
    Hi, sri!
    Hi, mst!

    --
    Regards,
    Us

SEE ALSO

Top

Catalyst, Catalyst::Plugin::SubRequest, Email::Send, Email::MIME::Creator

AUTHOR

Top

Sebastian Riedel, sri@cpan.org Andy Grundman Carl Franks Marcus Ramberg mramberg@cpan.org

COPYRIGHT

Top


Catalyst-Plugin-Email documentation Contained in the Catalyst-Plugin-Email distribution.
package Catalyst::Plugin::Email;

use strict;
use Email::Send;
use Email::MIME;
use Email::MIME::Creator;
use Carp qw/croak/;

our $VERSION = '0.08';

sub email {
    my $c = shift;
    my $email = $_[1] ? {@_} : $_[0];
    $email = Email::MIME->create(%$email);
    my $args = $c->config->{email} || [];
    my @args = @{$args};
    my $class;
    unless ( $class = shift @args ) {
        $class = 'SMTP';
        unshift @args, 'localhost';
    }
    send $class => $email, @args;
}

1;