Für ein B2E-Projekt brauchten wir eine Möglichkeit bei einem 1.9er Magento-Shop zu definieren, dass Mails an eine bestimmte Domain (z.B. @keinemails.com) nicht verschickt werden sollten.
Da wir die SMTP-Pro-Extension von ASchroder im Einsatz haben, lag es nahe diese anzupassen, um das gewünschte Ergebnis zu erhalten.
Im ersten Schritt nehmen wir die app/code/local/Aschroder/SMTPPro/Model/Email/Template.php
Hier fügen wir vor
/** * Send mail to recipient
und hinter
class Aschroder_SMTPPro_Model_Email_Template extends Mage_Core_Model_Email_Template {
folgenden Code ein
protected $_avoidedDomains = array('keinemails.com', 'auchkeinemails.com'); protected function isPreventForDomain($email, $domains) { foreach ($domains as $domain) { $pos = strpos($email, $domain, strlen($email) - strlen($domain)); if ($pos === false) continue; if ($pos == 0 || $email[(int) $pos - 1] == "@" || $email[(int) $pos - 1] == ".") return true; } return false; }
weiterhin hinter
public function send($email, $name = null, array $variables = array()) {
und vor
$_helper = Mage::helper('smtppro');
diesen Code
$emails = array_values((array)$email); $sentEmails = array();
abschließend nach
$_helper = Mage::helper('smtppro');
noch diesen Code
foreach($emails as $emailValue){ if($this->isPreventForDomain($emailValue, $this->_avoidedDomains)){ Mage::log(sprintf("We do not send email to %s", $emailValue), false, 'not_sent_email.log', true); continue; } $sentEmails[] = $emailValue; } if(count($sentEmails) == 0) return false; $emails = $sentEmails;
So, dass wir dann alles in allem folgendes haben
class Aschroder_SMTPPro_Model_Email_Template extends Mage_Core_Model_Email_Template { protected $_avoidedDomains = array('keinemails.com', 'auchkeinemails.com'); protected function isPreventForDomain($email, $domains) { foreach ($domains as $domain) { $pos = strpos($email, $domain, strlen($email) - strlen($domain)); if ($pos === false) continue; if ($pos == 0 || $email[(int) $pos - 1] == "@" || $email[(int) $pos - 1] == ".") return true; } return false; } /** * 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()) { $emails = array_values((array)$email); $sentEmails = array(); $_helper = Mage::helper('smtppro'); foreach($emails as $emailValue){ if($this->isPreventForDomain($emailValue, $this->_avoidedDomains)){ Mage::log(sprintf("We do not send email to %s", $emailValue), false, 'not_sent_email.log', true); continue; } $sentEmails[] = $emailValue; } if(count($sentEmails) == 0) return false; $emails = $sentEmails; // 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
In unserem Fall haben wir zwei Domains hinterlegt. Dies kann natürlich auch nur eine sein.
Wir erstellen hierdurch noch eine Log-Datei (/var/log/not_sent_email.log) – hier sieht man welche Mailadressen wann abgelehnt wurden.
Genutzt in Magento Version 1.9 Kommentare? Ergänzungen? Hinweise? Gerne!