Browse Source

+ LDV::Email->create

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

38
lib/LDV/Email.pm

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

16
t/Email.pm

@ -5,10 +5,22 @@ use warnings;
use utf8;
use LDV::Email;
use Test::More tests => 2;
use Test::More tests => 3;
my $email = LDV::Email->new({from => 'noreply@state.gov.no'});
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;

Loading…
Cancel
Save