Sending email in Dancer using Dancer::Plugin::Email

Many sites use email forms to provide a fast and comfortable feedback method. The user just types their details and message and clicks on 'Submit' button, and the details are sent to a specified email address.

If you want to send email from your Dancer application, you can do so easily using the Dancer::Plugin::Email plugin.

How do I use it?

Dancer::Plugin::Email is very simple to use - in fact, there is only one function, email, which sends email with the options specified.. The email call with all options looks like:

email {
    to         => 'user@example.ru',
    subject    => 'I found a bug in your site',
    msg        => 'BlahBlahBlah',
    attachment => ['/sbin/rm'],
    type       => 'text',
    headers    => {
        "X-Mailer" => 'This fine Dancer application',
        "X-Accept-Language" => 'en'
    },
    encoding   => 'base64',
};

Simple example

use Dancer;
use Dancer::Plugin::Email;
    
post '/contact' => sub {
    email {
        to => '...',
        subject => '...',
        message => $msg,
    };
};

Code recipes

Below we give you some code recipes to show how to use Dancer::Plugin::Email

Errors handling

# Handle Email Failures
    
post '/contact' => sub {
    
    my $msg = email {
        to => '...',
        subject => '...',
        message => $msg,
        encoding => 'base64',
    };
        
    warn $msg->{string} if $msg->{type} eq 'failure';
        
};

Adding additional email headers

email {
    to => '...',
    subject => '...',
    message => $msg,
    headers => {
        "X-Mailer" => 'This fine Dancer application',
        "X-Accept-Language" => 'en'
    }
};

Sending text and HTML multi-part messages

email {
    to => '...',
    subject => '...',
    type => 'multi',  # must be set to "multi" to send both parts
    message => {
        text => $txt,
        html => $html,
    },
};

AUTHOR

Kindly authored by Andrew Inishev, a student participating in the Google Code-In, mentored by David Precious - thanks Andrew for your contribution!