The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WebService::Mailgun - API client for Mailgun (https://mailgun.com/)

SYNOPSIS

    use WebService::Mailgun;

    my $mailgun = WebService::Mailgun->new(
        api_key => '<YOUR_API_KEY>',
        domain => '<YOUR_MAIL_DOMAIN>',
    );

    # send mail
    my $res = $mailgun->message({
        from    => 'foo@example.com',
        to      => 'bar@example.com',
        subject => 'test',
        text    => 'text',
    });

DESCRIPTION

WebService::Mailgun is API client for Mailgun (https://mailgun.com/).

METHOD

new(api_key => $api_key, domain => $domain, region => "us"|"eu", RaiseError => 0|1)

Create mailgun object.

RaiseError (default: 0)

The RaiseError attribute can be used to force errors to raise exceptions rather than simply return error codes in the normal way. It is "off" by default.

region (default: "us")

The region attribute determines what region the domain belongs to, either US or EU. Default is US.

error

return recent error message.

error_status

return recent API result status_line.

message($args)

Send email message.

    # send mail
    my $res = $mailgun->message({
        from    => 'foo@example.com',
        to      => 'bar@example.com',
        subject => 'test',
        text    => 'text',
    });

https://documentation.mailgun.com/en/latest/api-sending.html#sending

mime($args)

Send a MIME message you build yourself, usually by using a library to create that MIME message. The to parameter needs to be passed as one of the arguments. Either the file or message parameter will also need to be passed.

The file parameter should contain the path to the filename that holds the MIME message. The message parameter should contain either a string or a reference to a string that holds the MIME message:

    # send MIME message via a filename: 
    my $res = $mailgun->message({
        to      => 'bar@example.com',
                file    => '/path/to/filename.mime',    
    });

    # send MIME message via a string:
        use MIME::Entity; 
        my $str = MIME::Entity->build(
                From    => 'foo@example.com',
        To      => 'bar@example.com',
        Subject => "Subject",
        Data    => 'Messag4')->as_string;
        
    my $res = $mailgun->message({
        to       => 'bar@example.com',
                message  => $str,
    });

    # or send MIME message via a string ref:    
    my $res = $mailgun->message({
        to       => 'bar@example.com',
                message  => \$str,
    });

https://documentation.mailgun.com/en/latest/api-sending.html#sending

lists()

Get list of mailing lists.

    # get mailing lists
    my $lists = $mailgun->lists();
    # => ArrayRef of mailing list object.

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

add_list($args)

Add mailing list.

    # add mailing list
    my $res = $mailgun->add_list({
        address => 'ml@example.com', # Mailing list address
        name    => 'ml sample',      # Mailing list name (Optional)
        description => 'sample',     # description (Optional)
        access_level => 'members',   # readonly(default), members, everyone
    });

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

list($address)

Get detail for mailing list.

    # get mailing list detail
    my $data = $mailgun->list('ml@exmaple.com');

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

update_list($address, $args)

Update mailing list detail.

    # update mailing list
    my $res = $mailgun->update_list('ml@example.com' => {
        address => 'ml@example.com', # Mailing list address (Optional)
        name    => 'ml sample',      # Mailing list name (Optional)
        description => 'sample',     # description (Optional)
        access_level => 'members',   # readonly(default), members, everyone
    });

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

delete_list($address)

Delete mailing list.

    # delete mailing list
    my $res = $mailgun->delete_list('ml@example.com');

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

list_members($address)

Get members for mailing list.

    # get members
    my $res = $mailgun->list_members('ml@example.com');

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

add_list_member($address, $args)

Add member for mailing list.

    # add member
    my $res = $mailgun->add_list_member('ml@example.com' => {
        address => 'user@example.com', # member address
        name    => 'username',         # member name (Optional)
        vars    => '{"age": 34}',      # member params(JSON string) (Optional)
        subscribed => 'yes',           # yes(default) or no
        upsert     => 'no',            # no (default). if yes, update exists member
    });

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

add_list_members($address, $args)

Adds multiple members for mailing list.

    use JSON; # auto export 'encode_json'

    # add members
    my $res = $mailgun->add_list_members('ml@example.com' => {
        members => encode_json [
            { address => 'user1@example.com' },
            { address => 'user2@example.com' },
            { address => 'user3@example.com' },
        ],
        upsert  => 'no',            # no (default). if yes, update exists member
    });

    # too simple
    my $res = $mailgun->add_list_members('ml@example.com' => {
        members => encode_json [qw/user1@example.com user2@example.com/],
    });

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

list_member($address, $member_address)

Get member detail.

    # update member
    my $res = $mailgun->list_member('ml@example.com', 'user@example.com');

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

update_list_member($address, $member_address, $args)

Update member detail.

    # update member
    my $res = $mailgun->update_list_member('ml@example.com', 'user@example.com' => {
        address => 'user@example.com', # member address (Optional)
        name    => 'username',         # member name (Optional)
        vars    => '{"age": 34}',      # member params(JSON string) (Optional)
        subscribed => 'yes',           # yes(default) or no
    });

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

delete_list_members($address, $member_address)

Delete member for mailing list.

    # delete member
    my $res = $mailgun->delete_list_member('ml@example.com' => 'user@example.com');

https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists

event($args)

Get event data.

    # get event data
    my ($events, $purl) = $mailgun->event({ event => 'stored', limit => 50 });

Events

get_message_from_event($event)

Get stored message.

    # get event data
    my ($events, $purl) = $mailgun->event({ event => 'stored' });
    my $msg = $mailgun->get_message_from_event($events->[0]);

Stored Message

add_template($args)

Add a template

    # add template
    my $res = $mailgun->add_template({
        name        => 'welcome',     # Template name
        template    => 'Hello!',      # Template data
        engine      => 'handlebars',  # Template engine (optional)
        description => 'xyz',         # Description of template (optional)
        tag         => '2.0' ,        # Version tag (optional)
        comment     => 'Test'         # Version comment (optional)
    });

https://documentation.mailgun.com/en/latest/api-templates.html#templates

delete_templates()

Delete all templates

    my $res = $mailgun->delete_templates();

https://documentation.mailgun.com/en/latest/api-templates.html#templates

delete_template($name)

Delete a template

    my $res = $mailgun->delete_template($name);

https://documentation.mailgun.com/en/latest/api-templates.html#templates

Event Pooling

event method return previous url. it can use for fetch event.

    # event Pooling
    my ($events, $purl) = $mailgun->event({ event => 'stored', begin => localtime->epoch() });
    // do something ...
    $events = $mailgun->event($purl);
    // ...

Event Polling

TODO

this API not implement yet.

SEE ALSO

WWW::Mailgun, https://documentation.mailgun.com/en/latest/

LICENSE

Copyright (C) Kan Fushihara.

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

AUTHOR

Kan Fushihara <kan.fushihara@gmail.com>