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

View file

@ -34,11 +34,11 @@ class Email {
protected $framework; protected $framework;
/** /**
* PHPMailer Instance * Contains email configuration.
* *
* @var PHPMailer * @var array
*/ */
protected $mailer; protected $config;
/** /**
* Class Constructor * Class Constructor
@ -47,24 +47,8 @@ class Email {
* @param array $config Contains the email configuration to be used. * @param array $config Contains the email configuration to be used.
*/ */
public function __construct(\CI_Controller $framework, array $config) { public function __construct(\CI_Controller $framework, array $config) {
$this->framework = $framework; $this->framework = $framework;
$this->config = $config;
$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;
} }
/** /**
@ -140,14 +124,16 @@ class Email {
$html = file_get_contents(__DIR__ . '/../../application/views/emails/appointment_details.php'); $html = file_get_contents(__DIR__ . '/../../application/views/emails/appointment_details.php');
$html = $this->_replaceTemplateVariables($replaceArray, $html); $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;
if (!$this->mailer->Send()) { $mailer = $this->_createMailer();
$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__ . '): ' throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
. $this->mailer->ErrorInfo); . $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 * @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". * 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\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, public function sendDeleteAppointment(array $appointment, array $provider,
array $service, array $customer, array $company, EmailAddress $recipientEmail, 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. * 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\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. * @param array $company The company settings to be included in the email.
*/ */
public function sendPassword(NonEmptyAlphanumeric $password, EmailAddress $recipientEmail, array $company) { 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 = file_get_contents(__DIR__ . '/../../application/views/emails/new_password.php');
$html = $this->_replaceTemplateVariables($replaceArray, $html); $html = $this->_replaceTemplateVariables($replaceArray, $html);
$this->mailer->From = $company['company_email']; $mailer = $this->_createMailer();
$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;
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__ . '): ' throw new \RuntimeException('Email could not been sent. Mailer Error (Line ' . __LINE__ . '): '
. $this->mailer->ErrorInfo); . $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;
}
} }