NAME

Mojolicious::Plugin::Mail - Mojolicious Plugin for send mail.

SYNOPSIS

# Mojolicious
$self->plugin(mail => {
  from     => 'sharifulin@gmail.com',
  encoding => 'base64',
  type     => 'text/html',
  how      => 'sendmail',
  howargs  => [ '/usr/sbin/sendmail -t' ],
});

# Mojolicious::Lite
plugin mail => { ... };

# in controller
$self->helper('mail',
  mail => {
    To      => 'sharifulin@gmail.com',
    Subject => 'Test email',
    Data    => '<p>Привет!</p>',
  }
);

DESCRIPTION

Mojolicous::Plugin::Mail is a plugin to send mail using MIME::Lite.

HELPERS

Mojolicious::Plugin::Mail contains two helpers: mail and render_mail.

mail

$self->helper('mail',
    test   => 1, # test mode
    mail   => { ... }, # as MIME::Lite->new( ... )
    attach => [
      { ... }, # as MIME::Lite->attach( .. )
      ...
    },
    headers => [
      { ... }, # as MIME::Lite->add( .. )
      ...
    },
    attr => [
      { ... }, # as MIME::Lite->attr( .. )
      ...
    },
);

Build and send email, return mail as string.

Supported parameters:

render_mail

my $data = $self->render_mail( ... ); # any stash params;

Render mail template and return data, mail template format is mail, i.e. controller/action.mail.ep.

ATTRIBUTES

Mojolicious::Plugin::Mail contains one attribute - conf.

conf

$plugin->conf;

Config of mail plugin, hashref.

Keys of hashref:

  • from

    Default from address

  • encoding

    Default encoding of Subject and any Data, value is MIME::Lite content transfer encoding http://search.cpan.org/~rjbs/MIME-Lite-3.027/lib/MIME/Lite.pm#Content_transfer_encodings

  • charset

    Default charset of Subject and any Data, default value is UTF-8

  • type

    Default type of Data, default value is text/plain.

  • how

    HOW parameter of MIME::Lite::send, value are sendmail or smtp

  • howargs

    HOWARGS parameter of MIME::Lite::send (arrayref)

METHODS

Mojolicious::Plugin::Mail inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

$plugin->register($app, $conf);

Register plugin hooks in Mojolicious application.

build

$plugin->build( mail => { ... }, ... );

Build mail using MIME::Lite and MIME::EncWords and return MIME::Lite object.

TEST MODE

Mojolicious::Plugin::Mail has test mode, no send mail.

# all mail don't send mail
BEGIN { $ENV{MOJO_MAIL_TEST} = 1 };

# or only once
$self->helper('mail',
  test => 1,
  mail => { ... },
);

EXAMPLES

Simple send mail:

get '/simple' => sub {
  my $self = shift;
  
  $self->helper('mail',
    mail => {
      To      => 'sharifulin@gmail.com',
      Subject => 'Тест письмо',
      Data    => "<p>Привет!</p>",
    },
  );
};

Simple send mail with test mode:

get '/simple2' => sub {
  my $self = shift;
  
  my $mail = $self->helper('mail',
    test => 1,
    mail => {
      To      => '"Анатолий Шарифулин" sharifulin@gmail.com',
      Cc      => '"Анатолий Шарифулин" <sharifulin@gmail.com>, Anatoly Sharifulin sharifulin@gmail.com',
      Bcc     => 'sharifulin@gmail.com',
      Subject => 'Тест письмо',
      Type    => 'text/plain',
      Data    => "<p>Привет!</p>",
    },
  );
  
  warn $mail;
};

Mail with binary attachcment, charset is windows-1251, mimewords off and mail has custom header:

get '/attach' => sub {
  my $self = shift;
  
  my $mail = $self->helper('mail',
    charset  => 'windows-1251',
    mimeword => 0,

    mail => {
      To      => 'sharifulin@gmail.com',
      Subject => 'Test attach',
      Type    => 'multipart/mixed'
    },
    attach => [
      {
        Data => 'Any data',
      },
      {
        Type        => 'BINARY',
        Filename    => 'crash.data',
        Disposition => 'attachment',
        Data        => 'binary data binary data binary data binary data binary data',
      },
    ],
    headers => [ { 'X-My-Header' => 'Mojolicious' } ],
  );
};

Multipart mixed mail:

get '/multi' => sub {
  my $self = shift;
  
  $self->helper('mail',
    mail => {
      To      => 'sharifulin@gmail.com',
      Subject => 'Мульти',
      Type    => 'multipart/mixed'
    },

    attach => [
      {
        Type     => 'TEXT',
        Encoding => '7bit',
        Data     => "Just a quick note to say hi!"
      },
      {
        Type     => 'image/gif',
        Path     => $0
      },
      {
        Type     => 'x-gzip',
        Path     => "gzip < $0 |",
        ReadNow  => 1,
        Filename => "somefile.zip"
      },
    ],
  );
};

Mail with render data and subject from stash param:

get '/render' => sub {
  my $self = shift;

  my $data = $self->helper('render_mail', 'render');
  $self->helper('mail',
    mail => {
      To      => 'sharifulin@gmail.com',
      Subject => $self->stash('subject'),
      Data    => $data,
    },
  );
} => 'render';

__DATA__

@@ render.html.ep
<p>Hello render!</p>

@@ render.mail.ep
% stash 'subject' => 'Привет render';

<p>Привет mail render!</p>

SEE ALSO

MIME::Lite MIME::EncWords Mojolicious Mojolicious::Guides http://mojolicious.org.

AUTHOR

Anatoly Sharifulin <sharifulin@gmail.com>

THANKS

Alex Kapranoff <kapranoff@gmail.com>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-mail at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.htMail?Queue=Mojolicious-Plugin-Mail. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

COPYRIGHT & LICENSE

Copyright (C) 2010 by Anatoly Sharifulin.

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