1e381f98dSGreg Roach<?php 2e381f98dSGreg Roach 3e381f98dSGreg Roach/** 4e381f98dSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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; 25*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry; 26e381f98dSGreg Roachuse Fisharebest\Webtrees\Site; 27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 28ef641919SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29fa5cbab5SGreg Roachuse Symfony\Component\Mailer\Exception\TransportExceptionInterface; 30e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Mailer; 31e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\NullTransport; 32e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\SendmailTransport; 33e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; 34e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\TransportInterface; 35e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Address; 36e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Crypto\DkimOptions; 37e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Crypto\DkimSigner; 38e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Email; 39fa5cbab5SGreg Roachuse Symfony\Component\Mime\Exception\RfcComplianceException; 40e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Message; 41e381f98dSGreg Roach 4204626e75SGreg Roachuse function checkdnsrr; 43e381f98dSGreg Roachuse function function_exists; 44e381f98dSGreg Roachuse function str_replace; 45e381f98dSGreg Roachuse function strrchr; 46e381f98dSGreg Roachuse function substr; 47e381f98dSGreg Roach 48e381f98dSGreg Roach/** 49e381f98dSGreg Roach * Send emails. 50e381f98dSGreg Roach */ 51e381f98dSGreg Roachclass EmailService 52e381f98dSGreg Roach{ 53e381f98dSGreg Roach /** 54e381f98dSGreg Roach * Send an external email message 55e381f98dSGreg Roach * Caution! gmail may rewrite the "From" header unless you have added the address to your account. 56e381f98dSGreg Roach * 57e381f98dSGreg Roach * @param UserInterface $from 58e381f98dSGreg Roach * @param UserInterface $to 59e381f98dSGreg Roach * @param UserInterface $reply_to 60e381f98dSGreg Roach * @param string $subject 61e381f98dSGreg Roach * @param string $message_text 62e381f98dSGreg Roach * @param string $message_html 63e381f98dSGreg Roach * 64e381f98dSGreg Roach * @return bool 65e381f98dSGreg Roach */ 66e381f98dSGreg Roach public function send(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): bool 67e381f98dSGreg Roach { 68e1ae561aSJonathan Jaubart try { 6905fc85fdSGreg Roach $message = $this->message($from, $to, $reply_to, $subject, $message_text, $message_html); 7005fc85fdSGreg Roach $transport = $this->transport(); 71e1ae561aSJonathan Jaubart $mailer = new Mailer($transport); 7205fc85fdSGreg Roach $mailer->send($message); 7366fffb93SGreg Roach } catch (RfcComplianceException $ex) { 7466fffb93SGreg Roach Log::addErrorLog('Cannot create email ' . $ex->getMessage()); 7566fffb93SGreg Roach 7666fffb93SGreg Roach return false; 77fa5cbab5SGreg Roach } catch (TransportExceptionInterface $ex) { 7866fffb93SGreg Roach Log::addErrorLog('Cannot send email: ' . $ex->getMessage()); 7905fc85fdSGreg Roach 8005fc85fdSGreg Roach return false; 8105fc85fdSGreg Roach } 8205fc85fdSGreg Roach 8305fc85fdSGreg Roach return true; 8405fc85fdSGreg Roach } 8505fc85fdSGreg Roach 8605fc85fdSGreg Roach /** 8705fc85fdSGreg Roach * Create a message 8805fc85fdSGreg Roach * 8905fc85fdSGreg Roach * @param UserInterface $from 9005fc85fdSGreg Roach * @param UserInterface $to 9105fc85fdSGreg Roach * @param UserInterface $reply_to 9205fc85fdSGreg Roach * @param string $subject 9305fc85fdSGreg Roach * @param string $message_text 9405fc85fdSGreg Roach * @param string $message_html 9505fc85fdSGreg Roach * 96e1ae561aSJonathan Jaubart * @return Message 9705fc85fdSGreg Roach */ 98e1ae561aSJonathan Jaubart protected function message(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): Message 9905fc85fdSGreg Roach { 100e2c25bffSGreg Roach // Mail needs MS-DOS line endings 101e381f98dSGreg Roach $message_text = str_replace("\n", "\r\n", $message_text); 102e381f98dSGreg Roach $message_html = str_replace("\n", "\r\n", $message_html); 103e381f98dSGreg Roach 104e1ae561aSJonathan Jaubart $message = (new Email()) 105e1ae561aSJonathan Jaubart ->subject($subject) 106e1ae561aSJonathan Jaubart ->from(new Address($from->email(), $from->realName())) 107e1ae561aSJonathan Jaubart ->to(new Address($to->email(), $to->realName())) 108e1ae561aSJonathan Jaubart ->replyTo(new Address($reply_to->email(), $reply_to->realName())) 109e1ae561aSJonathan Jaubart ->html($message_html); 110e381f98dSGreg Roach 111e381f98dSGreg Roach $dkim_domain = Site::getPreference('DKIM_DOMAIN'); 112e381f98dSGreg Roach $dkim_selector = Site::getPreference('DKIM_SELECTOR'); 113e381f98dSGreg Roach $dkim_key = Site::getPreference('DKIM_KEY'); 114e381f98dSGreg Roach 115e381f98dSGreg Roach if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') { 116e1ae561aSJonathan Jaubart $signer = new DkimSigner($dkim_key, $dkim_domain, $dkim_selector); 117e1ae561aSJonathan Jaubart $options = (new DkimOptions()) 118e1ae561aSJonathan Jaubart ->headerCanon('relaxed') 119e1ae561aSJonathan Jaubart ->bodyCanon('relaxed'); 120e381f98dSGreg Roach 121e1ae561aSJonathan Jaubart return $signer->sign($message, $options->toArray()); 12205babb96SGreg Roach } 12305babb96SGreg Roach 124e381f98dSGreg Roach // DKIM body hashes don't work with multipart/alternative content. 125e1ae561aSJonathan Jaubart $message->text($message_text); 126e381f98dSGreg Roach 12705fc85fdSGreg Roach return $message; 128e381f98dSGreg Roach } 129e381f98dSGreg Roach 130e381f98dSGreg Roach /** 131e381f98dSGreg Roach * Create a transport mechanism for sending mail 132e381f98dSGreg Roach * 133e1ae561aSJonathan Jaubart * @return TransportInterface 134e381f98dSGreg Roach */ 135e1ae561aSJonathan Jaubart protected function transport(): TransportInterface 136e381f98dSGreg Roach { 137e381f98dSGreg Roach switch (Site::getPreference('SMTP_ACTIVE')) { 138e381f98dSGreg Roach case 'sendmail': 139e381f98dSGreg Roach // Local sendmail (requires PHP proc_* functions) 140*d35568b4SGreg Roach $request = Registry::container()->get(ServerRequestInterface::class); 141b55cbc6bSGreg Roach $sendmail_command = Validator::attributes($request)->string('sendmail_command', '/usr/sbin/sendmail -bs'); 142ef641919SGreg Roach 143e1ae561aSJonathan Jaubart return new SendmailTransport($sendmail_command); 144e381f98dSGreg Roach 145e381f98dSGreg Roach case 'external': 146e381f98dSGreg Roach // SMTP 147e2c25bffSGreg Roach $smtp_helo = Site::getPreference('SMTP_HELO'); 148e381f98dSGreg Roach $smtp_host = Site::getPreference('SMTP_HOST'); 1498a07c98eSGreg Roach $smtp_port = (int) Site::getPreference('SMTP_PORT'); 150e381f98dSGreg Roach $smtp_auth = (bool) Site::getPreference('SMTP_AUTH'); 151e381f98dSGreg Roach $smtp_user = Site::getPreference('SMTP_AUTH_USER'); 152e381f98dSGreg Roach $smtp_pass = Site::getPreference('SMTP_AUTH_PASS'); 153e1ae561aSJonathan Jaubart $smtp_encr = Site::getPreference('SMTP_SSL') === 'ssl'; 154e381f98dSGreg Roach 155e1ae561aSJonathan Jaubart $transport = new EsmtpTransport($smtp_host, $smtp_port, $smtp_encr); 156e381f98dSGreg Roach 157e2c25bffSGreg Roach $transport->setLocalDomain($smtp_helo); 158e381f98dSGreg Roach 159e381f98dSGreg Roach if ($smtp_auth) { 160e381f98dSGreg Roach $transport 161e381f98dSGreg Roach ->setUsername($smtp_user) 162e381f98dSGreg Roach ->setPassword($smtp_pass); 163e381f98dSGreg Roach } 164e381f98dSGreg Roach 165e381f98dSGreg Roach return $transport; 166e381f98dSGreg Roach 167e381f98dSGreg Roach default: 168e381f98dSGreg Roach // For testing 169e1ae561aSJonathan Jaubart return new NullTransport(); 170e381f98dSGreg Roach } 171e381f98dSGreg Roach } 172e381f98dSGreg Roach 173e381f98dSGreg Roach /** 174e381f98dSGreg Roach * Many mail relays require a valid sender email. 175e381f98dSGreg Roach * 176e381f98dSGreg Roach * @param string $email 177e381f98dSGreg Roach * 178e381f98dSGreg Roach * @return bool 179e381f98dSGreg Roach */ 180e381f98dSGreg Roach public function isValidEmail(string $email): bool 181e381f98dSGreg Roach { 182e1ae561aSJonathan Jaubart try { 183e1ae561aSJonathan Jaubart $address = new Address($email); 18428d026adSGreg Roach } catch (RfcComplianceException) { 185ff00880fSGreg Roach return false; 186ff00880fSGreg Roach } 187ff00880fSGreg Roach 18804626e75SGreg Roach // Some web hosts disable checkdnsrr. 189e1ae561aSJonathan Jaubart if (function_exists('checkdnsrr')) { 190e1ae561aSJonathan Jaubart $domain = substr(strrchr($address->getAddress(), '@') ?: '@', 1); 191a9866bf2SGreg Roach return checkdnsrr($domain); 19204626e75SGreg Roach } 19304626e75SGreg Roach 194e1ae561aSJonathan Jaubart return true; 195e381f98dSGreg Roach } 196e381f98dSGreg Roach 197e381f98dSGreg Roach /** 198e381f98dSGreg Roach * A list SSL modes (e.g. for an edit control). 199e381f98dSGreg Roach * 20024f2a3afSGreg Roach * @return array<string> 201e381f98dSGreg Roach */ 202e381f98dSGreg Roach public function mailSslOptions(): array 203e381f98dSGreg Roach { 204e381f98dSGreg Roach return [ 205e381f98dSGreg Roach 'none' => I18N::translate('none'), 206e1ae561aSJonathan Jaubart /* I18N: Use SMTP over SSL/TLS, or Implicit TLS - a secure communications protocol */ 207e1ae561aSJonathan Jaubart 'ssl' => I18N::translate('SSL/TLS'), 208e1ae561aSJonathan Jaubart /* I18N: Use SMTP with STARTTLS, or Explicit TLS - a secure communications protocol */ 209e1ae561aSJonathan Jaubart 'tls' => I18N::translate('STARTTLS'), 210e381f98dSGreg Roach ]; 211e381f98dSGreg Roach } 212e381f98dSGreg Roach 213e381f98dSGreg Roach /** 214e381f98dSGreg Roach * A list SSL modes (e.g. for an edit control). 215e381f98dSGreg Roach * 21624f2a3afSGreg Roach * @return array<string> 217e381f98dSGreg Roach */ 218e381f98dSGreg Roach public function mailTransportOptions(): array 219e381f98dSGreg Roach { 220e381f98dSGreg Roach $options = [ 221e381f98dSGreg Roach /* I18N: "sendmail" is the name of some mail software */ 222e381f98dSGreg Roach 'sendmail' => I18N::translate('Use sendmail to send messages'), 223e381f98dSGreg Roach 'external' => I18N::translate('Use SMTP to send messages'), 224e381f98dSGreg Roach ]; 225e381f98dSGreg Roach 226e381f98dSGreg Roach if (!function_exists('proc_open')) { 227e381f98dSGreg Roach unset($options['sendmail']); 228e381f98dSGreg Roach } 229e381f98dSGreg Roach 230e381f98dSGreg Roach return $options; 231e381f98dSGreg Roach } 232e381f98dSGreg Roach} 233