Send mail notifications.

This commit is contained in:
Andrés Montañez 2013-12-15 23:44:28 -02:00
parent 75a2463240
commit f646647036
3 changed files with 129 additions and 3 deletions

View file

@ -19,6 +19,7 @@ use Mage\Task\ErrorWithMessageException;
use Mage\Task\SkipException;
use Mage\Console;
use Mage\Config;
use Mage\Mailer;
use Exception;
@ -177,7 +178,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
// Send Notifications
$this->sendNotification();
$this->sendNotification(self::$failedTasks > 0 ? false : true);
// Unlock
if (file_exists('.mage/~working.lock')) {
@ -514,8 +515,9 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
/**
* Send Email Notification if enabled
* @param boolean $result
*/
protected function sendNotification()
protected function sendNotification($result)
{
$projectName = $this->getConfig()->general('name', false);
$projectEmail = $this->getConfig()->general('email', false);
@ -525,6 +527,13 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if (!$projectName || !$projectEmail || !$notificationsEnabled) {
return false;
}
$mailer = new Mailer;
$mailer->setAddress($projectEmail)
->setProject($projectName)
->setLogFile(Console::getLogFile())
->setEnvironment($this->getConfig()->getEnvironment())
->send($result);
}
}

View file

@ -26,6 +26,12 @@ class Console
*/
private static $log = null;
/**
* The current logfile
* @var string
*/
private static $logFile = null;
/**
* Enables or Disables Logging
* @var boolean
@ -181,7 +187,8 @@ class Console
{
if (self::$logEnabled) {
if (self::$log == null) {
self::$log = fopen('.mage/logs/log-' . date('Ymd-His') . '.log', 'w');
self::$logFile = realpath('.mage/logs') . '/log-' . date('Ymd-His') . '.log';
self::$log = fopen(self::$logFile, 'w');
}
$message = date('Y-m-d H:i:s -- ') . $message;
@ -189,6 +196,24 @@ class Console
}
}
/**
* Return the screen buffer
* @return string
*/
public static function getOutput()
{
return self::$screenBuffer;
}
/**
* Returns the Log File
* @return string
*/
public static function getLogFile()
{
return self::$logFile;
}
/**
* Check Logs
* @param \Mage\Config $config

92
Mage/Mailer.php Normal file
View file

@ -0,0 +1,92 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage;
use Mage\Console;
/**
* Mailer Helper.
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class Mailer
{
const EOL = "\r\n";
const SUBJECT = '[Magallanes] Deployment of {project} to {environment}: {result}';
protected $address;
protected $project;
protected $environment;
protected $logFile;
public function setAddress($address)
{
$this->address = $address;
return $this;
}
public function setProject($project)
{
$this->project = $project;
return $this;
}
public function setEnvironment($environment)
{
$this->environment = $environment;
return $this;
}
public function setLogFile($logFile)
{
$this->logFile = $logFile;
return $this;
}
public function send($result)
{
$boundary = md5(date('r', time()));
$headers = 'From: ' . $this->address
. self::EOL
. 'Reply-To: ' . $this->address
. self::EOL
. 'MIME-Version: 1.0'
. self::EOL
. 'Content-Type: multipart/mixed; boundary=Mage-mixed-' . $boundary;
$subject = str_replace(
array('{project}', '{environment}', '{result}'),
array($this->project, $this->environment, $result ? 'SUCCESS' : 'FAILURE'),
self::SUBJECT
)
;
$attachment = chunk_split(base64_encode(file_get_contents($this->logFile)));
$message = 'This is a multi-part message in MIME format.' . self::EOL
. '--Mage-mixed-' . $boundary . self::EOL
. 'Content-Type: text/plain; charset=iso-8859-1' . self::EOL
. 'Content-Transfer-Encoding: quoted-printable' . self::EOL
. self::EOL
. strip_tags(Console::getOutput()) . self::EOL
. self::EOL
. '--Mage-mixed-' . $boundary . self::EOL
. 'Content-Type: text/plain; name="log.txt"' . self::EOL
. 'Content-Transfer-Encoding: base64' . self::EOL
. 'Content-Disposition: attachment' . self::EOL
. self::EOL
. $attachment . self::EOL
. '--Mage-mixed-' . $boundary . '--' . self::EOL
;
$mail_sent = @mail($this->address, $subject, $message, $headers);
}
}