WebService::Mailgun - API client for Mailgun (https://mailgun.com/)
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',
});
WebService::Mailgun is API client for Mailgun (https://mailgun.com/).
Create mailgun object.
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.
The region attribute determines what region the domain belongs to, either US or EU. Default is US.
return recent error message.
return recent API result status_line.
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
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
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 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
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 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 mailing list.
# delete mailing list
my $res = $mailgun->delete_list('ml@example.com');
https://documentation.mailgun.com/en/latest/api-mailinglists.html#mailing-lists
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 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
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
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 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 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
Get event data.
# get event data
my ($events, $purl) = $mailgun->event({ event => 'stored', limit => 50 });
Get stored message.
# get event data
my ($events, $purl) = $mailgun->event({ event => 'stored' });
my $msg = $mailgun->get_message_from_event($events->[0]);
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 all templates
my $res = $mailgun->delete_templates();
https://documentation.mailgun.com/en/latest/api-templates.html#templates
Delete a template
my $res = $mailgun->delete_template($name);
https://documentation.mailgun.com/en/latest/api-templates.html#templates
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);
// ...
this API not implement yet.
- Domains
- Stats
- Tags
- Suppressions
- Routes
- Webhooks
- Email Validation
- Templates (partial)
WWW::Mailgun, https://documentation.mailgun.com/en/latest/
Copyright (C) Kan Fushihara.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Kan Fushihara kan.fushihara@gmail.com