magallanes/Mage/Mailer.php

90 lines
2.5 KiB
PHP
Raw Normal View History

2013-12-16 02:44:28 +01:00
<?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;
/**
* Mailer Helper.
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class Mailer
{
2014-08-06 19:01:39 +02:00
const EOL = "\r\n";
const SUBJECT = '[Magallanes] Deployment of {project} to {environment}: {result}';
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
protected $address;
protected $project;
protected $environment;
protected $logFile;
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
public function setAddress($address)
{
2013-12-16 02:44:28 +01:00
$this->address = $address;
return $this;
2014-08-06 19:01:39 +02:00
}
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
public function setProject($project)
{
$this->project = $project;
return $this;
}
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
public function setEnvironment($environment)
{
$this->environment = $environment;
return $this;
}
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
public function setLogFile($logFile)
{
$this->logFile = $logFile;
return $this;
}
2013-12-16 02:44:28 +01:00
public function send($result)
{
2014-08-06 19:01:39 +02:00
$boundary = md5(date('r', time()));
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
$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;
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
$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)));
2013-12-16 02:44:28 +01:00
2014-08-06 19:01:39 +02:00
$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;
2013-12-16 02:44:28 +01:00
2014-10-11 18:44:48 +02:00
mail($this->address, $subject, $message, $headers);
2013-12-16 02:44:28 +01:00
}
}