There must always be one PHPMailer instance per email (fixes #192).

This commit is contained in:
alextselegidis 2016-10-23 22:32:12 +02:00
parent 863a61886b
commit 457e59b502
1 changed files with 49 additions and 36 deletions

View File

@ -34,11 +34,11 @@ class Email {
protected $framework;
/**
* PHPMailer Instance
* Contains email configuration.
*
* @var PHPMailer
* @var array
*/
protected $mailer;
protected $config;
/**
* Class Constructor
@ -48,23 +48,7 @@ class Email {
*/
public function __construct(\CI_Controller $framework, array $config) {
$this->framework = $framework;
$mailer = new \PHPMailer;
if ($config['protocol'] === 'smtp') {
$mailer->isSMTP();
$mailer->Host = $config['smtp_host'];
$mailer->SMTPAuth = true;
$mailer->Username = $config['smtp_user'];
$mailer->Password = $config['smtp_pass'];
$mailer->SMTPSecure = $config['smtp_crypto'];
$mailer->Port = $config['smtp_port'];
}
$mailer->IsHTML($config['mailtype'] === 'html');
$mailer->CharSet = $config['charset'];
$this->mailer = $mailer;
$this->config = $config;
}
/**
@ -141,13 +125,15 @@ class Email {
$html = file_get_contents(__DIR__ . '/../../application/views/emails/appointment_details.php');
$html = $this->_replaceTemplateVariables($replaceArray, $html);
$this->mailer->From = $company['company_email'];
$this->mailer->FromName = $company['company_name'];
$this->mailer->AddAddress($recipientEmail->get());
$this->mailer->Subject = $title->get();
$this->mailer->Body = $html;
$mailer = $this->_createMailer();
if (!$this->mailer->Send()) {
$mailer->From = $company['company_email'];
$mailer->FromName = $company['company_name'];
$mailer->AddAddress($recipientEmail->get());
$mailer->Subject = $title->get();
$mailer->Body = $html;
if (!$mailer->Send()) {
throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
. $this->mailer->ErrorInfo);
}
@ -169,7 +155,7 @@ class Email {
* @param array $company Some settings that are required for this function. By now this array must contain
* the following values: "company_link", "company_name", "company_email".
* @param \EA\Engine\Types\Email $recipientEmail The email address of the email recipient.
* @param \EA\Engine\Tyeps\String $reason The reason why the appointment is deleted.
* @param \EA\Engine\Types\String $reason The reason why the appointment is deleted.
*/
public function sendDeleteAppointment(array $appointment, array $provider,
array $service, array $customer, array $company, EmailAddress $recipientEmail,
@ -224,7 +210,7 @@ class Email {
* This method sends an email with the new password of a user.
*
* @param \EA\Engine\Types\NonEmptyAlphanumeric $password Contains the new password.
* @param \EA\Engine\Types\Email $email The receiver's email address.
* @param \EA\Engine\Types\Email $recipientEmail The receiver's email address.
* @param array $company The company settings to be included in the email.
*/
public function sendPassword(NonEmptyAlphanumeric $password, EmailAddress $recipientEmail, array $company) {
@ -240,15 +226,42 @@ class Email {
$html = file_get_contents(__DIR__ . '/../../application/views/emails/new_password.php');
$html = $this->_replaceTemplateVariables($replaceArray, $html);
$this->mailer->From = $company['company_email'];
$this->mailer->FromName = $company['company_name'];
$this->mailer->AddAddress($recipientEmail->get()); // "Name" argument crushes the phpmailer class.
$this->mailer->Subject = $this->framework->lang->line('new_account_password');
$this->mailer->Body = $html;
$mailer = $this->_createMailer();
if (!$this->mailer->Send()) {
$mailer->From = $company['company_email'];
$mailer->FromName = $company['company_name'];
$mailer->AddAddress($recipientEmail->get()); // "Name" argument crushes the phpmailer class.
$mailer->Subject = $this->framework->lang->line('new_account_password');
$mailer->Body = $html;
if (!$mailer->Send()) {
throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
. $this->mailer->ErrorInfo);
}
}
/**
* Create PHP Mailer Instance
*
* @return \PHPMailer
*/
protected function _createMailer()
{
$mailer = new \PHPMailer;
if ($this->config['protocol'] === 'smtp') {
$mailer->isSMTP();
$mailer->Host = $this->config['smtp_host'];
$mailer->SMTPAuth = true;
$mailer->Username = $this->config['smtp_user'];
$mailer->Password = $this->config['smtp_pass'];
$mailer->SMTPSecure = $this->config['smtp_crypto'];
$mailer->Port = $this->config['smtp_port'];
}
$mailer->IsHTML($this->config['mailtype'] === 'html');
$mailer->CharSet = $this->config['charset'];
return $mailer;
}
}