Filled out the Plugin\Email::sendEmail(). Pulls in the following settings from the phpci config under the heading email_settings:
smtp_address smtp_port smtp_username smtp_password and from_address
This commit is contained in:
parent
f76a2a75cc
commit
f0a5ba50ca
2 changed files with 202 additions and 3 deletions
|
|
@ -29,10 +29,32 @@ class Email implements \PHPCI\Plugin
|
|||
*/
|
||||
protected $options;
|
||||
|
||||
public function __construct(\PHPCI\Builder $phpci, array $options = array())
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $emailConfig;
|
||||
|
||||
/**
|
||||
* @var \Swift_Mailer
|
||||
*/
|
||||
protected $mailer;
|
||||
|
||||
public function __construct(\PHPCI\Builder $phpci,
|
||||
array $options = array(),
|
||||
\Swift_Mailer $mailer = null)
|
||||
{
|
||||
$this->phpci = $phpci;
|
||||
$this->options = $options;
|
||||
$this->emailConfig = $phpci->getConfig('email_settings');
|
||||
|
||||
// Either a mailer will have been passed in or we load from the
|
||||
// config.
|
||||
if ($mailer === null) {
|
||||
$this->loadSwiftMailerFromConfig();
|
||||
}
|
||||
else {
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -43,4 +65,53 @@ class Email implements \PHPCI\Plugin
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
if (isset($this->emailConfig[$configName])) {
|
||||
return $this->emailConfig[$configName];
|
||||
}
|
||||
// Check defaults
|
||||
else {
|
||||
switch($configName) {
|
||||
case 'smtp_port':
|
||||
return '25';
|
||||
case 'from_address':
|
||||
return "notifications-ci@phptesting.org";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue