diff --git a/lib/LDV/Email.pm b/lib/LDV/Email.pm index c28feee..fc59f7a 100644 --- a/lib/LDV/Email.pm +++ b/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 + + 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 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 diff --git a/t/Email.pm b/t/Email.pm index 21746c0..2ab0214 100644 --- a/t/Email.pm +++ b/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;