/app/code/community/Aschroder/SMTPPro/Model/Email/Template.php

https://github.com/seloshow/Magento-Pruebas · PHP · 130 lines · 74 code · 33 blank · 23 comment · 12 complexity · a234cf2f9fa7d4b9a0e83c52e46e3d0f MD5 · raw file

  1. <?php
  2. /**
  3. * This class wraps the Template email sending functionality
  4. * If SMTP Pro is enabled it will send emails using the given
  5. * configuration.
  6. *
  7. * @author Ashley Schroder (aschroder.com)
  8. */
  9. class Aschroder_SMTPPro_Model_Email_Template extends Mage_Core_Model_Email_Template {
  10. public function send($email, $name=null, array $variables = array()) {
  11. // If it's not enabled, just return the parent result.
  12. if (!Mage::helper('smtppro')->isEnabled()) {
  13. return parent::send($email, $name, $variables);
  14. }
  15. Mage::log('SMTPPro is enabled, sending email in Aschroder_SMTPPro_Model_Email_Template');
  16. // The remainder of this function closely mirrors the parent
  17. // method except for providing the SMTP auth details from the
  18. // configuration. This is not good OO, but the parent class
  19. // leaves little room for useful subclassing. This will probably
  20. // become redundant sooner or later anyway.
  21. if(!$this->isValidForSend()) {
  22. Mage::log('SMTPPro: Email not valid for sending - check template, and smtp enabled/disabled setting');
  23. Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
  24. return false;
  25. }
  26. $emails = array_values((array)$email);
  27. $names = is_array($name) ? $name : (array)$name;
  28. $names = array_values($names);
  29. foreach ($emails as $key => $email) {
  30. if (!isset($names[$key])) {
  31. $names[$key] = substr($email, 0, strpos($email, '@'));
  32. }
  33. }
  34. $variables['email'] = reset($emails);
  35. $variables['name'] = reset($names);
  36. $mail = $this->getMail();
  37. $dev = Mage::helper('smtppro')->getDevelopmentMode();
  38. if ($dev == "contact") {
  39. $email = Mage::getStoreConfig('contacts/email/recipient_email', $this->getDesignConfig()->getStore());
  40. Mage::log("Development mode set to send all emails to contact form recipient: " . $email);
  41. } elseif ($dev == "supress") {
  42. Mage::log("Development mode set to supress all emails.");
  43. # we bail out, but report success
  44. return true;
  45. }
  46. // In Magento core they set the Return-Path here, for the sendmail command.
  47. // we assume our outbound SMTP server (or Gmail) will set that.
  48. foreach ($emails as $key => $email) {
  49. $mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
  50. }
  51. $this->setUseAbsoluteLinks(true);
  52. $text = $this->getProcessedTemplate($variables, true);
  53. if($this->isPlain()) {
  54. $mail->setBodyText($text);
  55. } else {
  56. $mail->setBodyHTML($text);
  57. }
  58. $mail->setSubject('=?utf-8?B?'.base64_encode($this->getProcessedTemplateSubject($variables)).'?=');
  59. $mail->setFrom($this->getSenderEmail(), $this->getSenderName());
  60. // If we are using store emails as reply-to's set the header
  61. // Check the header is not already set by the application.
  62. // The contact form, for example, set's it to the sender of
  63. // the contact. Thanks i960 for pointing this out.
  64. if (Mage::helper('smtppro')->isReplyToStoreEmail()
  65. && !array_key_exists('Reply-To', $mail->getHeaders())) {
  66. // Patch for Zend upgrade
  67. // Later versions of Zend have a method for this, and disallow direct header setting...
  68. if (method_exists($mail, "setReplyTo")) {
  69. $mail->setReplyTo($this->getSenderEmail(), $this->getSenderName());
  70. } else {
  71. $mail->addHeader('Reply-To', $this->getSenderEmail());
  72. }
  73. Mage::log('ReplyToStoreEmail is enabled, just set Reply-To header: ' . $this->getSenderEmail());
  74. }
  75. $transport = Mage::helper('smtppro')->getTransport($this->getDesignConfig()->getStore());
  76. try {
  77. Mage::log('About to send email');
  78. $mail->send($transport); // Zend_Mail warning..
  79. Mage::log('Finished sending email');
  80. // Record one email for each receipient
  81. foreach ($emails as $key => $email) {
  82. Mage::dispatchEvent('smtppro_email_after_send',
  83. array('to' => $email,
  84. 'template' => $this->getTemplateId(),
  85. 'subject' => $this->getProcessedTemplateSubject($variables),
  86. 'html' => !$this->isPlain(),
  87. 'email_body' => $text));
  88. }
  89. $this->_mail = null;
  90. }
  91. catch (Exception $e) {
  92. Mage::logException($e);
  93. return false;
  94. }
  95. return true;
  96. }
  97. }