Bug 22343: Add classes for handling SMTP servers
[koha.git] / Koha / SMTP / Server.pm
1 package Koha::SMTP::Server;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Exceptions::Object;
22 use Koha::SMTP::Servers;
23
24 use Email::Sender::Transport::SMTP;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::SMTP::Server - Koha SMTP Server Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =head3 transport
37
38     my $transport = $smtp_server->transport;
39     sendmail( $message, { transport => $transport } );
40
41 Returns an I<Email::Sender::Transport::SMTP> object that can be used directly
42 with Email::Sender.
43
44 =cut
45
46 sub transport {
47     my ($self) = @_;
48
49     my $params = {
50         host => $self->host,
51         port => $self->port,
52     };
53
54     $params->{ssl} = $self->ssl_mode
55         unless $self->ssl_mode eq 'disabled';
56
57     $params->{timeout} = $self->timeout
58         if $self->timeout;
59
60     $params->{sasl_username} = $self->user_name
61         if $self->user_name;
62
63     $params->{sasl_password} = $self->password
64         if $self->password;
65
66
67     my $transport = Email::Sender::Transport::SMTP->new( $params );
68
69     return $transport;
70 }
71
72 =head3 libraries
73
74     my $libraries = $smtp_server->libraries
75
76 Accessor to get the list of libraries that are linked to this SMTP server
77
78 =cut
79
80 sub libraries {
81     my ($self) = @_;
82
83     my @library_ids = $self->_result->library_smtp_servers->get_column('library_id')->all;
84     return Koha::Libraries->search( { branchcode => { -in => \@library_ids } } );
85 }
86
87 =head3 is_system_default
88
89     if ( $smtp_server->is_system_default ) { ... }
90
91 Method that tells if a Koha::SMTP::Server is the hardcoded one.
92
93 =cut
94
95 sub is_system_default {
96     my ($self) = @_;
97
98     return $self->{_is_system_default};
99 }
100
101 =head3 to_api_mapping
102
103 This method returns the mapping for representing a Koha::SMTP::Server object
104 on the API.
105
106 =cut
107
108 sub to_api_mapping {
109     return {
110         id => 'smtp_server_id'
111     };
112 }
113
114 =head2 Internal methods
115
116 =head3 _type
117
118 Return type of Object relating to Schema ResultSet
119
120 =cut
121
122 sub _type {
123     return 'SmtpServer';
124 }
125
126 1;