Tools/Mail/Sender.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 47 CC 2 SCAR 10.6
CC
2
Cov
0%
send_mails
6.0
send_mail
2.0
_send_mail
2.0
CRAP 2.9
1 package Daje::Tools::Mail::Sender;
2 use Mojo::Base -base;
3 use v5.42;
4  
5 # NAME
6 # ====
7 #
8 # Daje::Tools::Mail::Sender - A mail sender
9 #
10 # SYNOPSIS
11 # ========
12 #;
13 #
14 # DESCRIPTION
15 # ===========
16 #
17 # Daje::Tools::Mail::Sender sends one or a collection of mails
18 #
19 # METHODS
20 # =======
21 #
22 # my $sender = Daje::Tools::Mail::Sender->new(
23 # smtp => 'smtp server',
24 # account => 'username',
25 # password => 'password'
26 # );
27 #
28 # $sender->send_mail($mail)
29 #
30 # $mail->{recipients} = "comma separated list";
31 # $mail->{subject} = "Whatever subject for this mail";
32 # $mail->{message} = "Probably a HTML message";
33 #
34 # $sender->send_mails($mails)
35 #
36 # $mails a list of $mail
37 #
38 # SEE ALSO
39 # ========
40 #
41 # Mojolicious, Mojolicious::Guides, https://mojolicious.org.
42 #
43 # LICENSE
44 # =======
45 #
46 # Copyright (C) janeskil1525.
47 #
48 # This library is free software; you can redistribute it and/or modify
49 # it under the same terms as Perl itself.
50 #
51 # AUTHOR
52 # ======
53 #
54 # janeskil1525 E<lt>janeskil1525@gmail.com
55 #
56  
57 use Email::Sender::Simple qw(sendmail);
58 use Email::Sender::Transport::SMTP;
59 use Email::MIME;
60  
61 has 'host';
62 has 'account';
63 has 'password';
64  
65 sub send_mail($self, $mail){
66 return $self->_send_mail($mail);
67 }
68  
69 sub send_mails($self, $mails){
70 if($mails->rows){
71 my $collection = $mails->hashes;
72 $collection->each(sub{
73 my $coll = shift;
74 $self->_send_mail($coll);
75 });
76 }
77 }
78  
79 sub _send_mail{
80 my ($self, $mail) = @_;
81  
82 my @parts;
83  
84 push @parts, Email::MIME->create(
85 attributes => {
86 content_type => "text/html",
87 disposition => "attachment",
88 charset => "ISO-8859-1",
89 encoding => 'base64',
90 encode_check => 0,
91 },
92 body_str => $mail->{message},
93 );
94  
95  
96 my $email = Email::MIME->create(
97 header_str => [
98 From => $self->account,
99 To => $mail->{recipients},
100 Subject => $mail->{subject},
101 ],
102 parts => [@parts],
103 );
104  
105 my $transport = Email::Sender::Transport::SMTP->new({
106 host => $self->host,
107 port => 587,
108 ssl => 'starttls',
109 sasl_username => $self->account,
110 sasl_password => $self->password,
111 });
112  
113 sendmail($email, {
114 transport => $transport
115 });
116  
117 return 1;
118 }
119  
120 1;