Skip to content

Commit

Permalink
Added events around Zend_Email (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjocoleman authored Jan 14, 2023
1 parent 29dd4b1 commit 68c9351
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
23 changes: 21 additions & 2 deletions app/code/core/Mage/Core/Model/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public function send()
return $this;
}

$mail = new Zend_Mail();
$mail = new Zend_Mail('utf-8');
$transport = new Varien_Object();

if (strtolower($this->getType()) == 'html') {
$mail->setBodyHtml($this->getBody());
Expand All @@ -142,7 +143,25 @@ public function send()
$mail->setFrom($this->getFromEmail(), $this->getFromName())
->addTo($this->getToEmail(), $this->getToName())
->setSubject($this->getSubject());
$mail->send();

Mage::dispatchEvent('email_send_before', [
'mail' => $mail,
'template' => $this->getTemplate(),
'transport' => $transport,
'variables' => $this->getTemplateVars()
]);

if ($transport->getTransport()) {
$mail->send($transport->getTransport());
} else {
$mail->send();
}

Mage::dispatchEvent('email_send_after', [
'to' => $this->getToEmail(),
'subject' => $this->getSubject(),
'email_body' => $this->getBody()
]);

return $this;
}
Expand Down
24 changes: 23 additions & 1 deletion app/code/core/Mage/Core/Model/Email/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,32 @@ public function send()
}

try {
$mailer->send();
$transport = new Varien_Object();
Mage::dispatchEvent('email_queue_send_before', [
'mail' => $mailer,
'message' => $message,
'transport' => $transport
]);

if ($transport->getTransport()) {
$mailer->send($transport->getTransport());
} else {
$mailer->send();
}

unset($mailer);
$message->setProcessedAt(Varien_Date::formatDate(true));
$message->save(); // save() is throwing exception when recipient is not set

foreach ($message->getRecipients() as $recipient) {
list($email, $name, $type) = $recipient;
Mage::dispatchEvent('email_queue_send_after', [
'to' => $email,
'html' => !$parameters->getIsPlain(),
'subject' => $parameters->getSubject(),
'email_body' => $message->getMessageBody()
]);
}
} catch (Exception $e) {
Mage::logException($e);
}
Expand Down
25 changes: 24 additions & 1 deletion app/code/core/Mage/Core/Model/Email/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,30 @@ public function send($email, $name = null, array $variables = [])
$mail->setFrom($this->getSenderEmail(), $this->getSenderName());

try {
$mail->send();
$transport = new Varien_Object();

Mage::dispatchEvent('email_template_send_before', [
'mail' => $mail,
'template' => $this,
'transport' => $transport,
'variables' => $variables
]);

if ($transport->getTransport()) {
$mail->send($transport->getTransport());
} else {
$mail->send();
}

foreach ($emails as $key => $email) {
Mage::dispatchEvent('email_template_send_after', [
'to' => $email,
'html' => !$this->isPlain(),
'subject' => $subject,
'template' => $this->getTemplateId(),
'email_body' => $text
]);
}
$this->_mail = null;
} catch (Exception $e) {
$this->_mail = null;
Expand Down
23 changes: 22 additions & 1 deletion app/code/core/Mage/Newsletter/Model/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,28 @@ public function send($subscriber, array $variables = [], $name = null, Mage_News
$mail->setFrom($this->getTemplateSenderEmail(), $this->getTemplateSenderName());

try {
$mail->send();
$transport = new Varien_Object();

Mage::dispatchEvent('newsletter_send_before', [
'mail' => $mail,
'transport' => $transport,
'template' => $this,
'subscriber' => $subscriber
]);

if ($transport->getTransport()) {
$mail->send($transport->getTransport());
} else {
$mail->send();
}

Mage::dispatchEvent('newsletter_send_after', [
'to' => $email,
'html' => !$this->isPlain(),
'queue' => $queue,
'subject' => $mail->getSubject(),
'email_body' => $text
]);
$this->_mail = null;
if (!is_null($queue)) {
$subscriber->received($queue);
Expand Down
10 changes: 9 additions & 1 deletion docs/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
| customer_registration_is_allowed | 1.9.4.5 |
| customer_session_init | 1.9.4.5 |
| eav_collection_abstract_load_before | 1.9.4.5 |
| email_queue_send_before | 19.4.18 |
| email_queue_send_after | 19.4.18 |
| email_send_before | 19.4.18 |
| email_send_after | 19.4.18 |
| email_template_send_before | 19.4.18 |
| email_template_send_after | 19.4.18 |
| end_index_events_[getEventTypeName] | 1.9.4.5 |
| end_process_event_[getEventTypeName] | 1.9.4.5 |
| gift_options_prepare_items | 1.9.4.5 |
Expand All @@ -235,6 +241,8 @@
| model_save_after | 1.9.4.5 |
| model_save_before | 1.9.4.5 |
| model_save_commit_after | 1.9.4.5 |
| newsletter_send_before | 19.4.18 |
| newsletter_send_after | 19.4.18 |
| on_view_report | 1.9.4.5 |
| order_cancel_after | 1.9.4.5 |
| order_status_changed_before_save | 19.5.0 / 20.1.0 |
Expand Down Expand Up @@ -361,4 +369,4 @@ class YourNamespace_YourModule_Model_Observer {
public function yourMethod(Varien_Event_Observer $observer) {
$customer = $observer->getData('customer');
}
}
}

0 comments on commit 68c9351

Please sign in to comment.