Created
May 17, 2010 11:42
-
-
Save marcheiligers/403677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Revamp::Mail::MadMimiMail; | |
use Switch; | |
use HTTP::Request::Common; | |
use LWP::UserAgent; | |
use YAML; | |
use Data::Dumper; | |
sub new { | |
my ($class, @args) = @_; | |
my $config = {}; | |
my $agent = LWP::UserAgent->new(); | |
# Set passed parameters into internal object hash | |
$config->{ username } = @args[0]; | |
$config->{ api_key } = @args[1]; | |
my $self = bless({},$class); | |
#stash internal config | |
$self->{'_config'} = $config; | |
$self->{'_user_agent'} = $agent; | |
return $self; | |
} | |
#build accessors for simple fields | |
foreach my $f (qw(username api_key promotion_name recipient subject bcc from raw_html)) { | |
#getters | |
my $field = $f; | |
*{"get_$field"} = sub{ | |
my $self = shift; | |
return $self->{'_config'}->{"$field"}; | |
}; | |
#setters | |
*{"set_$field"} = sub { | |
my $self = shift; | |
@_ or die "calling set_$field without any arguments"; | |
$self->{'_config'}->{"$field"} = shift; | |
return 1; | |
}; | |
} | |
# accessors for body hash | |
sub set_body { | |
my $self = shift; | |
my $body = shift; | |
switch (ref $body) { | |
case "HASH" { $self->{'_config'}->{'body'} = Dump($body) } | |
case "SCALAR" { $self->{'_config'}->{'body'} = $body } | |
else { die "body must be a hash or a yaml string representation of a hash"} | |
} | |
return 1; | |
} | |
sub get_body { | |
my $self = shift; | |
return $self->{'_config'}->{'body'} | |
} | |
sub get_body_as_hash { | |
my $self = shift; | |
return Load($self->{'_config'}->{'body'}); | |
} | |
sub send { | |
my ($self) = @_; | |
$response = $self->{'_user_agent'}->post('https://madmimi.com/mailer', $self->{'_config'}); | |
print STDERR Dumper($response); | |
} | |
$body_hash = { | |
title => 'Them titles', | |
name => 'Them names', | |
body => 'Them bodies' | |
}; | |
# example usage | |
my $mail = Revamp::Mail::MadMimiMail->new('your_mad_mimi_account_address', 'your_mad_mimi_api_key'); | |
$mail->set_promotion_name('Testing'); | |
$mail->set_subject('PERL Hacking'); | |
$mail->set_from('[email protected]'); | |
$mail->set_recipient('[email protected]'); | |
$mail->set_body($body_hash); | |
$mail->send; | |
1; | |
_END_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment