xref: /webtrees/app/Services/EmailService.php (revision a9866bf217fcbf79695b119a127478d64260caff)
1e381f98dSGreg Roach<?php
2e381f98dSGreg Roach
3e381f98dSGreg Roach/**
4e381f98dSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6e381f98dSGreg Roach * This program is free software: you can redistribute it and/or modify
7e381f98dSGreg Roach * it under the terms of the GNU General Public License as published by
8e381f98dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e381f98dSGreg Roach * (at your option) any later version.
10e381f98dSGreg Roach * This program is distributed in the hope that it will be useful,
11e381f98dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e381f98dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e381f98dSGreg Roach * GNU General Public License for more details.
14e381f98dSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e381f98dSGreg Roach */
17e381f98dSGreg Roach
18e381f98dSGreg Roachdeclare(strict_types=1);
19e381f98dSGreg Roach
20e381f98dSGreg Roachnamespace Fisharebest\Webtrees\Services;
21e381f98dSGreg Roach
22e381f98dSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N;
24e381f98dSGreg Roachuse Fisharebest\Webtrees\Log;
25e381f98dSGreg Roachuse Fisharebest\Webtrees\Site;
26ef641919SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27fa5cbab5SGreg Roachuse Symfony\Component\Mailer\Exception\TransportExceptionInterface;
28e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Mailer;
29e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\NullTransport;
30e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\SendmailTransport;
31e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
32e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\TransportInterface;
33e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Address;
34e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Crypto\DkimOptions;
35e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Crypto\DkimSigner;
36e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Email;
37fa5cbab5SGreg Roachuse Symfony\Component\Mime\Exception\RfcComplianceException;
38e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Message;
39e381f98dSGreg Roach
4094fc28f5SGreg Roachuse function assert;
4104626e75SGreg Roachuse function checkdnsrr;
42e381f98dSGreg Roachuse function function_exists;
43e381f98dSGreg Roachuse function str_replace;
44e381f98dSGreg Roachuse function strrchr;
45e381f98dSGreg Roachuse function substr;
46e381f98dSGreg Roach
47e381f98dSGreg Roach/**
48e381f98dSGreg Roach * Send emails.
49e381f98dSGreg Roach */
50e381f98dSGreg Roachclass EmailService
51e381f98dSGreg Roach{
52e381f98dSGreg Roach    /**
53e381f98dSGreg Roach     * Send an external email message
54e381f98dSGreg Roach     * Caution! gmail may rewrite the "From" header unless you have added the address to your account.
55e381f98dSGreg Roach     *
56e381f98dSGreg Roach     * @param UserInterface $from
57e381f98dSGreg Roach     * @param UserInterface $to
58e381f98dSGreg Roach     * @param UserInterface $reply_to
59e381f98dSGreg Roach     * @param string        $subject
60e381f98dSGreg Roach     * @param string        $message_text
61e381f98dSGreg Roach     * @param string        $message_html
62e381f98dSGreg Roach     *
63e381f98dSGreg Roach     * @return bool
64e381f98dSGreg Roach     */
65e381f98dSGreg Roach    public function send(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): bool
66e381f98dSGreg Roach    {
67e1ae561aSJonathan Jaubart        try {
6805fc85fdSGreg Roach            $message   = $this->message($from, $to, $reply_to, $subject, $message_text, $message_html);
6905fc85fdSGreg Roach            $transport = $this->transport();
70e1ae561aSJonathan Jaubart            $mailer    = new Mailer($transport);
7105fc85fdSGreg Roach            $mailer->send($message);
72fa5cbab5SGreg Roach        } catch (TransportExceptionInterface $ex) {
7305fc85fdSGreg Roach            Log::addErrorLog('MailService: ' . $ex->getMessage());
7405fc85fdSGreg Roach
7505fc85fdSGreg Roach            return false;
7605fc85fdSGreg Roach        }
7705fc85fdSGreg Roach
7805fc85fdSGreg Roach        return true;
7905fc85fdSGreg Roach    }
8005fc85fdSGreg Roach
8105fc85fdSGreg Roach    /**
8205fc85fdSGreg Roach     * Create a message
8305fc85fdSGreg Roach     *
8405fc85fdSGreg Roach     * @param UserInterface $from
8505fc85fdSGreg Roach     * @param UserInterface $to
8605fc85fdSGreg Roach     * @param UserInterface $reply_to
8705fc85fdSGreg Roach     * @param string        $subject
8805fc85fdSGreg Roach     * @param string        $message_text
8905fc85fdSGreg Roach     * @param string        $message_html
9005fc85fdSGreg Roach     *
91e1ae561aSJonathan Jaubart     * @return Message
9205fc85fdSGreg Roach     */
93e1ae561aSJonathan Jaubart    protected function message(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): Message
9405fc85fdSGreg Roach    {
95e2c25bffSGreg Roach        // Mail needs MS-DOS line endings
96e381f98dSGreg Roach        $message_text = str_replace("\n", "\r\n", $message_text);
97e381f98dSGreg Roach        $message_html = str_replace("\n", "\r\n", $message_html);
98e381f98dSGreg Roach
99e1ae561aSJonathan Jaubart        $message = (new Email())
100e1ae561aSJonathan Jaubart            ->subject($subject)
101e1ae561aSJonathan Jaubart            ->from(new Address($from->email(), $from->realName()))
102e1ae561aSJonathan Jaubart            ->to(new Address($to->email(), $to->realName()))
103e1ae561aSJonathan Jaubart            ->replyTo(new Address($reply_to->email(), $reply_to->realName()))
104e1ae561aSJonathan Jaubart            ->html($message_html);
105e381f98dSGreg Roach
106e381f98dSGreg Roach        $dkim_domain   = Site::getPreference('DKIM_DOMAIN');
107e381f98dSGreg Roach        $dkim_selector = Site::getPreference('DKIM_SELECTOR');
108e381f98dSGreg Roach        $dkim_key      = Site::getPreference('DKIM_KEY');
109e381f98dSGreg Roach
110e381f98dSGreg Roach        if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') {
111e1ae561aSJonathan Jaubart            $signer = new DkimSigner($dkim_key, $dkim_domain, $dkim_selector);
112e1ae561aSJonathan Jaubart            $options = (new DkimOptions())
113e1ae561aSJonathan Jaubart                ->headerCanon('relaxed')
114e1ae561aSJonathan Jaubart                ->bodyCanon('relaxed');
115e381f98dSGreg Roach
116e1ae561aSJonathan Jaubart            return $signer->sign($message, $options->toArray());
11705babb96SGreg Roach        }
11805babb96SGreg Roach
119e381f98dSGreg Roach        // DKIM body hashes don't work with multipart/alternative content.
120e1ae561aSJonathan Jaubart        $message->text($message_text);
121e381f98dSGreg Roach
12205fc85fdSGreg Roach        return $message;
123e381f98dSGreg Roach    }
124e381f98dSGreg Roach
125e381f98dSGreg Roach    /**
126e381f98dSGreg Roach     * Create a transport mechanism for sending mail
127e381f98dSGreg Roach     *
128e1ae561aSJonathan Jaubart     * @return TransportInterface
129e381f98dSGreg Roach     */
130e1ae561aSJonathan Jaubart    protected function transport(): TransportInterface
131e381f98dSGreg Roach    {
132e381f98dSGreg Roach        switch (Site::getPreference('SMTP_ACTIVE')) {
133e381f98dSGreg Roach            case 'sendmail':
134e381f98dSGreg Roach                // Local sendmail (requires PHP proc_* functions)
135ef641919SGreg Roach                $request = app(ServerRequestInterface::class);
136ef641919SGreg Roach                assert($request instanceof ServerRequestInterface);
137ef641919SGreg Roach
1384bb3a6faSGreg Roach                $sendmail_command = $request->getAttribute('sendmail_command', '/usr/sbin/sendmail -bs');
139ef641919SGreg Roach
140e1ae561aSJonathan Jaubart                return new SendmailTransport($sendmail_command);
141e381f98dSGreg Roach
142e381f98dSGreg Roach            case 'external':
143e381f98dSGreg Roach                // SMTP
144e2c25bffSGreg Roach                $smtp_helo = Site::getPreference('SMTP_HELO');
145e381f98dSGreg Roach                $smtp_host = Site::getPreference('SMTP_HOST');
1468a07c98eSGreg Roach                $smtp_port = (int) Site::getPreference('SMTP_PORT');
147e381f98dSGreg Roach                $smtp_auth = (bool) Site::getPreference('SMTP_AUTH');
148e381f98dSGreg Roach                $smtp_user = Site::getPreference('SMTP_AUTH_USER');
149e381f98dSGreg Roach                $smtp_pass = Site::getPreference('SMTP_AUTH_PASS');
150e1ae561aSJonathan Jaubart                $smtp_encr = Site::getPreference('SMTP_SSL') === 'ssl';
151e381f98dSGreg Roach
152e1ae561aSJonathan Jaubart                $transport = new EsmtpTransport($smtp_host, $smtp_port, $smtp_encr);
153e381f98dSGreg Roach
154e2c25bffSGreg Roach                $transport->setLocalDomain($smtp_helo);
155e381f98dSGreg Roach
156e381f98dSGreg Roach                if ($smtp_auth) {
157e381f98dSGreg Roach                    $transport
158e381f98dSGreg Roach                        ->setUsername($smtp_user)
159e381f98dSGreg Roach                        ->setPassword($smtp_pass);
160e381f98dSGreg Roach                }
161e381f98dSGreg Roach
162e381f98dSGreg Roach                return $transport;
163e381f98dSGreg Roach
164e381f98dSGreg Roach            default:
165e381f98dSGreg Roach                // For testing
166e1ae561aSJonathan Jaubart                return new NullTransport();
167e381f98dSGreg Roach        }
168e381f98dSGreg Roach    }
169e381f98dSGreg Roach
170e381f98dSGreg Roach    /**
171e381f98dSGreg Roach     * Many mail relays require a valid sender email.
172e381f98dSGreg Roach     *
173e381f98dSGreg Roach     * @param string $email
174e381f98dSGreg Roach     *
175e381f98dSGreg Roach     * @return bool
176e381f98dSGreg Roach     */
177e381f98dSGreg Roach    public function isValidEmail(string $email): bool
178e381f98dSGreg Roach    {
179e1ae561aSJonathan Jaubart        try {
180e1ae561aSJonathan Jaubart            $address = new Address($email);
181fa5cbab5SGreg Roach        } catch (RfcComplianceException $ex) {
182ff00880fSGreg Roach            return false;
183ff00880fSGreg Roach        }
184ff00880fSGreg Roach
18504626e75SGreg Roach        // Some web hosts disable checkdnsrr.
186e1ae561aSJonathan Jaubart        if (function_exists('checkdnsrr')) {
187e1ae561aSJonathan Jaubart            $domain = substr(strrchr($address->getAddress(), '@') ?: '@', 1);
188*a9866bf2SGreg Roach            return checkdnsrr($domain);
18904626e75SGreg Roach        }
19004626e75SGreg Roach
191e1ae561aSJonathan Jaubart        return true;
192e381f98dSGreg Roach    }
193e381f98dSGreg Roach
194e381f98dSGreg Roach    /**
195e381f98dSGreg Roach     * A list SSL modes (e.g. for an edit control).
196e381f98dSGreg Roach     *
19724f2a3afSGreg Roach     * @return array<string>
198e381f98dSGreg Roach     */
199e381f98dSGreg Roach    public function mailSslOptions(): array
200e381f98dSGreg Roach    {
201e381f98dSGreg Roach        return [
202e381f98dSGreg Roach            'none' => I18N::translate('none'),
203e1ae561aSJonathan Jaubart            /* I18N: Use SMTP over SSL/TLS, or Implicit TLS - a secure communications protocol */
204e1ae561aSJonathan Jaubart            'ssl'  => I18N::translate('SSL/TLS'),
205e1ae561aSJonathan Jaubart            /* I18N: Use SMTP with STARTTLS, or Explicit TLS - a secure communications protocol */
206e1ae561aSJonathan Jaubart            'tls'  => I18N::translate('STARTTLS'),
207e381f98dSGreg Roach        ];
208e381f98dSGreg Roach    }
209e381f98dSGreg Roach
210e381f98dSGreg Roach    /**
211e381f98dSGreg Roach     * A list SSL modes (e.g. for an edit control).
212e381f98dSGreg Roach     *
21324f2a3afSGreg Roach     * @return array<string>
214e381f98dSGreg Roach     */
215e381f98dSGreg Roach    public function mailTransportOptions(): array
216e381f98dSGreg Roach    {
217e381f98dSGreg Roach        $options = [
218e381f98dSGreg Roach            /* I18N: "sendmail" is the name of some mail software */
219e381f98dSGreg Roach            'sendmail' => I18N::translate('Use sendmail to send messages'),
220e381f98dSGreg Roach            'external' => I18N::translate('Use SMTP to send messages'),
221e381f98dSGreg Roach        ];
222e381f98dSGreg Roach
223e381f98dSGreg Roach        if (!function_exists('proc_open')) {
224e381f98dSGreg Roach            unset($options['sendmail']);
225e381f98dSGreg Roach        }
226e381f98dSGreg Roach
227e381f98dSGreg Roach        return $options;
228e381f98dSGreg Roach    }
229e381f98dSGreg Roach}
230