Magento - Mailgun插件不发送电子邮件

时间:2017-02-08 03:58:53

标签: php magento magento-1.9 mailgun

我正在使用Magento v1.9.1,并安装了MailGun插件。

插件网址: https://www.magentocommerce.com/magento-connect/mailgun-for-email-delivery.html

您可以在此处看到管理区域中的MailGun插件:

enter image description here

然后我使用以下代码发送测试电子邮件(test_email.php):

<?php
// Invoke the Magento environment
require_once( 'app/Mage.php' );
Mage::app();

//Getting the Store E-Mail Sender Name.
$senderName = Mage::getStoreConfig('trans_email/ident_general/name');

//Getting the Store General E-Mail.
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

$customerEmail = "<personal_email@hidden_on_purpose>";
$text = "Hello, this is a test.";

//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
 ->setToName($senderName)
 ->setToEmail($customerEmail)
 ->setBody($text)
 ->setSubject('Subject :')
 ->setFromEmail($senderEmail)
 ->setFromName($senderName)
 ->setType('html');
 try{
 //Confimation E-Mail Send
 $mail->send();
 }
 catch(Exception $error)
 {
 Mage::getSingleton('core/session')->addError($error->getMessage());
 return false;
 }
?>

然后电子邮件到达我,但它不是通过MailGun发送的。我知道,因为在收到的电子邮件的标题上是应用程序所在的服务器,而在MailGun日志中,电子邮件不会被记录。

我认为MailGun插件仅适用于后端管理区域,但在使用Magento发送电子邮件时不会调用它。

我还做了一个修改文件的测试:

  

./应用程序/代码/小区/ FreeLunchLabs / MailGun /型号/ Mailgun.php   通过添加以下行:

die("Send stopped on file: ./app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php");

像:

...
    public function send($message) {

        die("Send stopped on file: ./app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php");

        $domain = $message->getStore()->getConfig('mailgun/general/domain');
        $apiKey = $message->getStore()->getConfig('mailgun/general/key');
        $files = null;

        if(count($message->getAttachments())) {
            foreach($message->getAttachments() as $attachment) {
                $files[] = $attachment;
            }
        }

        $sendResponse = $this->mailgunRequest('messages', $domain, $apiKey, $message->getMessage(), Zend_Http_Client::POST, false, $files);

        if($message->getStore()->getConfig('mailgun/events/store')) {
            Mage::getModel('freelunchlabs_mailgun/email')->saveInitialSend($message, $sendResponse);
        }

        return $sendResponse;
    }

...

但是在运行文件test_email.php时,上面代码中stopped message内的die(...)不会出现。

有关如何解决/调试此问题的想法吗?

1 个答案:

答案 0 :(得分:0)

好的,经过一些尝试/错误实验,我找到了答案:MailGun扩展仅适用于transactional emails,如其说明中所述。您无法使用MailGun直接发送自定义电子邮件,就像我的初始代码一样:test_email.php。如果您想使用MailGun发送一些自定义电子邮件,您应该先创建一个模板,然后使用该模板,如下面的代码所示,我在其中创建了ID为1的模板

<?php
// Invoke the Magento environment
require_once( 'app/Mage.php' );
Mage::app();

$templateId = 1;
$receiveName  = "<Your Name Here>";
$receiveEmail = "<your email here>";

$storeId = Mage::app()->getStore()->getStoreId();

$emailTemplate = Mage::getModel("core/email_template")->load($templateId);

$vars = array(
        "name"      => $receiveName,
        "email"     => $receiveEmail,
        "phone"     => "+13052491037",
        "comment"   => "This is my comment: Hello World!"
);

$emailTemplate->getProcessedTemplate($vars);

/*
echo "<pre>\n";
print_r($emailTemplate);
echo "</pre>\n";
die();
//*/

$emailTemplate->setSenderName(Mage::getStoreConfig("trans_email/ident_general/name", $storeId));
$emailTemplate->setSenderEmail(Mage::getStoreConfig("trans_email/ident_general/email", $storeId));

$emailTemplate->send($receiveEmail, $receiveName, $vars);

?>

希望这有助于某人。