1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2019 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Services;
use Exception;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Site;
use Swift_Mailer;
use Swift_Message;
use Swift_NullTransport;
use Swift_SendmailTransport;
use Swift_Signers_DKIMSigner;
use Swift_SmtpTransport;
use Swift_Transport;
use Throwable;
use function filter_var;
use function function_exists;
use function gethostbyaddr;
use function gethostbyname;
use function gethostname;
use function getmxrr;
use function str_replace;
use function strrchr;
use function substr;
use const FILTER_VALIDATE_DOMAIN;
use const FILTER_VALIDATE_EMAIL;
/**
* Send mail messages.
*/
class MailService
{
/**
* Send an external email message
* Caution! gmail may rewrite the "From" header unless you have added the address to your account.
*
* @param UserInterface $from
* @param UserInterface $to
* @param UserInterface $reply_to
* @param string $subject
* @param string $message_text
* @param string $message_html
*
* @return bool
*/
public function send(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): bool
{
// Mail needs MSDOS line endings
$message_text = str_replace("\n", "\r\n", $message_text);
$message_html = str_replace("\n", "\r\n", $message_html);
// Special accounts do not have an email address. Use the system one.
$from_email = $from->email() ?: $this->senderEmail();
$reply_to_email = $reply_to->email() ?: $this->senderEmail();
$message = (new Swift_Message())
->setSubject($subject)
->setFrom($from_email, $from->realName())
->setTo($to->email(), $to->realName())
->setBody($message_html, 'text/html');
if ($from_email !== $reply_to_email) {
$message->setReplyTo($reply_to_email, $reply_to->realName());
}
$dkim_domain = Site::getPreference('DKIM_DOMAIN');
$dkim_selector = Site::getPreference('DKIM_SELECTOR');
$dkim_key = Site::getPreference('DKIM_KEY');
if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') {
$signer = new Swift_Signers_DKIMSigner($dkim_key, $dkim_domain, $dkim_selector);
$signer
->setHeaderCanon('relaxed')
->setBodyCanon('relaxed');
$message->attachSigner($signer);
} else {
// DKIM body hashes don't work with multipart/alternative content.
$message->addPart($message_text, 'text/plain');
}
$mailer = new Swift_Mailer($this->transport());
try {
$mailer->send($message);
} catch (Exception $ex) {
Log::addErrorLog('MailService: ' . $ex->getMessage());
return false;
}
return true;
}
/**
* Create a transport mechanism for sending mail
*
* @return Swift_Transport
*/
private function transport(): Swift_Transport
{
switch (Site::getPreference('SMTP_ACTIVE')) {
case 'sendmail':
// Local sendmail (requires PHP proc_* functions)
return new Swift_SendmailTransport();
case 'external':
// SMTP
$smtp_host = Site::getPreference('SMTP_HOST');
$smtp_port = (int) Site::getPreference('SMTP_PORT', '25');
$smtp_auth = (bool) Site::getPreference('SMTP_AUTH');
$smtp_user = Site::getPreference('SMTP_AUTH_USER');
$smtp_pass = Site::getPreference('SMTP_AUTH_PASS');
$smtp_encr = Site::getPreference('SMTP_SSL');
$transport = new Swift_SmtpTransport($smtp_host, $smtp_port, $smtp_encr);
$transport->setLocalDomain($this->localDomain());
if ($smtp_auth) {
$transport
->setUsername($smtp_user)
->setPassword($smtp_pass);
}
return $transport;
default:
// For testing
return new Swift_NullTransport();
}
}
/**
* Where are we sending mail from?
*
* @return string
*/
public function localDomain(): string
{
$local_domain = Site::getPreference('SMTP_HELO');
try {
// Look ourself up using DNS.
$default = gethostbyaddr(gethostbyname(gethostname()));
} catch (Throwable $ex) {
$default = 'localhost';
}
return $local_domain ?: $default;
}
/**
* Where are we sending mail from?
*
* @return string
*/
public function senderEmail(): string
{
$sender = Site::getPreference('SMTP_FROM_NAME');
$default = 'no-reply@' . $this->localDomain();
return $sender ?: $default;
}
/**
* Many mail relays require a valid sender email.
*
* @param string $email
*
* @return bool
*/
public function isValidEmail(string $email): bool
{
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
return false;
}
$domain = substr(strrchr($email, '@'), 1);
if (filter_var($domain, FILTER_VALIDATE_DOMAIN) === false) {
return false;
}
return getmxrr($domain, $mxhosts);
}
/**
* A list SSL modes (e.g. for an edit control).
*
* @return string[]
*/
public function mailSslOptions(): array
{
return [
'none' => I18N::translate('none'),
/* I18N: Secure Sockets Layer - a secure communications protocol*/
'ssl' => I18N::translate('ssl'),
/* I18N: Transport Layer Security - a secure communications protocol */
'tls' => I18N::translate('tls'),
];
}
/**
* A list SSL modes (e.g. for an edit control).
*
* @return string[]
*/
public function mailTransportOptions(): array
{
$options = [
/* I18N: "sendmail" is the name of some mail software */
'sendmail' => I18N::translate('Use sendmail to send messages'),
'external' => I18N::translate('Use SMTP to send messages'),
];
if (!function_exists('proc_open')) {
unset($options['sendmail']);
}
return $options;
}
}
|