diff --git a/lib/LDV/Email.pm b/lib/LDV/Email.pm new file mode 100644 index 0000000..c28feee --- /dev/null +++ b/lib/LDV/Email.pm @@ -0,0 +1,63 @@ +package LDV::Email; + +use strict; +use warnings; +use utf8; + +use Email::Simple; +use Email::Sender::Simple qw(sendmail); + +sub new { + my ($class, $opts) = @_; + my $self = { + from => 'noreply@example.com', + reply => 'admin@example.com', + %$opts, + }; + + return bless($self, $class); +} + +sub send { + my ($self, $to, $subject, $body) = @_; + + 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; + } or do { + return $@->message if ref $@; + return $@; + }; + + return; +} + +1; + +=pod + +=head1 NAME + +LDV::Email -- simple wrapper class to work with emails + +=head2 C + + my $email = LDV::Email->new(\%opts); + +* from -- default 'From' header +* reply -- reply to this email instead 'from' + +=head2 C + + my $email = LDV::Email->new(\%opts); + my $error = $email->send($to, $subject, $body); + die($result) if $error; + +=cut diff --git a/t/Email.pm b/t/Email.pm new file mode 100644 index 0000000..21746c0 --- /dev/null +++ b/t/Email.pm @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use utf8; + +use LDV::Email; +use Test::More tests => 2; + +my $email = LDV::Email->new({from => 'noreply@state.gov.no'}); +isa_ok($email, "LDV::Email", "LDV::Email->new"); +can_ok($email, qw(send)); + +exit 0;