Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MailAdapter | |
0.00% |
0 / 45 |
|
0.00% |
0 / 4 |
240 | |
0.00% |
0 / 1 |
| getMailer | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| send | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| getUserMail | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getFrom | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 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 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2013 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | namespace oat\tao\model\messaging\transportStrategy; |
| 27 | |
| 28 | use Exception; |
| 29 | use oat\generis\model\GenerisRdf; |
| 30 | use oat\oatbox\service\ConfigurableService; |
| 31 | use oat\tao\model\messaging\Transport; |
| 32 | use oat\tao\model\messaging\transportStrategy\AbstractAdapter; |
| 33 | use oat\tao\model\messaging\Message; |
| 34 | use oat\oatbox\user\User; |
| 35 | |
| 36 | /** |
| 37 | * MailAdapter sends email messages using PHPMailer. |
| 38 | * |
| 39 | * @access public |
| 40 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 41 | * @package tao |
| 42 | */ |
| 43 | class MailAdapter extends ConfigurableService implements Transport |
| 44 | { |
| 45 | public const CONFIG_SMTP_CONFIG = 'SMTPConfig'; |
| 46 | |
| 47 | public const CONFIG_DEFAULT_SENDER = 'defaultSender'; |
| 48 | |
| 49 | /** |
| 50 | * Initialize PHPMailer |
| 51 | * |
| 52 | * @access public |
| 53 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 54 | * @return \PHPMailer |
| 55 | */ |
| 56 | protected function getMailer() |
| 57 | { |
| 58 | $mailer = new \PHPMailer(); |
| 59 | |
| 60 | $SMTPConfig = $this->getOption(self::CONFIG_SMTP_CONFIG); |
| 61 | |
| 62 | $mailer->IsSMTP(); |
| 63 | $mailer->SMTPKeepAlive = true; |
| 64 | $mailer->Debugoutput = 'error_log'; |
| 65 | |
| 66 | $mailer->Host = $SMTPConfig['SMTP_HOST']; |
| 67 | $mailer->Port = $SMTPConfig['SMTP_PORT']; |
| 68 | $mailer->Username = $SMTPConfig['SMTP_USER']; |
| 69 | $mailer->Password = $SMTPConfig['SMTP_PASS']; |
| 70 | |
| 71 | if (isset($SMTPConfig['DEBUG_MODE'])) { |
| 72 | $mailer->SMTPDebug = $SMTPConfig['DEBUG_MODE']; |
| 73 | } |
| 74 | if (isset($SMTPConfig['Mailer'])) { |
| 75 | $mailer->Mailer = $SMTPConfig['Mailer']; |
| 76 | } |
| 77 | if (isset($SMTPConfig['SMTP_AUTH'])) { |
| 78 | $mailer->SMTPAuth = $SMTPConfig['SMTP_AUTH']; |
| 79 | } |
| 80 | if (isset($SMTPConfig['SMTP_SECURE'])) { |
| 81 | $mailer->SMTPSecure = $SMTPConfig['SMTP_SECURE']; |
| 82 | } |
| 83 | |
| 84 | return $mailer; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sent email message |
| 89 | * |
| 90 | * @access public |
| 91 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 92 | * @param Message $message |
| 93 | * @return boolean whether message was sent |
| 94 | */ |
| 95 | public function send(Message $message) |
| 96 | { |
| 97 | $mailer = $this->getMailer(); |
| 98 | $mailer->SetFrom($this->getFrom($message)); |
| 99 | $mailer->AddReplyTo($this->getFrom($message)); |
| 100 | $mailer->Subject = $message->getTitle(); |
| 101 | $mailer->AltBody = strip_tags(preg_replace("/<br.*>/i", "\n", $message->getBody())); |
| 102 | $mailer->MsgHTML($message->getBody()); |
| 103 | $mailer->AddAddress($this->getUserMail($message->getTo())); |
| 104 | |
| 105 | $result = false; |
| 106 | try { |
| 107 | if ($mailer->Send()) { |
| 108 | $message->setStatus(\oat\tao\model\messaging\Message::STATUS_SENT); |
| 109 | $result = true; |
| 110 | } |
| 111 | if ($mailer->IsError()) { |
| 112 | $message->setStatus(\oat\tao\model\messaging\Message::STATUS_ERROR); |
| 113 | } |
| 114 | } catch (phpmailerException $pe) { |
| 115 | \common_Logger::e($pe->getMessage()); |
| 116 | } |
| 117 | $mailer->ClearReplyTos(); |
| 118 | $mailer->ClearAllRecipients(); |
| 119 | $mailer->SmtpClose(); |
| 120 | |
| 121 | return $result; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get user email address. |
| 126 | * @param User $user |
| 127 | * @return string |
| 128 | * @throws Exception if email address is not valid |
| 129 | */ |
| 130 | public function getUserMail(User $user) |
| 131 | { |
| 132 | $userMail = current($user->getPropertyValues(GenerisRdf::PROPERTY_USER_MAIL)); |
| 133 | |
| 134 | if (!$userMail || !filter_var($userMail, FILTER_VALIDATE_EMAIL)) { |
| 135 | throw new Exception('User email is not valid.'); |
| 136 | } |
| 137 | |
| 138 | return $userMail; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get a "From" address. If it was not specified for message then value will be retrieved from config. |
| 143 | * @param Message $message (optional) |
| 144 | * @return string |
| 145 | */ |
| 146 | public function getFrom(Message $message = null) |
| 147 | { |
| 148 | $from = $message === null ? null : $message->getFrom(); |
| 149 | if (!$from) { |
| 150 | $from = $this->getOption(self::CONFIG_DEFAULT_SENDER); |
| 151 | } |
| 152 | return $from; |
| 153 | } |
| 154 | } |