Email not send to buyer in paypal using sandox ipn

Hi there i am working on a paypal plugin that add a responder, add products.

I am working on a sandbox for testing.

Read More

I make responder than add products related responder. On front end when i click on the “Buy” button, it goes to sandbox.paypal and here i complete the procedure of payment and on my dummy merchant account i accept the payment and know the email send to buyer of completion but the email not send i read the code 10 times but i dont get the issue.

Here is my code:

Paypal form:

<form name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_blank">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="'.$paypalID.'">
    <input type="hidden" name="return" value="'.$return_url.'">
    <input type="hidden" name="currency_code" value="'.$currency.'">
    <input type="hidden" name="item_name" value="'.$product_name.'">
    <input type="hidden" name="amount" id="p'.$product_id.'" value="'.$product_price.'">
    <input type="hidden" name="custom" value="'.$responderID.'">
    <input name="notify_url" value="'.plugin_dir_url( __FILE__ ).'ipn_sandbox.php" type="hidden">
    <input type="image" src="'.$upload_image.'" border="0" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>

And here is my ipn_sandbox.php code:

<?php
    // STEP 1: Read POST data
    // reading posted data from directly from $_POST causes serialization 
    // issues with array data in POST
    // reading raw POST data from input stream instead. 

    $raw_post_data = file_get_contents('php://input');
    $raw_post_array = explode('&', $raw_post_data);

    $myPost = array();
    foreach ($raw_post_array as $keyval) {
        $keyval = explode ('=', $keyval);

        if (count($keyval) == 2)
            $myPost[$keyval[0]] = urldecode($keyval[1]);
    }

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    if(function_exists('get_magic_quotes_gpc')) {
        $get_magic_quotes_exists = true;
    } 

    foreach ($myPost as $key => $value) {        
        if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
            $value = urlencode(stripslashes($value)); 
        } else {
            $value = urlencode($value);
        }
        $req .= "&$key=$value";
    }

    // STEP 2: Post IPN data back to paypal to validate
    $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    if( !($res = curl_exec($ch)) ) {
        curl_close($ch);
        exit; 
    }
    curl_close($ch);

    $path = $_SERVER['DOCUMENT_ROOT'];

    include_once $path . '/wp-config.php';
    include_once $path . '/wp-load.php';
    include_once $path . '/wp-includes/wp-db.php';
    include_once $path . '/wp-includes/pluggable.php';

    global $wpdb;
    if (strcmp ($res, "VERIFIED") == 0) {
        $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'];
        $responderID            =   $_POST['custom'];
        $name                   =   $_POST['first_name'];
        $payment_status         =   $_POST['payment_status'];
        $site_url               =   get_bloginfo('wpurl');
        $table_resp             =   $wpdb->prefix.'paypal_responders';
        $responder_to_use       =   $wpdb->get_row("SELECT * FROM $table_resp WHERE id ='$responderID'");
        $subject                =   $responder_to_use->subject;
        $from                   =   $responder_to_use->from_email;
        $attachment             =   $responder_to_use->attachment;
        $att_secure             =   $responder_to_use->att_secure;
        $message        .=  $responder_to_use->message_body;
        $message                .=  '<br /><br />
                                    <a title="Click here to download Attachment" href="'.plugin_dir_url(__FILE__).'responders/download.php?filename='.$att_secure.'" width="150" height="150" target="_blank">Click here to download Attachment</a>';

        if($message){
            $message    =   str_replace('[item_name]',$item_name,$message);
            $message    =   str_replace('[txn_id]',$txn_id,$message);
            $message    =   str_replace(' [mc_gross]',$payment_amount,$message);
            $message    =   str_replace('[mc_currency]',$payment_currency,$message);
            $message    =   str_replace('[receiver_email]',$receiver_email,$message);
            $message    =   str_replace('[payer_email]',$payer_email,$message);
            $message    =   str_replace('[name]',$name,$message);
            $message    =   str_replace('[site_url]',$site_url,$message);
            $message    =   str_replace('[payment_status]',$payment_status,$message);
        }else{
            $message    =       'Dear '.$name.',
                                Thank you for your purchase from '.$site_url.'. The details of your purchase are below.
                                Transaction ID: '.$txn_id.'
                                Item Name: '.$item_name.'
                                Payment Amount: '.$payment_amount.'
                                Payment Amount: '.$payment_status.'
                                Paid to: '.$receiver_email.'
                                Thanks and Enjoy!
                                ~Enigma Digital <br />
                                <br />
                                <a title="Click here to download Attachment" href="'.plugin_dir_url(__FILE__).'responders/download.php?filename='.$att_secure.'" width="150" height="150" target="_blank">Click here to download Attachment</a>';
        }

        $table          =   $wpdb->prefix . "paypal_transactions";
        $txn_id_check   =   $wpdb->get_results("SELECT * FROM $table WHERE txn_id ='$txn_id'");

        if(!$txn_id_check){
            $data   =   array(
                            'txn_id'            =>      $txn_id,
                            'product_name'      =>      $item_name,
                            'product_price'     =>      $payment_amount,
                            'payer_email'       =>      $payer_email,
                        );

            $wpdb->insert($table,$data) or die(mysql_error());
            $num = md5(time());

            $headers .= 'From: ' .$from. "rn" .'Reply-To: ' .$from . "rn";
            $headers  .= 'MIME-Version: 1.0' . "rn";
            $headers  .= "Content-Type: text/html; charset=iso-8859-1 ";
            $headers  .= "--".$num."--";

            //mail to buyer
            mail( $payer_email , $subject, $message, $headers );
        }
    }
?>

I also use wp_mail() except mail() but nothing happen.

Please can anyone help what the issue is.

Related posts

Leave a Reply

3 comments

  1. There are a few issues with the code, and a few things you can do to commence the debugging process. It’s unlikely you’ll get a straightforward fix here though, as it’s an open-ended question.

    Before doing anything, please ensure error_reporting is set to E^ALL and display_errors is set to On in your php.ini. (Turorial, if you need it). Check your error_log – if one exists – for immediate clues.

    You need to declare $message before appending more data to it. You begin appending data to $message on line 77 ($message .= $responder_to_use->message_body;) but it hasn’t been set yet, which will produce a Warning.

    There are now 3 reasons why the email itself wouldn’t be sent:

    1. Your server isn’t setup to send mail.
    2. $txn_id_check isn’t set or is false. This would be the case if $txn_id is not in your database. I’m assuming you add this row when beginning the transaction – but have you checked?
    3. You have a syntax error in one of the included files.

    Let’s address #1. Create a file on the server with 1 line that sends you a simple email:

    <?php mail('your@email', 'Test email', 'Yay, I can send mail!'); ?>
    

    Visit that file in your browser. Check your inbox, and check your spam. No mail? That’s the issue.


    Next #2: This is likely a problem with the script that adds the transaction to the database before the user even gets to PayPal. We don’t have this code, but it’s easy for you to check – just check the DB to see if you have any rows present.


    And #3: Syntax errors should be easy to check for here, just visit the IPN script directly in your browser. Fatal errors will be displayed to you.


    Still not working? There may be issues elsewhere. The best way to check is with some basic debugging…

    Start by sending the full $_POST array to yourself over email, right at the top of the script.

    <?php
        mail('your@email', 'Full Post Data - '.time(), print_r($_REQUEST, true));
    
        // STEP 1: Read POST data
        // reading posted data from directly from $_POST causes serialization 
        // issues with array data in POST
        // reading raw POST data from input stream instead. 
    
        $raw_post_data = file_get_contents('php://input');
        $raw_post_array = explode('&', $raw_post_data);
    
        [...]
    

    And email yourself the output of the script right at the end.

    <?php
        ob_start();
    
        $time = time(); // Referenced in your email subjects. It will get confusing with lots of these emails otherwise. Gmail and other clients who use a conversation view will properly catalogue these emails if the subjects match.
    
        mail('your@email', 'Full Post Data - '.$time, print_r($_REQUEST, true));
    
        // STEP 1: Read POST data
        // reading posted data from directly from $_POST causes serialization 
        // issues with array data in POST
        // reading raw POST data from input stream instead. 
    
        [...] // Rest of your script here...
    
        $output_buffer = ob_get_contents();
        ob_end_clean();
    
        mail('your@email', 'Output buffer - '.$time, $output_buffer);
    ?>
    
  2. $cleanedFrom = 'Ashutosh';
                $to ='awesomeworks1@gmail.com; 
                $subject = 'CONTACT US';
                $headers = "From: " . $cleanedFrom . "rn";
                $headers .= "MIME-Version: 1.0rn";
                $headers .= "Content-Type: text/html; charset=ISO-8859-1rn";
                $message= '<html><body>';
                $message .= '<table rules="all" style="border-color: #68A1D7;" cellpadding="15" width="450" align="center">';
                $message .= "<tr style='background: #68A1D7; color: #fff;'><td colspan='2'><strong>CONTACT US</strong></td></tr>";
                $message .= "<tr><td><strong>Name:</strong> </td><td>" .$ram[0]['name'] . "</td></tr>";
    
                $message .= "<tr style='background:#68A1D7;'><td colspan='2'>&nbsp;</td></tr>";
                $message .= "</table>";
                $message .= "</body></html>";
                $send =    mail($to, $subject, $message, $headers);
                if($send)
                {
                echo "<script> alert('Message Sent. Thank you') </script>";
                echo "<script>window.location='$webroot'</script>";
                exit;
    
    
                }
                else
                {
                echo "<script> alert('Message Not Sent. Thank you') </script>";
                echo "<script>window.location='$webroot'</script>";
                exit;
    
                }
    
  3. Turn on mail in your php.ini.

    You need to have something like this:

    In php.ini:

    [mail function]
    ; For Win32 only.
    ;SMTP = localhost
    ;smtp_port = 25
    
    ; For Win32 only.
    ;sendmail_from = me@example.com
    
    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    sendmail_path = /usr/sbin/sendmail -t -i -f  me@example.com
    

    The lines that start with “;” are commented out. So make sure you uncomment the right part depending which operating system you are on.