* @package tao */ class MailAdapter extends ConfigurableService implements Transport { const CONFIG_SMTP_CONFIG = 'SMTPConfig'; const CONFIG_DEFAULT_SENDER = 'defaultSender'; /** * Initialize PHPMailer * * @access public * @author Bertrand Chevrier, * @return \PHPMailer */ protected function getMailer() { $mailer = new \PHPMailer(); $SMTPConfig = $this->getOption(self::CONFIG_SMTP_CONFIG); $mailer->IsSMTP(); $mailer->SMTPKeepAlive = true; $mailer->Debugoutput = 'error_log'; $mailer->Host = $SMTPConfig['SMTP_HOST']; $mailer->Port = $SMTPConfig['SMTP_PORT']; $mailer->Username = $SMTPConfig['SMTP_USER']; $mailer->Password = $SMTPConfig['SMTP_PASS']; if (isset($SMTPConfig['DEBUG_MODE'])) { $mailer->SMTPDebug = $SMTPConfig['DEBUG_MODE']; } if (isset($SMTPConfig['Mailer'])) { $mailer->Mailer = $SMTPConfig['Mailer']; } if (isset($SMTPConfig['SMTP_AUTH'])) { $mailer->SMTPAuth = $SMTPConfig['SMTP_AUTH']; } if (isset($SMTPConfig['SMTP_SECURE'])) { $mailer->SMTPSecure = $SMTPConfig['SMTP_SECURE']; } return $mailer; } /** * Sent email message * * @access public * @author Bertrand Chevrier, * @param Message $message * @return boolean whether message was sent */ public function send(Message $message) { $mailer = $this->getMailer(); $mailer->SetFrom($this->getFrom($message)); $mailer->AddReplyTo($this->getFrom($message)); $mailer->Subject = $message->getTitle(); $mailer->AltBody = strip_tags(preg_replace("//i", "\n", $message->getBody())); $mailer->MsgHTML($message->getBody()); $mailer->AddAddress($this->getUserMail($message->getTo())); $result = false; try { if ($mailer->Send()) { $message->setStatus(\oat\tao\model\messaging\Message::STATUS_SENT); $result = true; } if ($mailer->IsError()) { $message->setStatus(\oat\tao\model\messaging\Message::STATUS_ERROR); } } catch (phpmailerException $pe) { \common_Logger::e($pe->getMessage()); } $mailer->ClearReplyTos(); $mailer->ClearAllRecipients(); $mailer->SmtpClose(); return $result; } /** * Get user email address. * @param User $user * @return string * @throws Exception if email address is not valid */ public function getUserMail(User $user) { $userMail = current($user->getPropertyValues(GenerisRdf::PROPERTY_USER_MAIL)); if (!$userMail || !filter_var($userMail, FILTER_VALIDATE_EMAIL)) { throw new Exception('User email is not valid.'); } return $userMail; } /** * Get a "From" address. If it was not specified for message then value will be retrieved from config. * @param Message $message (optional) * @return string */ public function getFrom(Message $message = null) { $from = $message === null ? null : $message->getFrom(); if (!$from) { $from = $this->getOption(self::CONFIG_DEFAULT_SENDER); } return $from; } }