Paypal IPN 和 mail() 函数
更新:(2/29/12) 好的,对于完全不同的服务器和托管公司上的不同客户端,我再次遇到了同样的问题。
同样,只有 mail() 的脚本可以正确发送电子邮件,没有任何问题。然后我添加了与下面类似的代码,并将其与 paypal IPN 连接起来。每次收到新付款时,IPN 都会触发,数据会保存到数据库中,但 mail() 函数不起作用。
然而,我遇到了一个有趣的问题。我使用相同的脚本从 paypal 的沙箱中进行了 IPN 测试,并且电子邮件已发送出去。
这是 paypals 生产 IPN 的问题吗?也许是它将数据发布到脚本的方式?
这里的任何信息都会非常有帮助,因为我当前使用 cronjobs 的解决方案很草率。
END UPDATE
我的 paypal IPN 侦听器已正确配置,因为当新付款进入时它会将所有信息写入数据库。现在我正在尝试设置一个 mail() 函数来向我发送一封电子邮件新付款提醒。
我以前曾在另一个项目中这样做过,但我一生都无法弄清楚为什么这次不起作用。我在 error_log 中没有收到任何错误,并且脚本的其余部分运行良好。
我已经测试过以确保服务器确实使用独立的 mail() 脚本发送邮件。我在这里真的很迷失和困惑。
这是我的代码:
mail('[email protected]', 'New Order', 'New Order', 'From: [email protected]');
define("_VALID_PHP", true);
require_once('../php/init.php');
$item_number = $_POST['item_number'];
$payment_gross = $_POST['payment_gross'];
$payment_status = $_POST['payment_status'];
$payer_email = $_POST['payer_email'];
$txn_id = $_POST['txn_id'];
if ($payment_status == 'Completed') {
$query = $db->query("SELECT price, id, uid FROM invoice WHERE md5='$item_number'");
$row = $db->fetch($query);
$iid = $row['id'];
$uid = $row['uid'];
if ($row['price'] == $payment_gross){
$invoiceUpdate['paid'] = 1;
$update = $db->update('invoice', $invoiceUpdate, "md5='$item_number'");
}
}
$data['iid'] = $iid;
$data['uid'] = $uid;
$data['payment_status'] = $payment_status;
$data['payer_email'] = $payer_email;
$data['payment_gross'] = $payment_gross;
$data['txn_id'] = $txn_id;
$db->insert('payment', $data);
UPDATE: (2/29/12) Okay, so I've run into this same issue again for a different client on a completely different server and hosting company.
Again, having a script with just mail() sends out the email correctly with no issues. I then added code that is similar to what I have below and hooked it up with paypal IPN. Every time a new payment comes in, the IPN fires, the data gets saved to the db but the mail() function just doesn't work.
However, I ran into an interesting issue. I did a test IPN fire from paypal's sandbox with the same script and the email was sent out.
Is this an issue with paypals production IPN, perhaps the way that it posts data to the script?
Any information here would be extremely helpful since my current solution using cronjobs is sloppy.
END UPDATE
I have my paypal IPN listener configured properly since it writes all the information to the DB when a new payment comes in. Now I'm trying to setup a mail() function that sends me an email alert of a new payment.
I have done this before for another project but I can't for the life of my figure out why it's not working this time. I'm not getting any error's in the error_log and the rest of the script runs fine.
I've tested to make sure that the server actually does send mail with a standalone mail() script. I'm really lost and confused here.
Here's the code that I have:
mail('[email protected]', 'New Order', 'New Order', 'From: [email protected]');
define("_VALID_PHP", true);
require_once('../php/init.php');
$item_number = $_POST['item_number'];
$payment_gross = $_POST['payment_gross'];
$payment_status = $_POST['payment_status'];
$payer_email = $_POST['payer_email'];
$txn_id = $_POST['txn_id'];
if ($payment_status == 'Completed') {
$query = $db->query("SELECT price, id, uid FROM invoice WHERE md5='$item_number'");
$row = $db->fetch($query);
$iid = $row['id'];
$uid = $row['uid'];
if ($row['price'] == $payment_gross){
$invoiceUpdate['paid'] = 1;
$update = $db->update('invoice', $invoiceUpdate, "md5='$item_number'");
}
}
$data['iid'] = $iid;
$data['uid'] = $uid;
$data['payment_status'] = $payment_status;
$data['payer_email'] = $payer_email;
$data['payment_gross'] = $payment_gross;
$data['txn_id'] = $txn_id;
$db->insert('payment', $data);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于您的邮件函数返回 true 并且您的代码看起来正确,我认为您应该检查邮件日志,因为问题可能与代码无关。尝试发送邮件,然后检查服务器上的邮件日志...有一次我花了两天时间试图找出类似的问题,最终问题是我的邮件不被其他服务器接受。
要查找您的邮件日志,您可以执行以下操作(从 shell):
或者
假设您使用的是 Linux,但问题也可能存在于 Windows 上
Since your mail function returns true and your code looks correct, i think you should check the mail log because the problem might not be related to code. Try to send a mail and then check the mail log on the server...once i lost two days trying to figure out a similar problem and in the end the problem was that my mail was not accepted by other servers.
to finde your mail log you can do (from the shell):
or
this assumes you are using linux, but the problem might as well exists also on windows
该代码对我来说似乎是正确的。
我的建议:
如果以上所有方法均失败,则问题肯定出在您的托管提供商上。
The code seems correct to me.
My advice:
If all of the above fail, the problem is definitely with your hosting provider.
从调用 mail() 开始,然后逐渐添加处理 $_POST 的代码以查看它何时崩溃怎么样?您应该使用 PayPal 进行沙箱测试,以使这变得更容易。
另外,为了安全起见,您应该向 Paypal 服务器发送一条验证消息,以检查请求是否确实来自 Paypal。
how about start off with a call to mail(), then gradually add the code that process $_POST to see when it breaks down? You should have sandbox testing with paypal to make this easier.
On a side note, you should send a verify message to Paypal server to check if the request is actually originated from Paypal, just for security.
问题不在于您的 PHP 代码,而在于服务器端。您的邮件可能已满,或者您的提供商/您的服务器的 SMTP 服务器存在问题。检查配置/联系提供商。
Problem isn't in your PHP code, but on server-side. You might have full mail or your provider/your server has problems with SMTP server. Check configuration/Contact provider.
使用 phpmailer 执行邮件任务,
http://sourceforge.net/projects/phpmailer/
它将允许您轻松调试电子邮件问题。
use phpmailer for mail tasks,
http://sourceforge.net/projects/phpmailer/
it will allow you to debug email problems easily.
如果您已经测试了 mail() 函数并且它发送了,那么我认为这与您的邮件设置无关。不过,有一点建议是,您需要小心放入 mail() 函数中的电子邮件地址。如今,许多托管提供商禁止您从未正式注册的域发送电子邮件(因此 [电子邮件受保护] 不起作用 - 它需要来自您的域并且需要是您设置的有效电子邮件地址 - 它不能是您真实域中的虚假地址)。
如果仍然无法正常工作,请尝试手动更新 php.ini 设置:
完成此操作后,请尝试将 mail() 放在脚本底部并向其提供一个变量。因此,一个例子可能是:
如果没有发送任何内容,我建议您重新评估代码以查看变量是否通过 if 语句进行过滤。如果所有其他方法都失败,请联系您的托管提供商并向他们描述您的邮件问题 - 毕竟这可能是服务器问题。如果您在本地主机上运行它,那么这完全是另一回事(在本地主机服务器上设置 mail() 非常棘手)。
If you've already tested the mail() function and it sends then I don't think it's anything to do with your mail settings. One word of advice, however, is that you need to be careful with the e-mail addresses you put into the mail() function. A lot of hosting providers nowadays prohibit you from sending e-mails from domains that aren't officially registered (so [email protected] would not work - it needs to be from your domain and it needs to be a valid e-mail address you've set up - it can't be a fake address at your real domain).
If it's still not working, try manually updating the php.ini settings:
Once this is done try putting your mail() at the bottom of the script and feeding one variable to it. So an example might be:
If nothing's being sent, I suggest you re-evaluate your code to see if variables are filtering through your if statements. If all else fails, contact your hosting provider and describe to them your mail problems - it might be a server issue after all. If you're running it on localhost, then that's a different matter entirely (it's quite tricky setting up mail() on a localhost server).
您是在 Windows 还是 Linux 上使用此代码?
邮件函数应该执行,您必须将 ipn 链接到重复的 ipn-handler php 文件,或者您没有正确地将更改保存到服务器。
否则,您的代码清晰明了,就没有任何意义,如果您在顶部发送邮件,它应该可以工作。
现在,如果您使用的是 Windows,mail() 通常不是最佳选择,因为 Windows 缺少默认的“sendmail”。
Are you using this code on Windows or on Linux ?
The mail function should execute you must be linking the ipn to a duplicate ipn-handler php file or you did not properly save the changes to the server.
Otherwise it just does not make sense your code is crisp clear and if you send out the mail right on the top it should work.
Now if you are on Windows mail() usually isnt the best choice as Windows lacks default 'sendmail'.