Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifycation in case of errors in sending the emails in the queue #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/code/local/Aschroder/SMTPPro/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Aschroder_SMTPPro_Helper_Data extends Mage_Core_Helper_Abstract

const LOG_FILE = 'aschroder_smtppro.log';

const SEND_EMAIL_NOTIFICATION = 'smtppro/debug/sendemail_notification';
const SEND_EMAIL_NOTIFICATION_ADDRESSES = 'smtppro/debug/sendemail_notification_addresses';
const SEND_EMAIL_NOTIFICATION_TEMPLATE = 'smtppro/debug/sendemail_notification_template';

public function getTransport($storeId = null)
{

Expand Down Expand Up @@ -184,6 +188,21 @@ public function getQueuePause($storeId = null)
return Mage::getStoreConfig('smtppro/queue/pause', $storeId);
}

public function getSendEmailNotification($storeId = null)
{
return Mage::getStoreConfig(self::SEND_EMAIL_NOTIFICATION, $storeId);
}

public function getSendEmailNotificationAddresses($storeId = null)
{
return Mage::getStoreConfig(self::SEND_EMAIL_NOTIFICATION_ADDRESSES, $storeId);
}

public function getSendEmailNotificationTemplate($storeId = null)
{
return Mage::getStoreConfig(self::SEND_EMAIL_NOTIFICATION_TEMPLATE, $storeId);
}


// These are not the droids you're looking for...

Expand Down
75 changes: 75 additions & 0 deletions app/code/local/Aschroder/SMTPPro/Model/Email/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function send()
/** @var $collection Mage_Core_Model_Resource_Email_Queue_Collection */
$collection = Mage::getModel('core/email_queue')->getCollection()
->addOnlyForSendingFilter()
->addFieldToFilter('failed', array('eq' => '0')) //Add filter in collection. Extract only message with processetAt is null and failed is 0
->setPageSize($percron)
->setCurPage(1)
->load();
Expand Down Expand Up @@ -126,6 +127,9 @@ public function send()
Mage::logException($e);
Mage::setIsDeveloperMode($oldDevMode);

$this->notifyAdmin($message, $e); //call function that notify admin user
$_helper->log('Error send email with message ID: ' . $message->getMessageId() . ' Message type: '. $message->getEntityType()); //report error in log module

continue;
}

Expand All @@ -141,4 +145,75 @@ public function send()
return $this;
}

/**
* Function send notification to specific addresses in case of error in queue email
* @param $message
* @param $error
* @return bool
*/
private function notifyAdmin($message, $error){

$_helper = Mage::helper('smtppro');

//Check to see if the notification is enabled
if(!$_helper->getSendEmailNotification()) {
return false;
}

//Check to see if the email address for notification not empty
if(!$_helper->getSendEmailNotificationAddresses()) {
return false;
}
$sendEmailNotificationAddress = array_map('trim', explode(',', $_helper->getSendEmailNotificationAddresses() ));

$emailRecipient = '';

//Exstract all recipients of email with error
foreach ($message->getRecipients() as $recipient) {
list($email, $name, $type) = $recipient;
$emailRecipient .= $name . ' (' . $email . '),';
}


//Get template email ID
$emailTemplateId = $_helper->getSendEmailNotificationTemplate();
if(!is_numeric($emailTemplateId)){
$emailTemplate = Mage::getModel('core/email_template')
->loadByCode('SMTP PRO - Notification');
if(!$emailTemplate->getTemplateId()){
return false;
}

$emailTemplateId = $emailTemplate->getTemplateId();

}
$parameters = new Varien_Object($message->getMessageParameters());

try{
$translate = Mage::getSingleton('core/translate');
//Send notify with information of email has generated error
Mage::getModel('core/email_template')
->setDesignConfig(array('area'=>'frontend'))
->sendTransactional(
$emailTemplateId,
'support',
$sendEmailNotificationAddress,
array(),
array(
'email_recipient' => $emailRecipient,
'email_type' => $message->getEntityType(),
'email_subject' => $parameters->getSubject(),
'message_error' => $error->getMessage()
));
$translate->setTranslateInline(true);
$message->setFailed((int)true); //set message in queue is failed
$messageSaved = $message->save();
return true;
}catch(Exception $e) {
$_helper->log('Error in email notification admin for message_id ' . $message->getMessageId());
return false;
}

}

}
3 changes: 2 additions & 1 deletion app/code/local/Aschroder/SMTPPro/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<config>
<modules>
<Aschroder_SMTPPro>
<version>2.0.7</version>
<version>2.0.8</version>
</Aschroder_SMTPPro>
</modules>
<frontend>
Expand Down Expand Up @@ -211,6 +211,7 @@
<cleanlog>1</cleanlog>
<cleanlog_after_days>90</cleanlog_after_days>
<log_debug>0</log_debug>
<sendemail_notification>0</sendemail_notification>
</debug>
</smtppro>
</default>
Expand Down
34 changes: 34 additions & 0 deletions app/code/local/Aschroder/SMTPPro/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,40 @@
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</test>
<sendemail_notification translate="label comment">
<label>Enable notify in case of error</label>
<comment>Send notify to specific address email in case of error in any queue email</comment>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>90</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</sendemail_notification>
<sendemail_notification_template translate="label">
<label>Email Template for notification</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>95</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<depends>
<sendemail_notification>1</sendemail_notification>
</depends>
</sendemail_notification_template>
<sendemail_notification_addresses translate="label comment">
<label>Addresses email for notification</label>
<comment>Insert addresses email (separated by comma) to send notifications</comment>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<depends>
<sendemail_notification>1</sendemail_notification>
</depends>
</sendemail_notification_addresses>
</fields>
</debug>
<esp module="smtppro" translate="label">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

$installer = $this;

$installer->startSetup();

//create template for email notification
$code = 'SMTP PRO - Notification';
$subject = '{{var store.getFrontendName()}}: SMTP PRO Notification error in send email';
$text = '
Email notification Alert <br/>
<br/>
Attention: there is an email in queue with an error.
<br/><br/>
Info for email with error:<br/>
Recipient: {{var email_recipient}} <br/>
Type of email: {{var email_type}} <br/>
Subjet of email with error: {{var email_subject }}<br/>
<br/>
Error generated: {{var message_error}}
';

$template = Mage::getModel('adminhtml/email_template');

$template->setTemplateSubject($subject)
->setTemplateCode($code)
->setTemplateText($text)
->setModifiedAt(Mage::getSingleton('core/date')->gmtDate())
->setAddedAt(Mage::getSingleton('core/date')->gmtDate())
->setTemplateType(Mage_Core_Model_Email_Template::TYPE_HTML);

$template->save();

//Alter table core_email_queue adding column failed for identifier with email generate error
Mage::helper('smtppro/mysql4_install')->attemptQuery($installer, "
ALTER TABLE `{$this->getTable('core_email_queue')}`
ADD COLUMN `failed` SMALLINT(6) NOT NULL DEFAULT '0' AFTER `processed_at`;");


$installer->endSetup();
32 changes: 16 additions & 16 deletions modman
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# modman module description
# see https://github.com/colinmollenhour/modman/ for more info
# author: Ashley

#SMTP Pro
app/etc/modules/Aschroder_SMTPPro.xml app/etc/modules/Aschroder_SMTPPro.xml
app/code/local/Aschroder/SMTPPro/ app/code/local/Aschroder/SMTPPro/
app/design/adminhtml/base/default/template/smtppro/ app/design/adminhtml/default/default/template/smtppro/
app/locale/de_DE/Aschroder_SMTPPro.csv app/locale/de_DE/Aschroder_SMTPPro.csv
app/locale/en_US/Aschroder_SMTPPro.csv app/locale/en_US/Aschroder_SMTPPro.csv
app/locale/fr_FR/Aschroder_SMTPPro.csv app/locale/fr_FR/Aschroder_SMTPPro.csv
app/locale/es_ES/Aschroder_SMTPPro.csv app/locale/es_ES/Aschroder_SMTPPro.csv
app/locale/nl_NL/Aschroder_SMTPPro.csv app/locale/nl_NL/Aschroder_SMTPPro.csv
app/locale/pt_BR/Aschroder_SMTPPro.csv app/locale/pt_BR/Aschroder_SMTPPro.csv
app/locale/tr_TR/Aschroder_SMTPPro.csv app/locale/tr_TR/Aschroder_SMTPPro.csv
app/locale/ru_RU/Aschroder_SMTPPro.csv app/locale/ru_RU/Aschroder_SMTPPro.csv
# Modman file generated by 'generate-modman'
# app/code
app/code/local/Aschroder/SMTPPro app/code/local/Aschroder/SMTPPro
# app/etc
app/etc/modules/Aschroder_SMTPPro.xml app/etc/modules/Aschroder_SMTPPro.xml
# app/locale
app/locale/en_US/Aschroder_SMTPPro.csv app/locale/en_US/Aschroder_SMTPPro.csv
app/locale/tr_TR/Aschroder_SMTPPro.csv app/locale/tr_TR/Aschroder_SMTPPro.csv
app/locale/fr_FR/Aschroder_SMTPPro.csv app/locale/fr_FR/Aschroder_SMTPPro.csv
app/locale/nl_NL/Aschroder_SMTPPro.csv app/locale/nl_NL/Aschroder_SMTPPro.csv
app/locale/es_ES/Aschroder_SMTPPro.csv app/locale/es_ES/Aschroder_SMTPPro.csv
app/locale/ru_RU/Aschroder_SMTPPro.csv app/locale/ru_RU/Aschroder_SMTPPro.csv
app/locale/pt_BR/Aschroder_SMTPPro.csv app/locale/pt_BR/Aschroder_SMTPPro.csv
app/locale/de_DE/Aschroder_SMTPPro.csv app/locale/de_DE/Aschroder_SMTPPro.csv
# app/design
app/design/adminhtml/base/default/template/smtppro/view.phtml app/design/adminhtml/base/default/template/smtppro/view.phtml