The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MojoX::Renderer::Mail - Mail renderer for Mojo and Mojolicious.

Uses MIME::Lite and MIME::EncWords.

All headers such as From, To, Cc, Bcc and Subject are encoded words.

SYNOPSIS

  #!/usr/bin/perl
  use utf8;

  use Mojolicious::Lite;
  use MojoX::Renderer::Mail;

  app->renderer->add_handler(
    mail => MojoX::Renderer::Mail->build(
      from     => 'sharifulin@gmail.com',
      encoding => 'base64',
      how      => 'sendmail',
      howargs  => [ '/usr/sbin/sendmail -t' ],
    ),
  );

Simple mail example:

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

Attachment mail example:

  get '/attach' => sub {
    my $self = shift;
    
    $self->render(
      handler => 'mail',
        # charset  => '...',
        # mimeword => 0,
        
        mail => {
          To      => 'sharifulin@gmail.com',
          Subject => 'Тест аттач',
          Type    => 'multipart/mixed'
        },
        
        attach => [
          {
            Data => 'Текст письма',
          },
          {
            Type        => 'BINARY',
            Filename    => 'crash.data',
            Disposition => 'attachment',
            Data        => 'binary data binary data binary data binary data binary data',
          },
        ],

        headers => [ { 'X-My-Header' => 'Mojolicious' } ],
    );

    $self->render_text('OK');
  };

Multipart mixed mail example:

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

    $self->render(
      handler => '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"
          },
       ],
    );
    
    $self->render_text('OK');
  };

Render mail example:

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

    $self->render(
      handler => 'mail',

      mail => {
        To      => 'sharifulin@gmail.com',
        Subject => 'Тест render',
        Type    => 'text/html',
        Data    => $self->render_partial('render', format => 'mail'),
      },
    );

    $self->render(format => 'html');
  } => 'render';

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

    my $data = $self->render_partial('render2', format => 'mail');
    
    $self->render(
      handler => 'mail',

      mail => {
        To      => 'sharifulin@gmail.com',
        Subject => $self->stash('subject'),
        Type    => 'text/html',
        Data    => $data,
      },
    );

    $self->render(template => 'render', format => 'html');
  } => 'render';
  
  app->start;
  
  __DATA__

  @@ render.html.ep
  <p>Привет render!</p>

  @@ render.mail.ep
  <p>Привет васса render!</p>

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

  <p>Привет васса render2!</p>

Also you can use Mojolicious plugin:

  plugin 'mail' => {
    from     => 'sharifulin@gmail.com',
    encoding => 'base64',
    how      => 'sendmail',
    howargs  => [ '/usr/sbin/sendmail -t' ],
  };

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

See Mojolicious::Plugin::Mail.

METHODS

build

This method returns a handler for the Mojo renderer.

Supported parameters:

RENDER

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

Supported parameters:

ENVIROMENT VARIABLES

Module has two env variables:

  • DEBUG

    Print mail, default value is 0

  • TEST

    No send mail, default value is 0

TEST AND RUN

  TEST=1 DEBUG=1 PATH_INFO='/multi' script/test cgi

SEE ALSO

AUTHOR

Anatoly Sharifulin <sharifulin@gmail.com>

THANKS

Alex Kapranoff <kapranoff@gmail.com>

BUGS

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

SUPPORT

You can find documentation for this module with the perldoc command.

  perldoc MojoX::Renderer::Mail

You can also look for information at:

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.