1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16namespace Fisharebest\Webtrees; 17 18use Exception; 19use Swift_Mailer; 20use Swift_MailTransport; 21use Swift_Message; 22use Swift_NullTransport; 23use Swift_Preferences; 24use Swift_SendmailTransport; 25use Swift_SmtpTransport; 26use Swift_Transport; 27 28/** 29 * Send mail messages. 30 */ 31class Mail 32{ 33 const EOL = "<br>\r\n"; // End-of-line that works for both TEXT and HTML messages 34 35 /** 36 * Send an external email message 37 * Caution! gmail may rewrite the "From" header unless you have added the address to your account. 38 * 39 * @param Tree $tree 40 * @param string $to_email 41 * @param string $to_name 42 * @param string $replyto_email 43 * @param string $replyto_name 44 * @param string $subject 45 * @param string $message 46 * 47 * @return bool 48 */ 49 public static function send(Tree $tree, $to_email, $to_name, $replyto_email, $replyto_name, $subject, $message) 50 { 51 try { 52 // Swiftmailer uses the PHP default tmp directory. On some servers, this 53 // is outside the open_basedir list. Therefore we must set one explicitly. 54 File::mkdir(WT_DATA_DIR . 'tmp'); 55 56 Swift_Preferences::getInstance()->setTempDir(WT_DATA_DIR . 'tmp'); 57 58 $mail = Swift_Message::newInstance() 59 ->setSubject($subject) 60 ->setFrom(Site::getPreference('SMTP_FROM_NAME'), $tree->getPreference('title')) 61 ->setTo($to_email, $to_name) 62 ->setReplyTo($replyto_email, $replyto_name) 63 ->setBody($message, 'text/html') 64 ->addPart(Filter::unescapeHtml($message), 'text/plain'); 65 66 Swift_Mailer::newInstance(self::transport())->send($mail); 67 } catch (Exception $ex) { 68 Log::addErrorLog('Mail: ' . $ex->getMessage()); 69 70 return false; 71 } 72 73 return true; 74 } 75 76 /** 77 * Send an automated system message (such as a password reminder) from a tree to a user. 78 * 79 * @param Tree $tree 80 * @param User $user 81 * @param string $subject 82 * @param string $message 83 * 84 * @return bool 85 */ 86 public static function systemMessage(Tree $tree, User $user, $subject, $message) 87 { 88 return self::send( 89 $tree, 90 $user->getEmail(), $user->getRealName(), 91 Site::getPreference('SMTP_FROM_NAME'), $tree->getPreference('title'), 92 $subject, 93 $message 94 ); 95 } 96 97 /** 98 * Create a transport mechanism for sending mail 99 * 100 * @return Swift_Transport 101 */ 102 public static function transport() 103 { 104 switch (Site::getPreference('SMTP_ACTIVE')) { 105 case 'internal': 106 return Swift_MailTransport::newInstance(); 107 case 'sendmail': 108 return Swift_SendmailTransport::newInstance(); 109 case 'external': 110 $transport = Swift_SmtpTransport::newInstance() 111 ->setHost(Site::getPreference('SMTP_HOST')) 112 ->setPort(Site::getPreference('SMTP_PORT')) 113 ->setLocalDomain(Site::getPreference('SMTP_HELO')); 114 115 if (Site::getPreference('SMTP_AUTH')) { 116 $transport 117 ->setUsername(Site::getPreference('SMTP_AUTH_USER')) 118 ->setPassword(Site::getPreference('SMTP_AUTH_PASS')); 119 } 120 121 if (Site::getPreference('SMTP_SSL') !== 'none') { 122 $transport->setEncryption(Site::getPreference('SMTP_SSL')); 123 } 124 125 return $transport; 126 default: 127 // For testing 128 return Swift_NullTransport::newInstance(); 129 } 130 } 131} 132