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 36e381f98dSGreg Roachuse function filter_var; 37e381f98dSGreg Roachuse function function_exists; 38e381f98dSGreg Roachuse function gethostbyaddr; 39e381f98dSGreg Roachuse function gethostbyname; 40e381f98dSGreg Roachuse function gethostname; 41e381f98dSGreg Roachuse function getmxrr; 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 77e381f98dSGreg Roach $message = (new Swift_Message()) 78e381f98dSGreg Roach ->setSubject($subject) 79e381f98dSGreg Roach ->setFrom($from_email, $from->realName()) 80e381f98dSGreg Roach ->setTo($to->email(), $to->realName()) 81e381f98dSGreg Roach ->setBody($message_html, 'text/html'); 82e381f98dSGreg Roach 83e381f98dSGreg Roach if ($from_email !== $reply_to_email) { 84e381f98dSGreg Roach $message->setReplyTo($reply_to_email, $reply_to->realName()); 85e381f98dSGreg Roach } 86e381f98dSGreg Roach 87e381f98dSGreg Roach $dkim_domain = Site::getPreference('DKIM_DOMAIN'); 88e381f98dSGreg Roach $dkim_selector = Site::getPreference('DKIM_SELECTOR'); 89e381f98dSGreg Roach $dkim_key = Site::getPreference('DKIM_KEY'); 90e381f98dSGreg Roach 91e381f98dSGreg Roach if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') { 92e381f98dSGreg Roach $signer = new Swift_Signers_DKIMSigner($dkim_key, $dkim_domain, $dkim_selector); 93e381f98dSGreg Roach $signer 94e381f98dSGreg Roach ->setHeaderCanon('relaxed') 95e381f98dSGreg Roach ->setBodyCanon('relaxed'); 96e381f98dSGreg Roach 97e381f98dSGreg Roach $message->attachSigner($signer); 98e381f98dSGreg Roach } else { 99e381f98dSGreg Roach // DKIM body hashes don't work with multipart/alternative content. 100e381f98dSGreg Roach $message->addPart($message_text, 'text/plain'); 101e381f98dSGreg Roach } 102e381f98dSGreg Roach 103e381f98dSGreg Roach $mailer = new Swift_Mailer($this->transport()); 104e381f98dSGreg Roach 105e381f98dSGreg Roach try { 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 137*83ad74b0SGreg Roach if ($smtp_encr === 'none') { 138*83ad74b0SGreg Roach $smtp_encr = null; 139*83ad74b0SGreg Roach } 140*83ad74b0SGreg 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 { 200e381f98dSGreg Roach if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 201e381f98dSGreg Roach return false; 202e381f98dSGreg Roach } 203e381f98dSGreg Roach 204e381f98dSGreg Roach $domain = substr(strrchr($email, '@'), 1); 205e381f98dSGreg Roach 206e381f98dSGreg Roach if (filter_var($domain, FILTER_VALIDATE_DOMAIN) === false) { 207e381f98dSGreg Roach return false; 208e381f98dSGreg Roach } 209e381f98dSGreg Roach 210e381f98dSGreg Roach return getmxrr($domain, $mxhosts); 211e381f98dSGreg Roach } 212e381f98dSGreg Roach 213e381f98dSGreg Roach /** 214e381f98dSGreg Roach * A list SSL modes (e.g. for an edit control). 215e381f98dSGreg Roach * 216e381f98dSGreg Roach * @return string[] 217e381f98dSGreg Roach */ 218e381f98dSGreg Roach public function mailSslOptions(): array 219e381f98dSGreg Roach { 220e381f98dSGreg Roach return [ 221e381f98dSGreg Roach 'none' => I18N::translate('none'), 222e381f98dSGreg Roach /* I18N: Secure Sockets Layer - a secure communications protocol*/ 223e381f98dSGreg Roach 'ssl' => I18N::translate('ssl'), 224e381f98dSGreg Roach /* I18N: Transport Layer Security - a secure communications protocol */ 225e381f98dSGreg Roach 'tls' => I18N::translate('tls'), 226e381f98dSGreg Roach ]; 227e381f98dSGreg Roach } 228e381f98dSGreg Roach 229e381f98dSGreg Roach /** 230e381f98dSGreg Roach * A list SSL modes (e.g. for an edit control). 231e381f98dSGreg Roach * 232e381f98dSGreg Roach * @return string[] 233e381f98dSGreg Roach */ 234e381f98dSGreg Roach public function mailTransportOptions(): array 235e381f98dSGreg Roach { 236e381f98dSGreg Roach $options = [ 237e381f98dSGreg Roach /* I18N: "sendmail" is the name of some mail software */ 238e381f98dSGreg Roach 'sendmail' => I18N::translate('Use sendmail to send messages'), 239e381f98dSGreg Roach 'external' => I18N::translate('Use SMTP to send messages'), 240e381f98dSGreg Roach ]; 241e381f98dSGreg Roach 242e381f98dSGreg Roach if (!function_exists('proc_open')) { 243e381f98dSGreg Roach unset($options['sendmail']); 244e381f98dSGreg Roach } 245e381f98dSGreg Roach 246e381f98dSGreg Roach return $options; 247e381f98dSGreg Roach } 248e381f98dSGreg Roach} 249