summaryrefslogtreecommitdiff
path: root/app/Services/EmailService.php
blob: e2df35181aeb5704371986eb50068558c6b710d7 (plain)
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
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Services;

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\NullTransport;
use Symfony\Component\Mailer\Transport\SendmailTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Crypto\DkimOptions;
use Symfony\Component\Mime\Crypto\DkimSigner;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Exception\RfcComplianceException;
use Symfony\Component\Mime\Message;

use function assert;
use function checkdnsrr;
use function function_exists;
use function str_replace;
use function strrchr;
use function substr;

/**
 * Send emails.
 */
class EmailService
{
    /**
     * 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
    {
        try {
            $message   = $this->message($from, $to, $reply_to, $subject, $message_text, $message_html);
            $transport = $this->transport();
            $mailer    = new Mailer($transport);
            $mailer->send($message);
        } catch (TransportExceptionInterface $ex) {
            Log::addErrorLog('MailService: ' . $ex->getMessage());

            return false;
        }

        return true;
    }

    /**
     * Create a message
     *
     * @param UserInterface $from
     * @param UserInterface $to
     * @param UserInterface $reply_to
     * @param string        $subject
     * @param string        $message_text
     * @param string        $message_html
     *
     * @return Message
     */
    protected function message(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): Message
    {
        // Mail needs MS-DOS line endings
        $message_text = str_replace("\n", "\r\n", $message_text);
        $message_html = str_replace("\n", "\r\n", $message_html);

        $message = (new Email())
            ->subject($subject)
            ->from(new Address($from->email(), $from->realName()))
            ->to(new Address($to->email(), $to->realName()))
            ->replyTo(new Address($reply_to->email(), $reply_to->realName()))
            ->html($message_html);

        $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 DkimSigner($dkim_key, $dkim_domain, $dkim_selector);
            $options = (new DkimOptions())
                ->headerCanon('relaxed')
                ->bodyCanon('relaxed');

            return $signer->sign($message, $options->toArray());
        }

        // DKIM body hashes don't work with multipart/alternative content.
        $message->text($message_text);

        return $message;
    }

    /**
     * Create a transport mechanism for sending mail
     *
     * @return TransportInterface
     */
    protected function transport(): TransportInterface
    {
        switch (Site::getPreference('SMTP_ACTIVE')) {
            case 'sendmail':
                // Local sendmail (requires PHP proc_* functions)
                $request = app(ServerRequestInterface::class);
                assert($request instanceof ServerRequestInterface);

                $sendmail_command = Validator::attributes($request)->string('sendmail_command', '/usr/sbin/sendmail -bs');

                return new SendmailTransport($sendmail_command);

            case 'external':
                // SMTP
                $smtp_helo = Site::getPreference('SMTP_HELO');
                $smtp_host = Site::getPreference('SMTP_HOST');
                $smtp_port = (int) Site::getPreference('SMTP_PORT');
                $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') === 'ssl';

                $transport = new EsmtpTransport($smtp_host, $smtp_port, $smtp_encr);

                $transport->setLocalDomain($smtp_helo);

                if ($smtp_auth) {
                    $transport
                        ->setUsername($smtp_user)
                        ->setPassword($smtp_pass);
                }

                return $transport;

            default:
                // For testing
                return new NullTransport();
        }
    }

    /**
     * Many mail relays require a valid sender email.
     *
     * @param string $email
     *
     * @return bool
     */
    public function isValidEmail(string $email): bool
    {
        try {
            $address = new Address($email);
        } catch (RfcComplianceException $ex) {
            return false;
        }

        // Some web hosts disable checkdnsrr.
        if (function_exists('checkdnsrr')) {
            $domain = substr(strrchr($address->getAddress(), '@') ?: '@', 1);
            return checkdnsrr($domain);
        }

        return true;
    }

    /**
     * A list SSL modes (e.g. for an edit control).
     *
     * @return array<string>
     */
    public function mailSslOptions(): array
    {
        return [
            'none' => I18N::translate('none'),
            /* I18N: Use SMTP over SSL/TLS, or Implicit TLS - a secure communications protocol */
            'ssl'  => I18N::translate('SSL/TLS'),
            /* I18N: Use SMTP with STARTTLS, or Explicit TLS - a secure communications protocol */
            'tls'  => I18N::translate('STARTTLS'),
        ];
    }

    /**
     * A list SSL modes (e.g. for an edit control).
     *
     * @return array<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;
    }
}