Browse Source

+ LDV::Email->create

master
Alex 'AdUser' Z 10 years ago
parent
commit
e94f262c8b
  1. 30
      lib/LDV/Email.pm
  2. 16
      t/Email.pm

30
lib/LDV/Email.pm

@ -18,19 +18,24 @@ sub new {
return bless($self, $class); return bless($self, $class);
} }
sub send { sub create {
my ($self, $to, $subject, $body) = @_; my ($self, $to, $subject, $body) = @_;
eval { return Email::Simple->create(
my $email = Email::Simple->create( body => $body, header => [
header => [
From => $self->{from}, From => $self->{from},
'Reply-To' => $self->{reply} || $self->{from}, 'Reply-To' => $self->{reply} || $self->{from},
To => $to, To => $to,
Subject => $subject, Subject => $subject,
], ],
body => $body); )->as_string;
sendmail($email, {from => $self->{from}}); 1; }
sub send {
my ($self, $to, $email) = @_;
eval {
sendmail($email, {from => $self->{from}, to => $to}); 1;
} or do { } or do {
return $@->message if ref $@; return $@->message if ref $@;
return $@; return $@;
@ -54,10 +59,21 @@ LDV::Email -- simple wrapper class to work with emails
* from -- default 'From' header * from -- default 'From' header
* reply -- reply to this email instead 'from' * reply -- reply to this email instead 'from'
=head2 C<create>
my $email = LDV::Email->new(\%opts);
my $text = $email->create($to, $subject, $body);
Create email message with some defaults set.
Returns message text (with headers).
=head2 C<send> =head2 C<send>
my $email = LDV::Email->new(\%opts); my $email = LDV::Email->new(\%opts);
my $error = $email->send($to, $subject, $body); my $error = $email->send($to, $email);
die($result) if $error; die($result) if $error;
Send previously created email.
Return nothing on success or scalar with description on error.
=cut =cut

16
t/Email.pm

@ -5,10 +5,22 @@ use warnings;
use utf8; use utf8;
use LDV::Email; use LDV::Email;
use Test::More tests => 2; use Test::More tests => 3;
my $email = LDV::Email->new({from => 'noreply@state.gov.no'}); my $email = LDV::Email->new({from => 'noreply@state.gov.no'});
isa_ok($email, "LDV::Email", "LDV::Email->new"); isa_ok($email, "LDV::Email", "LDV::Email->new");
can_ok($email, qw(send)); can_ok($email, qw(create send));
my @args = ('user@acme.com', 'Registration', 'body text');
my $expected = join("\r\n",
'From: noreply@state.gov.no',
'Reply-To: admin@example.com',
'To: user@acme.com',
'Subject: Registration',
'',
'body text',
'');
my $got = $email->create(@args);
$got =~ s/^Date:.*\r\n//m;
is($got, $expected, "creating of test email");
exit 0; exit 0;

Loading…
Cancel
Save