php-censor/src/Helper/Email.php

145 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2014-05-08 22:38:32 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Helper;
2014-05-08 22:38:32 +02:00
2018-03-04 09:56:02 +01:00
use PHPCensor\Config;
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder;
2014-05-08 22:38:32 +02:00
/**
2017-10-21 10:51:05 +02:00
* Helper class for sending emails using email configuration.
*/
2014-05-08 22:38:32 +02:00
class Email
{
2016-07-19 13:05:02 +02:00
const DEFAULT_FROM = 'PHP Censor <no-reply@php-censor.local>';
2014-05-08 22:38:32 +02:00
2016-04-20 17:39:48 +02:00
protected $emailTo = [];
protected $emailCc = [];
2016-07-19 13:05:02 +02:00
protected $subject = 'Email from PHP Censor';
2016-04-20 17:39:48 +02:00
protected $body = '';
protected $isHtml = false;
2014-05-08 22:38:32 +02:00
protected $config;
/**
* Create a new email object.
*/
2014-05-08 22:38:32 +02:00
public function __construct()
{
$this->config = Config::getInstance();
}
/**
* Set the email's To: header.
* @param string $email
* @param string|null $name
* @return $this
*/
2014-05-08 22:43:06 +02:00
public function setEmailTo($email, $name = null)
2014-05-08 22:38:32 +02:00
{
2014-05-08 22:43:06 +02:00
$this->emailTo[$email] = $name;
2014-05-08 22:38:32 +02:00
return $this;
}
/**
* Add an address to the email's CC header.
* @param string $email
* @param string|null $name
* @return $this
*/
2014-05-08 22:38:32 +02:00
public function addCc($email, $name = null)
{
2014-05-08 22:43:06 +02:00
$this->emailCc[$email] = $name;
2014-05-08 22:38:32 +02:00
return $this;
}
/**
* Set the email subject.
* @param string $subject
* @return $this
*/
2014-05-08 22:38:32 +02:00
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Set the email body.
* @param string $body
* @return $this
*/
2014-05-08 22:38:32 +02:00
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Set whether or not the email body is HTML.
* @param bool $isHtml
* @return $this
*/
2015-02-12 15:11:58 +01:00
public function setHtml($isHtml = false)
2014-05-08 22:38:32 +02:00
{
$this->isHtml = $isHtml;
return $this;
}
/**
2016-08-17 16:20:07 +02:00
* Get the from address to use for the email.
* @return mixed|string
*/
2016-08-17 16:20:07 +02:00
protected function getFrom()
2014-05-08 22:38:32 +02:00
{
$from = $this->config->get(
'php-censor.email_settings.from_address',
self::DEFAULT_FROM
);
2018-03-04 09:56:02 +01:00
if (strpos($from, '<') === false) {
return (string)$from;
2014-05-08 22:38:32 +02:00
}
2018-03-04 09:56:02 +01:00
preg_match('#^(.*?)<(.*)>$#ui', $from, $fromParts);
2018-03-04 09:56:02 +01:00
return [$fromParts[2] => $fromParts[1]];
2014-05-08 22:38:32 +02:00
}
/**
2016-08-17 16:20:07 +02:00
* Send the email.
*
* @param Builder $builder
*
* @return integer
*/
2016-12-29 06:43:02 +01:00
public function send(Builder $builder = null)
2014-05-08 22:38:32 +02:00
{
2016-08-17 16:20:07 +02:00
$smtpServer = $this->config->get('php-censor.email_settings.smtp_address');
2016-12-29 06:43:02 +01:00
if (null !== $builder) {
$builder->logDebug(sprintf("SMTP: '%s'", !empty($smtpServer) ? 'true' : 'false'));
}
2016-08-17 16:20:07 +02:00
2016-07-21 19:02:11 +02:00
$factory = new MailerFactory($this->config->get('php-censor'));
2014-05-08 22:38:32 +02:00
$mailer = $factory->getSwiftMailerFromConfig();
$message = \Swift_Message::newInstance($this->subject)
->setFrom($this->getFrom())
2014-05-08 22:43:06 +02:00
->setTo($this->emailTo)
2014-05-08 22:38:32 +02:00
->setBody($this->body);
if ($this->isHtml) {
$message->setContentType('text/html');
}
2014-05-08 22:43:06 +02:00
if (is_array($this->emailCc) && count($this->emailCc)) {
$message->setCc($this->emailCc);
2014-05-08 22:38:32 +02:00
}
return $mailer->send($message);
}
2014-05-08 22:43:06 +02:00
}