PHP Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
turnej
Forum Newbie
Posts: 1
Joined: Thu Jan 13, 2011 7:15 am

PHP Help

Post by turnej »

Hi Guys,

I am using IPN with my site and want to send an email to the purchasers once the purchase is complete, but during testing in sandbox the email is not being sent and I can's see why, any ideas?

<?php


// PHP 4.1

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

if ($payment_status=="Completed")
{
if ($payment_amount==29.99&&$payment_currency=="GBP")
{

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>

any help much appreciated.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PHP Help

Post by Jade »

Are you sure you're getting a successful payment response message? Also, please put PHP code tags around your PHP code.

Code: Select all


echo $payment_status;
if ($payment_status=="Completed")
{
echo "<br/>$payment_amount $payment_currency";
if ($payment_amount==29.99&&$payment_currency=="GBP")
{

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
}
}
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP Help

Post by danwguy »

Not sure if this helps or not but I had a problem with email not sending and found that this part of your code...

Code: Select all

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

Should look like this...

Code: Select all

// post back to PayPal system to validate
$header .= 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n";
$header .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
$header .= 'Content-Length: " . strlen($req)' . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

That's what fixed my problem, worth a shot.
Riquez
Forum Newbie
Posts: 10
Joined: Sun Feb 17, 2008 6:39 pm

Re: PHP Help

Post by Riquez »

The problem could be email not sent or something else in your PayPal communication.

Just test it separately - make a file with only the mail() function & run it with your email address.
Did you get an email?
If yes, the problem is with your paypal communication.
If no, then sendmail isnt configured correctly on the server.

Here is a mail function that should work for you to test.

the -f attribute can sometimes help for servers that strip headers, so use that to be safe.

Code: Select all

$to="myname@server.com";
$subject = "This is a test";
$header = "From: website@server.com\n";
	
$message = "============\n";
$message .="This is a test.\n";
$message .="============\n";

mail($to,$subject,$message,$header, '-f website@server.com');
Post Reply