Giter Club home page Giter Club logo

p5-web-service-mailgun's Introduction

Build Status Coverage Status

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    => '[email protected]',
    to      => '[email protected]',
    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    => '[email protected]',
    to      => '[email protected]',
    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      => '[email protected]',
            file    => '/path/to/filename.mime',    
});

# send MIME message via a string:
    use MIME::Entity; 
    my $str = MIME::Entity->build(
            From    => '[email protected]',
    To      => '[email protected]',
    Subject => "Subject",
    Data    => 'Messag4')->as_string;
    
my $res = $mailgun->message({
    to       => '[email protected]',
            message  => $str,
});

# or send MIME message via a string ref:    
my $res = $mailgun->message({
    to       => '[email protected]',
            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 => '[email protected]', # 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('[email protected]');

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('[email protected]' => {
    address => '[email protected]', # 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('[email protected]');

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('[email protected]');

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('[email protected]' => {
    address => '[email protected]', # 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('[email protected]' => {
    members => encode_json [
        { address => '[email protected]' },
        { address => '[email protected]' },
        { address => '[email protected]' },
    ],
    upsert  => 'no',            # no (default). if yes, update exists member
});

# too simple
my $res = $mailgun->add_list_members('[email protected]' => {
    members => encode_json [qw/[email protected] [email protected]/],
});

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('[email protected]', '[email protected]');

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('[email protected]', '[email protected]' => {
    address => '[email protected]', # 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('[email protected]' => '[email protected]');

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 [email protected]

p5-web-service-mailgun's People

Contributors

kan avatar bjornjac avatar justingit avatar joshrabinowitz avatar

Stargazers

Tom Erik Paulsen avatar  avatar

Watchers

 avatar  avatar James Cloos avatar  avatar  avatar

p5-web-service-mailgun's Issues

Failed test 'event results is 110 (2019-2020)'

The test suite started to fail since some days, see http://matrix.cpantesters.org/?dist=WebService-Mailgun%200.07;reports=1#sl=7,1

Excerpt of test log:

    #   Failed test 'event results is 110 (2019-2020)'
    #   at t/04_event.t line 23.
    #          got: '0'
    #     expected: '110'
    # Looks like you failed 1 test of 2.

#   Failed test 'get events'
#   at t/04_event.t line 25.
# Looks like you failed 1 test of 1.
t/04_event.t .... 
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests 

API URL for EU

$API_BASE is hardcoded to the US (non-EU) API URL.

The API URL is different for domains registered as EU (required due to different privacy regulation, GDPR) and need to be api.eu.mailgun.net/v3

Is it possible to add a "eu" flag to the constructor, or maybe a more general "region" attribute and default it to the US one? Can possibly make API_BASE overridable but that seems like a bad idea maintenance wise from the outside world.

If you want, I can modify the code and send the patch.

Sending MIME messages

Are you at all considering adding support for sending raw MIME messages? If not, would you accept a PR?

Using JSON::XS rather than just, JSON

Is there a reason that the JSON::XS module is explicitly used, rather than JSON? For slightly better portability, would simply using, JSON be preferable? JSON will use JSON::XS if it is available, before falling back to a Pure Perl module, or can be configiured to allow you to choose.

Failing non-ascii characters on "message" method

When posting data using the message method, non-ascii characters passed as arguments comes through garbled in messages sent from Mailgun.

When not setting content type to "form-data" (using default), it comes through fine with iso-8859-1 characters. The data is then sent as application/x-www-form-urlencoded (URL paramter string) rather than multipart/form-data (mime chunks).

I have tried most things, like encoding/decoding the arguments, but they always comes out garbled in the mails from Mailgun unless I convert the arguments to 8 bit characters first (not really a fix for those needing wider characters). I don't really know if the problem is on Mailguns side (not handling utf-8 in form-data possibly?) or HTTP::Request/Furl does something weird (the content it generates seems accurate, but with form-data there is no charset passed so it might just assume it's 8 bit latin 1).

I didn't open a pull request because I don't know how it will work with non-8-bit character sets.

Test failure: Cannot create SSL connection vs Cannot resolve host name

On some of my systems the test suite fails like this:

#   Failed test 'error message'
#   at t/03_error.t line 22.
#                   'Cannot create SSL connection: hostname verification failed at /usr/home/cpansand/.cpan/build/2021080206/WebService-Mailgun-0.11-2/blib/lib/WebService/Mailgun.pm line 113.
# '
#     doesn't match '(?^:Cannot resolve host name: )'
# Looks like you failed 1 test of 3.
t/03_error.t ........... 
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/3 subtests 

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.