CakePHP Paypal IPN插件电子邮件无法正常工作

时间:2016-03-03 02:21:33

标签: php cakephp php-5.5 cakephp-2.5

我在CakePHP应用程序中使用this插件。除了发送电子邮件外,一切似乎都有效。

我在AppController.php中有以下代码

function afterPaypalNotification($txnId)
{
    //Here is where you can implement code to apply the transaction to your app.
    //for example, you could now mark an order as paid, a subscription, or give the user premium access.
    //retrieve the transaction using the txnId passed and apply whatever logic your site needs.

    $transaction = ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->findById($txnId);
    $this->log($transaction['InstantPaymentNotification']['id'], 'paypal');

    //Tip: be sure to check the payment_status is complete because failure
    //     are also saved to your database for review.

    if ($transaction['InstantPaymentNotification']['payment_status'] == 'Completed') 
    {
        //Yay!  We have monies!
        ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
            'id' => $txnId,
            'subject' => 'Thanks!',
            'message' => 'Thank you for the transaction!'
        ));
    }
    else
    {
        //Oh no, better look at this transaction to determine what to do; like email a decline letter.
        ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
            'id' => $txnId,
            'subject' => 'Failed!',
            'message' => 'Please review your transaction'
        ));
    }
}

但是从Paypal返回的数据保存在instant_payment_notifications表中,但由于某种原因,不会发送电子邮件。有没有人之前尝试过这个插件并完成了电子邮件功能?

我是否需要在app / Config中启用email.php才能使电子邮件生效?我在Cake的网站上看到我不需要该文件来发送电子邮件,所以我想这不是问题所在。

任何帮助将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

在CakePhp 2.5中,您应该使用CakeEmail

使用EmailConfig类创建文件/app/Config/email.php。 /app/Config/email.php.default有一个这个文件的例子。

在类声明

之前在/app/Controller/AppController.php中添加以下行
App::uses('CakeEmail', 'Network/Email');

在afterPaypalNotification函数中替换

ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
    'id' => $txnId,
    'subject' => 'Thanks!',
    'message' => 'Thank you for the transaction!'
));

(快速电子邮件,没有风格)

CakeEmail::deliver('customer@example.com', 'Thanks!', 'Thank you for the transaction!', array('from' => 'you@example.com'));

$Email = new CakeEmail();
$Email->template('email_template', 'email_layout')
    ->emailFormat('html')
    ->to('customer@example.com')
    ->from('you@domain.com')
    ->viewVars(array('id' => $txnId))
    ->send();

电子邮件模板转到/app/View/Emails/html/email_template.ctp

电子邮件布局转到/app/View/Layouts/Emails/html/email_layout.ctp