xref: /webtrees/app/Services/EmailService.php (revision 18db2ab658063be21412220f2003ba7401442b71)
1e381f98dSGreg Roach<?php
2e381f98dSGreg Roach
3e381f98dSGreg Roach/**
4e381f98dSGreg Roach * webtrees: online genealogy
5e381f98dSGreg Roach * Copyright (C) 2019 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
15e381f98dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16e381f98dSGreg Roach */
17e381f98dSGreg Roach
18e381f98dSGreg Roachdeclare(strict_types=1);
19e381f98dSGreg Roach
20e381f98dSGreg Roachnamespace Fisharebest\Webtrees\Services;
21e381f98dSGreg Roach
22e381f98dSGreg Roachuse Exception;
23e381f98dSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N;
25e381f98dSGreg Roachuse Fisharebest\Webtrees\Log;
26e381f98dSGreg Roachuse Fisharebest\Webtrees\Site;
27e381f98dSGreg Roachuse Swift_Mailer;
28e381f98dSGreg Roachuse Swift_Message;
29e381f98dSGreg Roachuse Swift_NullTransport;
30e381f98dSGreg Roachuse Swift_SendmailTransport;
31e381f98dSGreg Roachuse Swift_Signers_DKIMSigner;
32e381f98dSGreg Roachuse Swift_SmtpTransport;
33e381f98dSGreg Roachuse Swift_Transport;
34e381f98dSGreg Roachuse Throwable;
35e381f98dSGreg Roach
3604626e75SGreg Roachuse function checkdnsrr;
37e381f98dSGreg Roachuse function filter_var;
38e381f98dSGreg Roachuse function function_exists;
39e381f98dSGreg Roachuse function gethostbyaddr;
40e381f98dSGreg Roachuse function gethostbyname;
41e381f98dSGreg Roachuse function gethostname;
42e381f98dSGreg Roachuse function str_replace;
43e381f98dSGreg Roachuse function strrchr;
44e381f98dSGreg Roachuse function substr;
45e381f98dSGreg Roach
46e381f98dSGreg Roachuse const FILTER_VALIDATE_DOMAIN;
47e381f98dSGreg Roachuse const FILTER_VALIDATE_EMAIL;
48e381f98dSGreg Roach
49e381f98dSGreg Roach/**
50e381f98dSGreg Roach * Send emails.
51e381f98dSGreg Roach */
52e381f98dSGreg Roachclass EmailService
53e381f98dSGreg Roach{
54e381f98dSGreg Roach    /**
55e381f98dSGreg Roach     * Send an external email message
56e381f98dSGreg Roach     * Caution! gmail may rewrite the "From" header unless you have added the address to your account.
57e381f98dSGreg Roach     *
58e381f98dSGreg Roach     * @param UserInterface $from
59e381f98dSGreg Roach     * @param UserInterface $to
60e381f98dSGreg Roach     * @param UserInterface $reply_to
61e381f98dSGreg Roach     * @param string        $subject
62e381f98dSGreg Roach     * @param string        $message_text
63e381f98dSGreg Roach     * @param string        $message_html
64e381f98dSGreg Roach     *
65e381f98dSGreg Roach     * @return bool
66e381f98dSGreg Roach     */
67e381f98dSGreg Roach    public function send(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): bool
68e381f98dSGreg Roach    {
69e381f98dSGreg Roach        // Mail needs MSDOS line endings
70e381f98dSGreg Roach        $message_text = str_replace("\n", "\r\n", $message_text);
71e381f98dSGreg Roach        $message_html = str_replace("\n", "\r\n", $message_html);
72e381f98dSGreg Roach
73e381f98dSGreg Roach        // Special accounts do not have an email address.  Use the system one.
74e381f98dSGreg Roach        $from_email     = $from->email() ?: $this->senderEmail();
75e381f98dSGreg Roach        $reply_to_email = $reply_to->email() ?: $this->senderEmail();
76e381f98dSGreg Roach
77*18db2ab6SGreg Roach        try {
78e381f98dSGreg Roach            $message = (new Swift_Message())
79e381f98dSGreg Roach                ->setSubject($subject)
80e381f98dSGreg Roach                ->setFrom($from_email, $from->realName())
81e381f98dSGreg Roach                ->setTo($to->email(), $to->realName())
82e381f98dSGreg Roach                ->setBody($message_html, 'text/html');
83e381f98dSGreg Roach
84e381f98dSGreg Roach            if ($from_email !== $reply_to_email) {
85e381f98dSGreg Roach                $message->setReplyTo($reply_to_email, $reply_to->realName());
86e381f98dSGreg Roach            }
87e381f98dSGreg Roach
88e381f98dSGreg Roach            $dkim_domain   = Site::getPreference('DKIM_DOMAIN');
89e381f98dSGreg Roach            $dkim_selector = Site::getPreference('DKIM_SELECTOR');
90e381f98dSGreg Roach            $dkim_key      = Site::getPreference('DKIM_KEY');
91e381f98dSGreg Roach
92e381f98dSGreg Roach            if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') {
93e381f98dSGreg Roach                $signer = new Swift_Signers_DKIMSigner($dkim_key, $dkim_domain, $dkim_selector);
94e381f98dSGreg Roach                $signer
95e381f98dSGreg Roach                    ->setHeaderCanon('relaxed')
96e381f98dSGreg Roach                    ->setBodyCanon('relaxed');
97e381f98dSGreg Roach
98e381f98dSGreg Roach                $message->attachSigner($signer);
99e381f98dSGreg Roach            } else {
100e381f98dSGreg Roach                // DKIM body hashes don't work with multipart/alternative content.
101e381f98dSGreg Roach                $message->addPart($message_text, 'text/plain');
102e381f98dSGreg Roach            }
103e381f98dSGreg Roach
104e381f98dSGreg Roach            $mailer = new Swift_Mailer($this->transport());
105e381f98dSGreg Roach
106e381f98dSGreg Roach            $mailer->send($message);
107e381f98dSGreg Roach        } catch (Exception $ex) {
108e381f98dSGreg Roach            Log::addErrorLog('MailService: ' . $ex->getMessage());
109e381f98dSGreg Roach
110e381f98dSGreg Roach            return false;
111e381f98dSGreg Roach        }
112e381f98dSGreg Roach
113e381f98dSGreg Roach        return true;
114e381f98dSGreg Roach    }
115e381f98dSGreg Roach
116e381f98dSGreg Roach    /**
117e381f98dSGreg Roach     * Create a transport mechanism for sending mail
118e381f98dSGreg Roach     *
119e381f98dSGreg Roach     * @return Swift_Transport
120e381f98dSGreg Roach     */
121e381f98dSGreg Roach    private function transport(): Swift_Transport
122e381f98dSGreg Roach    {
123e381f98dSGreg Roach        switch (Site::getPreference('SMTP_ACTIVE')) {
124e381f98dSGreg Roach            case 'sendmail':
125e381f98dSGreg Roach                // Local sendmail (requires PHP proc_* functions)
126e381f98dSGreg Roach                return new Swift_SendmailTransport();
127e381f98dSGreg Roach
128e381f98dSGreg Roach            case 'external':
129e381f98dSGreg Roach                // SMTP
130e381f98dSGreg Roach                $smtp_host = Site::getPreference('SMTP_HOST');
131e381f98dSGreg Roach                $smtp_port = (int) Site::getPreference('SMTP_PORT', '25');
132e381f98dSGreg Roach                $smtp_auth = (bool) Site::getPreference('SMTP_AUTH');
133e381f98dSGreg Roach                $smtp_user = Site::getPreference('SMTP_AUTH_USER');
134e381f98dSGreg Roach                $smtp_pass = Site::getPreference('SMTP_AUTH_PASS');
135e381f98dSGreg Roach                $smtp_encr = Site::getPreference('SMTP_SSL');
136e381f98dSGreg Roach
13783ad74b0SGreg Roach                if ($smtp_encr === 'none') {
13883ad74b0SGreg Roach                    $smtp_encr = null;
13983ad74b0SGreg Roach                }
14083ad74b0SGreg Roach
141e381f98dSGreg Roach                $transport = new Swift_SmtpTransport($smtp_host, $smtp_port, $smtp_encr);
142e381f98dSGreg Roach
143e381f98dSGreg Roach                $transport->setLocalDomain($this->localDomain());
144e381f98dSGreg Roach
145e381f98dSGreg Roach                if ($smtp_auth) {
146e381f98dSGreg Roach                    $transport
147e381f98dSGreg Roach                        ->setUsername($smtp_user)
148e381f98dSGreg Roach                        ->setPassword($smtp_pass);
149e381f98dSGreg Roach                }
150e381f98dSGreg Roach
151e381f98dSGreg Roach                return $transport;
152e381f98dSGreg Roach
153e381f98dSGreg Roach            default:
154e381f98dSGreg Roach                // For testing
155e381f98dSGreg Roach                return new Swift_NullTransport();
156e381f98dSGreg Roach        }
157e381f98dSGreg Roach    }
158e381f98dSGreg Roach
159e381f98dSGreg Roach    /**
160e381f98dSGreg Roach     * Where are we sending mail from?
161e381f98dSGreg Roach     *
162e381f98dSGreg Roach     * @return string
163e381f98dSGreg Roach     */
164e381f98dSGreg Roach    public function localDomain(): string
165e381f98dSGreg Roach    {
166e381f98dSGreg Roach        $local_domain = Site::getPreference('SMTP_HELO');
167e381f98dSGreg Roach
168e381f98dSGreg Roach        try {
169e381f98dSGreg Roach            // Look ourself up using DNS.
170e381f98dSGreg Roach            $default = gethostbyaddr(gethostbyname(gethostname()));
171e381f98dSGreg Roach        } catch (Throwable $ex) {
172e381f98dSGreg Roach            $default = 'localhost';
173e381f98dSGreg Roach        }
174e381f98dSGreg Roach
175e381f98dSGreg Roach        return $local_domain ?: $default;
176e381f98dSGreg Roach    }
177e381f98dSGreg Roach
178e381f98dSGreg Roach    /**
179e381f98dSGreg Roach     * Who are we sending mail from?
180e381f98dSGreg Roach     *
181e381f98dSGreg Roach     * @return string
182e381f98dSGreg Roach     */
183e381f98dSGreg Roach    public function senderEmail(): string
184e381f98dSGreg Roach    {
185e381f98dSGreg Roach        $sender  = Site::getPreference('SMTP_FROM_NAME');
186e381f98dSGreg Roach        $default = 'no-reply@' . $this->localDomain();
187e381f98dSGreg Roach
188e381f98dSGreg Roach        return $sender ?: $default;
189e381f98dSGreg Roach    }
190e381f98dSGreg Roach
191e381f98dSGreg Roach    /**
192e381f98dSGreg Roach     * Many mail relays require a valid sender email.
193e381f98dSGreg Roach     *
194e381f98dSGreg Roach     * @param string $email
195e381f98dSGreg Roach     *
196e381f98dSGreg Roach     * @return bool
197e381f98dSGreg Roach     */
198e381f98dSGreg Roach    public function isValidEmail(string $email): bool
199e381f98dSGreg Roach    {
200ff00880fSGreg Roach        $at_domain = strrchr($email, '@');
201ff00880fSGreg Roach
202ff00880fSGreg Roach        if ($at_domain === false) {
203ff00880fSGreg Roach            return false;
204ff00880fSGreg Roach        }
205ff00880fSGreg Roach
206ff00880fSGreg Roach        $domain = substr($at_domain, 1);
207e381f98dSGreg Roach
20823945a1eSGreg Roach        $email_valid  = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
20923945a1eSGreg Roach        $domain_valid = filter_var($domain, FILTER_VALIDATE_DOMAIN) !== false;
210e381f98dSGreg Roach
21104626e75SGreg Roach        // Some web hosts disable checkdnsrr.
21204626e75SGreg Roach        if ($domain_valid && function_exists('checkdnsrr')) {
21304626e75SGreg Roach            $domain_valid = checkdnsrr($domain);
21404626e75SGreg Roach        }
21504626e75SGreg Roach
21623945a1eSGreg Roach        return $email_valid && $domain_valid;
217e381f98dSGreg Roach    }
218e381f98dSGreg Roach
219e381f98dSGreg Roach    /**
220e381f98dSGreg Roach     * A list SSL modes (e.g. for an edit control).
221e381f98dSGreg Roach     *
222e381f98dSGreg Roach     * @return string[]
223e381f98dSGreg Roach     */
224e381f98dSGreg Roach    public function mailSslOptions(): array
225e381f98dSGreg Roach    {
226e381f98dSGreg Roach        return [
227e381f98dSGreg Roach            'none' => I18N::translate('none'),
228e381f98dSGreg Roach            /* I18N: Secure Sockets Layer - a secure communications protocol*/
229e381f98dSGreg Roach            'ssl'  => I18N::translate('ssl'),
230e381f98dSGreg Roach            /* I18N: Transport Layer Security - a secure communications protocol */
231e381f98dSGreg Roach            'tls'  => I18N::translate('tls'),
232e381f98dSGreg Roach        ];
233e381f98dSGreg Roach    }
234e381f98dSGreg Roach
235e381f98dSGreg Roach    /**
236e381f98dSGreg Roach     * A list SSL modes (e.g. for an edit control).
237e381f98dSGreg Roach     *
238e381f98dSGreg Roach     * @return string[]
239e381f98dSGreg Roach     */
240e381f98dSGreg Roach    public function mailTransportOptions(): array
241e381f98dSGreg Roach    {
242e381f98dSGreg Roach        $options = [
243e381f98dSGreg Roach            /* I18N: "sendmail" is the name of some mail software */
244e381f98dSGreg Roach            'sendmail' => I18N::translate('Use sendmail to send messages'),
245e381f98dSGreg Roach            'external' => I18N::translate('Use SMTP to send messages'),
246e381f98dSGreg Roach        ];
247e381f98dSGreg Roach
248e381f98dSGreg Roach        if (!function_exists('proc_open')) {
249e381f98dSGreg Roach            unset($options['sendmail']);
250e381f98dSGreg Roach        }
251e381f98dSGreg Roach
252e381f98dSGreg Roach        return $options;
253e381f98dSGreg Roach    }
254e381f98dSGreg Roach}
255