2013-05-31 13:56:35 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Email Plugin - Provides simple email capability to PHPCI.
|
|
|
|
* @author Steve Brazier <meadsteve@gmail.com>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
|
|
|
class Email implements \PHPCI\Plugin
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Builder
|
|
|
|
*/
|
|
|
|
protected $phpci;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $options;
|
|
|
|
|
2013-06-01 10:40:05 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $emailConfig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Swift_Mailer
|
|
|
|
*/
|
|
|
|
protected $mailer;
|
|
|
|
|
|
|
|
public function __construct(\PHPCI\Builder $phpci,
|
|
|
|
array $options = array(),
|
|
|
|
\Swift_Mailer $mailer = null)
|
2013-05-31 13:56:35 +02:00
|
|
|
{
|
2013-06-01 14:29:59 +02:00
|
|
|
$phpCiSettings = $phpci->getSystemConfig('phpci');
|
2013-05-31 13:56:35 +02:00
|
|
|
$this->phpci = $phpci;
|
|
|
|
$this->options = $options;
|
2013-06-01 14:29:59 +02:00
|
|
|
$this->emailConfig = isset($phpCiSettings['email_settings']) ? $phpCiSettings['email_settings'] : array();
|
2013-06-01 10:40:05 +02:00
|
|
|
|
|
|
|
// Either a mailer will have been passed in or we load from the
|
|
|
|
// config.
|
|
|
|
if ($mailer === null) {
|
|
|
|
$this->loadSwiftMailerFromConfig();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
}
|
2013-05-31 13:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to MySQL and runs a specified set of queries.
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-01 10:40:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array|string $toAddresses Array or single address to send to
|
|
|
|
* @param string $subject Email subject
|
|
|
|
* @param string $body Email body
|
|
|
|
* @return array Array of failed addresses
|
|
|
|
*/
|
|
|
|
public function sendEmail($toAddresses, $subject, $body)
|
|
|
|
{
|
|
|
|
$message = \Swift_Message::newInstance($subject)
|
|
|
|
->setFrom($this->getMailConfig('from_address'))
|
|
|
|
->setTo($toAddresses)
|
|
|
|
->setBody($body);
|
|
|
|
$failedAddresses = array();
|
|
|
|
$this->mailer->send($message, $failedAddresses);
|
|
|
|
|
|
|
|
return $failedAddresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadSwiftMailerFromConfig()
|
|
|
|
{
|
|
|
|
/** @var \Swift_SmtpTransport $transport */
|
|
|
|
$transport = \Swift_SmtpTransport::newInstance(
|
|
|
|
$this->getMailConfig('smtp_address'),
|
|
|
|
$this->getMailConfig('smtp_port')
|
|
|
|
);
|
|
|
|
$transport->setUsername($this->getMailConfig('smtp_username'));
|
|
|
|
$transport->setPassword($this->getMailConfig('smtp_password'));
|
|
|
|
|
|
|
|
$this->mailer = \Swift_Mailer::newInstance($transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMailConfig($configName)
|
|
|
|
{
|
2013-06-01 14:29:59 +02:00
|
|
|
if (isset($this->emailConfig[$configName])
|
|
|
|
&& $this->emailConfig[$configName] != "")
|
|
|
|
{
|
2013-06-01 10:40:05 +02:00
|
|
|
return $this->emailConfig[$configName];
|
|
|
|
}
|
|
|
|
// Check defaults
|
|
|
|
else {
|
|
|
|
switch($configName) {
|
2013-06-01 14:29:59 +02:00
|
|
|
case 'smtp_address':
|
|
|
|
return "localhost";
|
2013-06-01 10:40:05 +02:00
|
|
|
case 'smtp_port':
|
|
|
|
return '25';
|
|
|
|
case 'from_address':
|
|
|
|
return "notifications-ci@phptesting.org";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 13:56:35 +02:00
|
|
|
}
|