You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.4 KiB

10 years ago
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 create {
10 years ago
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) = @_;
10 years ago
eval {
sendmail($email, {from => $self->{from}, to => $to}); 1;
10 years ago
} or do {
return $@->message if ref $@;
return $@;
};
return;
}
1;
=pod
=head1 NAME
LDV::Email -- simple wrapper class to work with emails
=head2 C<new>
my $email = LDV::Email->new(\%opts);
* 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).
10 years ago
=head2 C<send>
my $email = LDV::Email->new(\%opts);
my $error = $email->send($to, $email);
10 years ago
die($result) if $error;
Send previously created email.
Return nothing on success or scalar with description on error.
10 years ago
=cut