1e381f98dSGreg Roach<?php 2e381f98dSGreg Roach 3e381f98dSGreg Roach/** 4e381f98dSGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg Roach * Copyright (C) 2022 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; 26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 27ef641919SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 28fa5cbab5SGreg Roachuse Symfony\Component\Mailer\Exception\TransportExceptionInterface; 29e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Mailer; 30e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\NullTransport; 31e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\SendmailTransport; 32e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; 33e1ae561aSJonathan Jaubartuse Symfony\Component\Mailer\Transport\TransportInterface; 34e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Address; 35e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Crypto\DkimOptions; 36e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Crypto\DkimSigner; 37e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Email; 38fa5cbab5SGreg Roachuse Symfony\Component\Mime\Exception\RfcComplianceException; 39e1ae561aSJonathan Jaubartuse Symfony\Component\Mime\Message; 40e381f98dSGreg Roach 4194fc28f5SGreg Roachuse function assert; 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); 73fa5cbab5SGreg Roach } catch (TransportExceptionInterface $ex) { 7405fc85fdSGreg Roach Log::addErrorLog('MailService: ' . $ex->getMessage()); 7505fc85fdSGreg Roach 7605fc85fdSGreg Roach return false; 7705fc85fdSGreg Roach } 7805fc85fdSGreg Roach 7905fc85fdSGreg Roach return true; 8005fc85fdSGreg Roach } 8105fc85fdSGreg Roach 8205fc85fdSGreg Roach /** 8305fc85fdSGreg Roach * Create a message 8405fc85fdSGreg Roach * 8505fc85fdSGreg Roach * @param UserInterface $from 8605fc85fdSGreg Roach * @param UserInterface $to 8705fc85fdSGreg Roach * @param UserInterface $reply_to 8805fc85fdSGreg Roach * @param string $subject 8905fc85fdSGreg Roach * @param string $message_text 9005fc85fdSGreg Roach * @param string $message_html 9105fc85fdSGreg Roach * 92e1ae561aSJonathan Jaubart * @return Message 9305fc85fdSGreg Roach */ 94e1ae561aSJonathan Jaubart protected function message(UserInterface $from, UserInterface $to, UserInterface $reply_to, string $subject, string $message_text, string $message_html): Message 9505fc85fdSGreg Roach { 96e2c25bffSGreg Roach // Mail needs MS-DOS line endings 97e381f98dSGreg Roach $message_text = str_replace("\n", "\r\n", $message_text); 98e381f98dSGreg Roach $message_html = str_replace("\n", "\r\n", $message_html); 99e381f98dSGreg Roach 100e1ae561aSJonathan Jaubart $message = (new Email()) 101e1ae561aSJonathan Jaubart ->subject($subject) 102e1ae561aSJonathan Jaubart ->from(new Address($from->email(), $from->realName())) 103e1ae561aSJonathan Jaubart ->to(new Address($to->email(), $to->realName())) 104e1ae561aSJonathan Jaubart ->replyTo(new Address($reply_to->email(), $reply_to->realName())) 105e1ae561aSJonathan Jaubart ->html($message_html); 106e381f98dSGreg Roach 107e381f98dSGreg Roach $dkim_domain = Site::getPreference('DKIM_DOMAIN'); 108e381f98dSGreg Roach $dkim_selector = Site::getPreference('DKIM_SELECTOR'); 109e381f98dSGreg Roach $dkim_key = Site::getPreference('DKIM_KEY'); 110e381f98dSGreg Roach 111e381f98dSGreg Roach if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') { 112e1ae561aSJonathan Jaubart $signer = new DkimSigner($dkim_key, $dkim_domain, $dkim_selector); 113e1ae561aSJonathan Jaubart $options = (new DkimOptions()) 114e1ae561aSJonathan Jaubart ->headerCanon('relaxed') 115e1ae561aSJonathan Jaubart ->bodyCanon('relaxed'); 116e381f98dSGreg Roach 117e1ae561aSJonathan Jaubart return $signer->sign($message, $options->toArray()); 11805babb96SGreg Roach } 11905babb96SGreg Roach 120e381f98dSGreg Roach // DKIM body hashes don't work with multipart/alternative content. 121e1ae561aSJonathan Jaubart $message->text($message_text); 122e381f98dSGreg Roach 12305fc85fdSGreg Roach return $message; 124e381f98dSGreg Roach } 125e381f98dSGreg Roach 126e381f98dSGreg Roach /** 127e381f98dSGreg Roach * Create a transport mechanism for sending mail 128e381f98dSGreg Roach * 129e1ae561aSJonathan Jaubart * @return TransportInterface 130e381f98dSGreg Roach */ 131e1ae561aSJonathan Jaubart protected function transport(): TransportInterface 132e381f98dSGreg Roach { 133e381f98dSGreg Roach switch (Site::getPreference('SMTP_ACTIVE')) { 134e381f98dSGreg Roach case 'sendmail': 135e381f98dSGreg Roach // Local sendmail (requires PHP proc_* functions) 136ef641919SGreg Roach $request = app(ServerRequestInterface::class); 137ef641919SGreg Roach assert($request instanceof ServerRequestInterface); 138ef641919SGreg Roach 139b55cbc6bSGreg Roach $sendmail_command = Validator::attributes($request)->string('sendmail_command', '/usr/sbin/sendmail -bs'); 140ef641919SGreg Roach 141e1ae561aSJonathan Jaubart return new SendmailTransport($sendmail_command); 142e381f98dSGreg Roach 143e381f98dSGreg Roach case 'external': 144e381f98dSGreg Roach // SMTP 145e2c25bffSGreg Roach $smtp_helo = Site::getPreference('SMTP_HELO'); 146e381f98dSGreg Roach $smtp_host = Site::getPreference('SMTP_HOST'); 1478a07c98eSGreg Roach $smtp_port = (int) Site::getPreference('SMTP_PORT'); 148e381f98dSGreg Roach $smtp_auth = (bool) Site::getPreference('SMTP_AUTH'); 149e381f98dSGreg Roach $smtp_user = Site::getPreference('SMTP_AUTH_USER'); 150e381f98dSGreg Roach $smtp_pass = Site::getPreference('SMTP_AUTH_PASS'); 151e1ae561aSJonathan Jaubart $smtp_encr = Site::getPreference('SMTP_SSL') === 'ssl'; 152e381f98dSGreg Roach 153e1ae561aSJonathan Jaubart $transport = new EsmtpTransport($smtp_host, $smtp_port, $smtp_encr); 154e381f98dSGreg Roach 155e2c25bffSGreg Roach $transport->setLocalDomain($smtp_helo); 156e381f98dSGreg Roach 157e381f98dSGreg Roach if ($smtp_auth) { 158e381f98dSGreg Roach $transport 159e381f98dSGreg Roach ->setUsername($smtp_user) 160e381f98dSGreg Roach ->setPassword($smtp_pass); 161e381f98dSGreg Roach } 162e381f98dSGreg Roach 163e381f98dSGreg Roach return $transport; 164e381f98dSGreg Roach 165e381f98dSGreg Roach default: 166e381f98dSGreg Roach // For testing 167e1ae561aSJonathan Jaubart return new NullTransport(); 168e381f98dSGreg Roach } 169e381f98dSGreg Roach } 170e381f98dSGreg Roach 171e381f98dSGreg Roach /** 172e381f98dSGreg Roach * Many mail relays require a valid sender email. 173e381f98dSGreg Roach * 174e381f98dSGreg Roach * @param string $email 175e381f98dSGreg Roach * 176e381f98dSGreg Roach * @return bool 177e381f98dSGreg Roach */ 178e381f98dSGreg Roach public function isValidEmail(string $email): bool 179e381f98dSGreg Roach { 180e1ae561aSJonathan Jaubart try { 181e1ae561aSJonathan Jaubart $address = new Address($email); 182fa5cbab5SGreg Roach } catch (RfcComplianceException $ex) { 183ff00880fSGreg Roach return false; 184ff00880fSGreg Roach } 185ff00880fSGreg Roach 18604626e75SGreg Roach // Some web hosts disable checkdnsrr. 187e1ae561aSJonathan Jaubart if (function_exists('checkdnsrr')) { 188e1ae561aSJonathan Jaubart $domain = substr(strrchr($address->getAddress(), '@') ?: '@', 1); 189a9866bf2SGreg Roach return checkdnsrr($domain); 19004626e75SGreg Roach } 19104626e75SGreg Roach 192e1ae561aSJonathan Jaubart return true; 193e381f98dSGreg Roach } 194e381f98dSGreg Roach 195e381f98dSGreg Roach /** 196e381f98dSGreg Roach * A list SSL modes (e.g. for an edit control). 197e381f98dSGreg Roach * 19824f2a3afSGreg Roach * @return array<string> 199e381f98dSGreg Roach */ 200e381f98dSGreg Roach public function mailSslOptions(): array 201e381f98dSGreg Roach { 202e381f98dSGreg Roach return [ 203e381f98dSGreg Roach 'none' => I18N::translate('none'), 204e1ae561aSJonathan Jaubart /* I18N: Use SMTP over SSL/TLS, or Implicit TLS - a secure communications protocol */ 205e1ae561aSJonathan Jaubart 'ssl' => I18N::translate('SSL/TLS'), 206e1ae561aSJonathan Jaubart /* I18N: Use SMTP with STARTTLS, or Explicit TLS - a secure communications protocol */ 207e1ae561aSJonathan Jaubart 'tls' => I18N::translate('STARTTLS'), 208e381f98dSGreg Roach ]; 209e381f98dSGreg Roach } 210e381f98dSGreg Roach 211e381f98dSGreg Roach /** 212e381f98dSGreg Roach * A list SSL modes (e.g. for an edit control). 213e381f98dSGreg Roach * 21424f2a3afSGreg Roach * @return array<string> 215e381f98dSGreg Roach */ 216e381f98dSGreg Roach public function mailTransportOptions(): array 217e381f98dSGreg Roach { 218e381f98dSGreg Roach $options = [ 219e381f98dSGreg Roach /* I18N: "sendmail" is the name of some mail software */ 220e381f98dSGreg Roach 'sendmail' => I18N::translate('Use sendmail to send messages'), 221e381f98dSGreg Roach 'external' => I18N::translate('Use SMTP to send messages'), 222e381f98dSGreg Roach ]; 223e381f98dSGreg Roach 224e381f98dSGreg Roach if (!function_exists('proc_open')) { 225e381f98dSGreg Roach unset($options['sendmail']); 226e381f98dSGreg Roach } 227e381f98dSGreg Roach 228e381f98dSGreg Roach return $options; 229e381f98dSGreg Roach } 230e381f98dSGreg Roach} 231