From 3567742dcfb268c8f493d9de763a819ef6ca3083 Mon Sep 17 00:00:00 2001 From: Marco Vito Moscaritolo Date: Tue, 11 Nov 2014 22:58:23 +0100 Subject: [PATCH] Refactor on mail sending to use Email helper and specific tempalte for successfull build. --- PHPCI/Plugin/Email.php | 111 ++++++++++++++------------------- PHPCI/View/Email/success.phtml | 15 +++++ 2 files changed, 61 insertions(+), 65 deletions(-) create mode 100644 PHPCI/View/Email/success.phtml diff --git a/PHPCI/Plugin/Email.php b/PHPCI/Plugin/Email.php index 7c5171d6..85720d7f 100644 --- a/PHPCI/Plugin/Email.php +++ b/PHPCI/Plugin/Email.php @@ -13,6 +13,7 @@ use b8\View; use PHPCI\Builder; use PHPCI\Helper\Lang; use PHPCI\Model\Build; +use PHPCI\Helper\Email as EmailHelper; /** * Email Plugin - Provides simple email capability to PHPCI. @@ -27,21 +28,16 @@ class Email implements \PHPCI\Plugin */ protected $phpci; + /** + * @var \PHPCI\Model\Build + */ + protected $build; + /** * @var array */ protected $options; - /** - * @var \Swift_Mailer - */ - protected $mailer; - - /** - * @var string - */ - protected $fromAddress; - /** * Set up the plugin, configure options, etc. * @param Builder $phpci @@ -52,25 +48,16 @@ class Email implements \PHPCI\Plugin public function __construct( Builder $phpci, Build $build, - \Swift_Mailer $mailer, array $options = array() ) { $this->phpci = $phpci; $this->build = $build; $this->options = $options; - - $phpCiSettings = $phpci->getSystemConfig('phpci'); - - $this->fromAddress = isset($phpCiSettings['email_settings']['from_address']) - ? $phpCiSettings['email_settings']['from_address'] - : "notifications-ci@phptesting.org"; - - $this->mailer = $mailer; } /** - * Connects to MySQL and runs a specified set of queries. - */ + * Send a notificaiton mail. + */ public function execute() { $addresses = $this->getEmailAddresses(); @@ -81,79 +68,73 @@ class Email implements \PHPCI\Plugin return false; } - $subjectTemplate = "PHPCI - %s - %s"; + $buildStatus = $this->build->isSuccessful() ? "Failing Build" : "Passing Build"; $projectName = $this->phpci->getBuildProjectTitle(); - $logText = $this->build->getLog(); + $mailTemplate = $this->build->isSuccessful() ? 'Email/success' : 'Email/failed'; - if ($this->build->isSuccessful()) { - $sendFailures = $this->sendSeparateEmails( - $addresses, - sprintf($subjectTemplate, $projectName, Lang::get('passing_build')), - sprintf(Lang::get('log_output')."
%s
", $logText) - ); - } else { - $view = new View('Email/failed'); - $view->build = $this->build; - $view->project = $this->build->getProject(); + $view = new View($mailTemplate); + $view->build = $this->build; + $view->project = $this->build->getProject(); + $body = $view->render(); - $emailHtml = $view->render(); - - $sendFailures = $this->sendSeparateEmails( - $addresses, - sprintf($subjectTemplate, $projectName, Lang::get('failing_build')), - $emailHtml - ); - } + $sendFailures = $this->sendSeparateEmails( + $addresses, + sprintf("PHPCI - %s - %s", $projectName, $buildStatus), + $body + ); // This is a success if we've not failed to send anything. + $this->phpci->log(sprintf("%d emails sent", (count($addresses) - $sendFailures))); + $this->phpci->log(sprintf("%d emails failed to send", $sendFailures)); - $this->phpci->log(Lang::get('n_emails_sent', (count($addresses) - count($sendFailures)))); - $this->phpci->log(Lang::get('n_emails_failed', count($sendFailures))); - - return (count($sendFailures) == 0); + return ($sendFailures === 0); } /** - * @param string[]|string $toAddresses Array or single address to send to + * @param string $toAddress Single address to send to * @param string[] $ccList * @param string $subject Email subject * @param string $body Email body * @return array Array of failed addresses */ - public function sendEmail($toAddresses, $ccList, $subject, $body) + public function sendEmail($toAddress, $ccList, $subject, $body) { - $message = \Swift_Message::newInstance($subject) - ->setFrom($this->fromAddress) - ->setTo($toAddresses) - ->setBody($body) - ->setContentType("text/html"); + $email = new EmailHelper(); + + $email->setEmailTo($toAddress, $toAddress); + $email->setSubject($subject); + $email->setBody($body); + $email->setIsHtml(true); if (is_array($ccList) && count($ccList)) { - $message->setCc($ccList); + foreach ($ccList as $address) { + $message->addCc($address, $address); + } } - $failedAddresses = array(); - $this->mailer->send($message, $failedAddresses); - - return $failedAddresses; + return $email->send(); } /** - * Send out build status emails. + * Send an email to a list of specified subjects. + * * @param array $toAddresses - * @param $subject - * @param $body - * @return array + * List of destinatary of message. + * @param string $subject + * Mail subject + * @param string $body + * Mail body + * + * @return int number of failed messages */ public function sendSeparateEmails(array $toAddresses, $subject, $body) { - $failures = array(); + $failures = 0; $ccList = $this->getCcAddresses(); foreach ($toAddresses as $address) { - $newFailures = $this->sendEmail($address, $ccList, $subject, $body); - foreach ($newFailures as $failure) { - $failures[] = $failure; + if (!$this->sendEmail($address, $ccList, $subject, $body)) { + $failures++; } } return $failures; diff --git a/PHPCI/View/Email/success.phtml b/PHPCI/View/Email/success.phtml new file mode 100644 index 00000000..a6dfccea --- /dev/null +++ b/PHPCI/View/Email/success.phtml @@ -0,0 +1,15 @@ +
+
+
+ getTitle(); ?> - Build #getId(); ?> +
+ +
+

Your commit getCommitId(); ?> genrate a successfull build in project getTitle(); ?>.

+ +

getCommitMessage(); ?>

+
getLog(); ?>
+

You can review your commit and the build log.

+
+
+