Skip to content

Instantly share code, notes, and snippets.

@ghorvat
Created April 28, 2016 12:08
Show Gist options
  • Save ghorvat/e8d180695b86069a9a5aa796b2c13fdc to your computer and use it in GitHub Desktop.
Save ghorvat/e8d180695b86069a9a5aa796b2c13fdc to your computer and use it in GitHub Desktop.
<?php
/**
* This class wraps the Template email sending functionality
* If SMTP Pro is enabled it will send emails using the given
* configuration.
*
* @author Ashley Schroder (aschroder.com)
* @copyright Copyright (c) 2014 Ashley Schroder
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Aschroder_SMTPPro_Model_Email_Template extends Mage_Core_Model_Email_Template
{
/**
* Send mail to recipient
*
* @param array|string $email E-mail(s)
* @param array|string|null $name receiver name(s)
* @param array $variables template variables
* @return boolean
**/
public function send($email, $name = null, array $variables = array())
{
$_helper = Mage::helper('smtppro');
// If it's not enabled, just return the parent result.
if (!$_helper->isEnabled()) {
$_helper->log('SMTP Pro is not enabled, fall back to parent class');
return parent::send($email, $name, $variables);
}
// As per parent class - except addition of before and after send events
if (!$this->isValidForSend()) {
$_helper->log('Email is not valid for sending, this is a core error that often means there\'s a problem with your email templates.');
Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
return false;
}
$emails = array_values((array)$email);
$names = is_array($name) ? $name : (array)$name;
$names = array_values($names);
foreach ($emails as $key => $email) {
if (!isset($names[$key])) {
$names[$key] = substr($email, 0, strpos($email, '@'));
}
}
$variables['email'] = reset($emails);
$variables['name'] = reset($names);
$this->setUseAbsoluteLinks(true);
$text = $this->getProcessedTemplate($variables, true);
$subject = $this->getProcessedTemplateSubject($variables);
$setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
switch ($setReturnPath) {
case 1:
$returnPathEmail = $this->getSenderEmail();
break;
case 2:
$returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
break;
default:
$returnPathEmail = null;
break;
}
// Use the queue IFF it's not bypassed and it's been set.
// if (!$_helper->isQueueBypassed() &&
// $this->hasQueue() && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {
//
// /** @var $emailQueue Mage_Core_Model_Email_Queue */
// $emailQueue = $this->getQueue();
// $emailQueue->setMessageBody($text);
// $emailQueue->setMessageParameters(array(
// 'subject' => $subject,
// 'return_path_email' => $returnPathEmail,
// 'is_plain' => $this->isPlain(),
// 'from_email' => $this->getSenderEmail(),
// 'from_name' => $this->getSenderName(),
// 'reply_to' => $this->getMail()->getReplyTo(),
// 'return_to' => $this->getMail()->getReturnPath(),
// ))
// ->addRecipients($emails, $names, Mage_Core_Model_Email_Queue::EMAIL_TYPE_TO)
// ->addRecipients($this->_bccEmails, array(), Mage_Core_Model_Email_Queue::EMAIL_TYPE_BCC);
// $emailQueue->addMessageToQueue();
//
// $_helper->log('Email not sent immediately, queued for sending.');
//
// return true;
// }
ini_set('SMTP', Mage::getStoreConfig('system/smtp/host'));
ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port'));
$mail = $this->getMail();
if ($returnPathEmail !== null) {
$mailTransport = new Zend_Mail_Transport_Sendmail("-f".$returnPathEmail);
Zend_Mail::setDefaultTransport($mailTransport);
}
foreach ($emails as $key => $email) {
$mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
}
if($this->isPlain()) {
$mail->setBodyText($text);
} else {
$mail->setBodyHTML($text);
}
$mail->setSubject('=?utf-8?B?' . base64_encode($subject) . '?=');
$mail->setFrom($this->getSenderEmail(), $this->getSenderName());
/* START LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
$time = date('dmY_His');
/* END LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
try {
$mail->send();
/* START LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
if($this->isPlain()) {
Mage::log($text, null, 'inchoo_less_email_ok_text_'.$time.'.log', true);
} else {
Mage::log($text, null, 'inchoo_less_email_ok_text_'.$time.'.html', true);
}
/* END LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
$this->_mail = null;
}
catch (Exception $e) {
$this->_mail = null;
/* START LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
if($this->isPlain()) {
Mage::log($text, null, 'inchoo_less_email_exception_text_'.$time.'.log', true);
} else {
Mage::log($text, null, 'inchoo_less_email_exception_text_'.$time.'.html', true);
}
/* END LESS - (L)ocalhost (E)mail (S)erver (S)imulator */
Mage::logException($e);
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment