View Full Version : Paypal IPN help!!

02-28-2010, 04:49 PM
Hello all! I've had this paypal IPN for some time now and it works perfectly. So here's what happens:

The user pressed "purchase now" on my site and they pay the selected amount, then this script is run somewhere down the line and the paypal users receives an email with the contents below. This works perfectly, but I would like one more thing to happen.

I have an online mailing list that users can subscribe to by clicking a link on my home site. It stores the email address in a text file, which looks like this:


I don't know what it would be or where to put it, but it would somehow write the user's paypal email into that text file, so they are subscribed to the mailing list. Here's my IPN code:

// 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 .= "Host: www.paypal.com:443\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.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'];

//set email variables
$From_email = "From: email@email.com";
$Subject_line = "Thing";
$email_msg = "Body Contents

if (!$fp) {
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment

$mail_From = $From_email;
$mail_To = $payer_email;
$mail_Subject = $Subject_line;
$mail_Body = $email_msg;

mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation

$mail_From = $From_email;
$mail_To = $receiver_email;
$mail_Subject = "Oops!";
$mail_Text = "Something went wrong.

mail($mail_To, $mail_Subject, $mail_Text, $mail_From);

fclose ($fp);

Thank you in advance!

02-28-2010, 10:50 PM
No one?