diff --git a/.gitignore b/.gitignore index 30dbedef..2e3f55a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Mods for HESK-specific files api/vendor +api/Tests/integration_test_mfh_settings.php # HESK Files admin/admin_suggest_articles.php diff --git a/api/BusinessLogic/Emails/BasicEmailSender.php b/api/BusinessLogic/Emails/BasicEmailSender.php new file mode 100644 index 00000000..a93aad9f --- /dev/null +++ b/api/BusinessLogic/Emails/BasicEmailSender.php @@ -0,0 +1,67 @@ +isSMTP(); + $mailer->SMTPAuth = true; + if ($heskSettings['smtp_ssl']) { + $mailer->SMTPSecure = "ssl"; + } elseif ($heskSettings['smtp_tls']) { + $mailer->SMTPSecure = "tls"; + } + $mailer->Host = $heskSettings['smtp_host_name']; + $mailer->Port = $heskSettings['smtp_host_port']; + $mailer->Username = $heskSettings['smtp_user']; + $mailer->Password = $heskSettings['smtp_password']; + } + + $mailer->FromName = $heskSettings['noreply_name'] ? $heskSettings['noreply_name'] : ''; + $mailer->From = $heskSettings['noreply_mail']; + + if ($emailBuilder->to !== null) { + foreach ($emailBuilder->to as $to) { + $mailer->addAddress($to); + } + } + + if ($emailBuilder->cc !== null) { + foreach ($emailBuilder->cc as $cc) { + $mailer->addCC($cc); + } + } + + if ($emailBuilder->bcc !== null) { + foreach ($emailBuilder->bcc as $bcc) { + $mailer->addBCC($bcc); + } + } + + $mailer->Subject = $emailBuilder->subject; + + if ($sendAsHtml) { + $mailer->Body = $emailBuilder->htmlMessage; + $mailer->AltBody = $emailBuilder->message; + } else { + $mailer->Body = $emailBuilder->message; + $mailer->isHTML(false); + } + $mailer->Timeout = $heskSettings['smtp_timeout']; + + if ($mailer->send()) { + return true; + } + + return $mailer->ErrorInfo; + } +} \ No newline at end of file diff --git a/api/BusinessLogic/Emails/EmailBuilder.php b/api/BusinessLogic/Emails/EmailBuilder.php index 62739d00..c5691df3 100644 --- a/api/BusinessLogic/Emails/EmailBuilder.php +++ b/api/BusinessLogic/Emails/EmailBuilder.php @@ -3,12 +3,41 @@ namespace BusinessLogic\Emails; +use BusinessLogic\Tickets\Attachment; + class EmailBuilder { - private $to; - private $cc; - private $bcc; - private $subject; - private $message; - private $htmlMessage; - private $attachments; + /** + * @var $to string[] + */ + public $to; + + /** + * @var $cc string[] + */ + public $cc; + + /** + * @var $bcc string[] + */ + public $bcc; + + /** + * @var $subject string + */ + public $subject; + + /** + * @var $message string + */ + public $message; + + /** + * @var $htmlMessage string + */ + public $htmlMessage; + + /** + * @var $attachments Attachment[] + */ + public $attachments; } \ No newline at end of file diff --git a/api/BusinessLogic/Emails/EmailSender.php b/api/BusinessLogic/Emails/EmailSender.php index 75f25e68..2cd191bc 100644 --- a/api/BusinessLogic/Emails/EmailSender.php +++ b/api/BusinessLogic/Emails/EmailSender.php @@ -5,21 +5,17 @@ namespace BusinessLogic\Emails; use BusinessLogic\Tickets\Attachment; use BusinessLogic\Tickets\Ticket; +use PHPMailer; interface EmailSender { /** + * Use to send emails that do NOT include ticket information + * * @param $emailBuilder EmailBuilder * @param $heskSettings array * @param $modsForHeskSettings array + * @param $sendAsHtml bool + * @return bool|string true if message sent successfully, error string otherwise */ - function sendEmail($emailBuilder, $heskSettings, $modsForHeskSettings); - - /** - * @param $emailBuilder EmailBuilder - * @param $ticket Ticket - * @param $attachments Attachment[] - * @param $heskSettings array - * @param $modsForHeskSettings array - */ - function sendEmailWithTicket($emailBuilder, $ticket, $attachments, $heskSettings, $modsForHeskSettings); + function sendEmail($emailBuilder, $heskSettings, $modsForHeskSettings, $sendAsHtml); } \ No newline at end of file diff --git a/api/BusinessLogic/Emails/PhpMailEmailSender.php b/api/BusinessLogic/Emails/PhpMailEmailSender.php deleted file mode 100644 index 56086090..00000000 --- a/api/BusinessLogic/Emails/PhpMailEmailSender.php +++ /dev/null @@ -1,29 +0,0 @@ -emailSender = new BasicEmailSender(); + $this->heskSettings = $hesk_settings; + $this->modsForHeskSettings = $modsForHesk_settings; + } + + function testItCanSendHtmlMail() { + //-- Arrange + //$hesk_settings['smtp'] = 0 //Uncomment this to use PHPMail + $emailBuilder = new EmailBuilder(); + $emailBuilder->to = array('mfh1@mailinator.com'); + $emailBuilder->cc = array('mfh2@mailinator.com'); + $emailBuilder->bcc = array('mfh3@mailinator.com'); + $emailBuilder->message = "Test PLAIN TEXT message"; + $emailBuilder->htmlMessage = "Test HTML message"; + $emailBuilder->subject = "BasicEmailSenderIntegrationTest"; + + + //-- Act + $result = $this->emailSender->sendEmail($emailBuilder, $this->heskSettings, $this->modsForHeskSettings, true); + + //-- Assert + if ($result !== true) { + $this->fail($result); + } + } + + function testItCanSendPlaintextMail() { + //-- Arrange + //$hesk_settings['smtp'] = 0 //Uncomment this to use PHPMail + $emailBuilder = new EmailBuilder(); + $emailBuilder->to = array('mfh1@mailinator.com'); + $emailBuilder->cc = array('mfh2@mailinator.com'); + $emailBuilder->bcc = array('mfh3@mailinator.com'); + $emailBuilder->message = "Test PLAIN TEXT message"; + $emailBuilder->subject = "BasicEmailSenderIntegrationTest"; + + + //-- Act + $result = $this->emailSender->sendEmail($emailBuilder, $this->heskSettings, $this->modsForHeskSettings, false); + + //-- Assert + if ($result !== true) { + $this->fail($result); + } + } +} diff --git a/api/Tests/integration_test_mfh_settings.sample.php b/api/Tests/integration_test_mfh_settings.sample.php new file mode 100644 index 00000000..592cbc13 --- /dev/null +++ b/api/Tests/integration_test_mfh_settings.sample.php @@ -0,0 +1,3 @@ +