Git Product home page Git Product logo

mojolicious-plugin-mail's Introduction

NAME

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

SYNOPSIS

# Mojolicious::Lite
plugin 'mail';

# Mojolicious with config
$self->plugin(mail => {
  from => '[email protected]',
  type => 'text/html',
});

# in controller
$self->mail(
  to      => '[email protected]',
  subject => 'Test',
  data    => 'use Perl or die;',
);

# in controller, using render
$self->mail(to => '[email protected]', template => 'controller/action', format => 'mail');

# template: controller/action.mail.ep
% stash subject => 'Test';
use Perl or die;

DESCRIPTION

Mojolicous::Plugin::Mail is a plugin for Mojolicious apps to send mail using MIME::Lite. Mojolicious 4.0 ready.

HELPERS

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

mail

# simple interface
$self->mail(
    to       => '[email protected]',
    from     => '[email protected]',
    
    reply_to => '[email protected]',
    
    cc       => '..',
    bcc      => '..',
    
    type     => 'text/plain',

    subject  => 'Test',
    data     => 'use Perl or die;',
);

# interface as MIME::Lite
$self->mail(
    # test mode
    test   => 1,
    
    # as MIME::Lite->new( ... )
    mail   => {
      To      => '[email protected]',
      Subject => 'Test',
      Data    => 'use Perl or die;',
    },

    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:

  • to

    Header 'To' of mail.

  • from

    Header 'From' of mail.

  • reply_to

    Header 'Reply-To' of mail.

  • cc

    Header 'Cc' of mail.

  • bcc

    Header 'Bcc' of mail.

  • type

    Content type of mail, default is conf's type.

  • subject

    Header 'Subject' of mail.

  • data

    Content of mail

  • mail

    Hashref, containts parameters as new(PARAMHASH). See MIME::Lite.

  • attach

    Arrayref of hashref, hashref containts parameters as attach(PARAMHASH). See MIME::Lite.

  • headers

    Arrayref of hashref, hashref containts parameters as add(TAG, VALUE). See MIME::Lite.

  • attr

    Arrayref of hashref, hashref containts parameters as attr(ATTR, VALUE). See MIME::Lite.

  • test

    Test mode, don't send mail.

  • charset

    Charset of mail, default charset is UTF-8.

  • mimeword

    Using mimeword or not, default value is 1.

  • nomailer

    No using 'X-Mailer' header of mail, default value is 1.

If no subject, uses value of stash parameter 'subject'.

If no data, call render_mail helper with all stash parameters.

render_mail

my $data = $self->render_mail('user/signup');

# or use stash params
my $data = $self->render_mail(template => 'user/signup', user => $user);

Render mail template and return data, mail template format is mail, i.e. user/signup.mail.ep.

ATTRIBUTES

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

conf

$plugin->conf;

Config of mail plugin, hashref.

Keys of conf:

my $conf = {
  from     => '[email protected],
  encoding => 'base64',
  type     => 'text/html',
  how      => 'sendmail',
  howargs  => [ '/usr/sbin/sendmail -t' ],
};

# in Mojolicious app
$self->plugin(mail => $conf);

# in Mojolicious::Lite app
plugin mail => $conf;

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->mail(
  test => 1,
  to   => '...',
);

EXAMPLES

The Mojolicious::Lite example you can see in example/test.pl.

Simple interface for send plain mail:

get '/simple' => sub {
  my $self = shift;
  
  $self->mail(
    to      => '[email protected]',
    type    => 'text/plain',
    subject => 'Тест письмо',
    data    => 'Привет!',
  );
};

Simple send mail:

get '/simple' => sub {
  my $self = shift;
  
  $self->mail(
    mail => {
      To      => '[email protected]',
      Subject => 'Тест письмо',
      Data    => "<p>Привет!</p>",
    },
  );
};

Simple send mail with test mode:

get '/simple2' => sub {
  my $self = shift;
  
  my $mail = $self->mail(
    test => 1,
    mail => {
      To      => '"Анатолий Шарифулин" [email protected]',
      Cc      => '"Анатолий Шарифулин" <[email protected]>, Anatoly Sharifulin [email protected]',
      Bcc     => '[email protected]',
      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->mail(
    charset  => 'windows-1251',
    mimeword => 0,

    mail => {
      To      => '[email protected]',
      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->mail(
    mail => {
      To      => '[email protected]',
      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"
      },
    ],
  );
};

Render mail using simple interface and Reply-To header:

get '/render_simple' => sub {
  my $self = shift;
  my $mail = $self->mail(to => '[email protected]', reply_to => '[email protected]');

  $self->render(ok => 1, mail => $mail);
} => 'render';

Mail with render data and subject from stash param:

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

  my $data = $self->render_mail('render');
  $self->mail(
    mail => {
      To      => '[email protected]',
      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 <[email protected]>

THANKS

Alex Kapranoff <[email protected]>

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-2015 by Anatoly Sharifulin.

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

mojolicious-plugin-mail's People

Contributors

borisdaeppen avatar sharifulin avatar simashin avatar zakir-hyder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mojolicious-plugin-mail's Issues

Wide character in subroutine entry at /usr/lib64/perl5/site_perl/5.12.2/MIME/Lite.pm line 2264.

Суть такова:
Плагин подключен как :

$self->plugin(mail => {
    from     => '[email protected]',
    encoding => 'base64',
    how      => 'sendmail',
    howargs  => [ '/usr/sbin/sendmail -t' ],
  });

Вызывается в контроллере :

 $self->mail(to => $params->{user_email}, template => 'mail/registration', format => 'mail', user=>$result);

В шаблоне русский текст, шаблон в utf8;
Все это на линуксе, локаль которого тоже utf8
Mojo, MIME::Lite и Mojolicious::Plugin::Mail последние с cpan.

2263:             /^BASE64/ and do {
2264:                 $out->print( encode_base64( $self->{Data} ) );
2265:                 last DATA;
2266:             };

Receiving mail

Hi,

Here's a little wishlist item, wouldn't it be nice if this plugin also offered a (default) way for receiving incoming mail?

One of the most popular setups these days seems to be a Postfix/Exim server that pipes all incoming mails to curl, which then performs a POST request against the web application with the whole mail attached as a url-encoded parameter.

Perhaps there could be a helper that checks for the presence of such a parameter and returns the parsed mail.

post '/incoming_mail' => sub {
  my $self = shift;

  # Check basic auth or something
  ...

  # Check for presence of mail parameter and render appropriate response (200, 404...)
  return unless my $parsed_mail = $self->mail('parameter_name');

  # Process mail
  ...
};

Doesn't really have to be very sophisticated, but just having the whole process documented in a module like Mojolicious::Plugin::Mail would be awesome. :)

SSL/TLS support

Because this plugin relies on MIME::Lite, it allows neither to send messages with STARTTLS nor with SSL/TLS. In our area, it gets more or less impossible to send messages with standard smtp. Any ideas how we could solve this?

Rolf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.