开发者

Paypal IPN and mail() function

开发者 https://www.devze.com 2023-03-21 21:49 出处:网络
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.

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('test@email.com', 'New Order', 'New Order', 'From: support@website.com');

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);


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):

updatedb;
locate mail.log

or

locate maillog

this assumes you are using linux, but the problem might as well exists also on windows


The code seems correct to me.

My advice:

  1. Create a new PHP script and test the function there. Does it work?
  2. Attempt PHP SMTP authentication with your mail server and send the email that way. Does it work? (You can use the PEAR Mail Package or any other valid SMTP class.)
  3. If the above also fails then attempt to use the SMTP script with a custom service (e.g GMail) and check if emails are being sent. Here are the GMail SMTP parameters.

If all of the above fail, the problem is definitely with your hosting provider.


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.


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.


use phpmailer for mail tasks, http://sourceforge.net/projects/phpmailer/ it will allow you to debug email problems easily.


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 test@email.com 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:

<?php ini_set ( sendmail_from, "my_email@my_server.com" ); ?>

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:

mail('test@email.com', 'New Order', $iid, 'From: support@website.com');

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).


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'.

0

精彩评论

暂无评论...
验证码 换一张
取 消