Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Email

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

PayPal Button Click Sends Email

When your PayPal button is tapped, an email is sent to you — optionally, only when affiliate ID, ad link code, or other source information is available.

To restrict the email from being sent every time the PayPal button is tapped, specify (in the software) that it be sent only when one or both of these conditions exist:

  • URL parameter is present:

    When the buyer arrived at the web page using a URL that contains a parameter.

    A URL parameter is any information following a "?". Example:

    https://example.com/buy.php?source=123XYZ
    

    In the above link, source=123XYZ is the URL parameter.

  • Cookie is present:

    When the buyer arrived at the web page and their browser has a cookie. Because every browser may bring along a site cookie, a cookie name to look for may be specified.

When a person has no other means to do so, the system can provide statistics about what links, ads, sites, affiliates, or website areas result in the most sales.

The PayPal button code provided by PayPal as "buy" buttons are web page forms. The step-by-step instructions are easily translated for other forms.

The article's step-by-step uses PayPal button code for illustrations.

Ideas for Using the System

Here are examples of use.

  1. When an arrangement is made with someone related to PayPal sales, the person's link can contain a unique URL parameter (?person=name and ?aff=ID are examples).

  2. Links to the sales page in email, within articles, and in PDF links can have unique URL parameters so you can measure how many sales result from the tapping of various links.

  3. A unique cookie can be set when a download is initiated. Or when other actions are taken at your website, such as logging into a membership area.

Other actions can set a unique cookie or have links with unique URL parameters.

When the PayPal button is tapped, URL parameter information and any cookie content can be sent to you in an email.

How It Works

These 3 overview steps describe the essence of how the system works.

  1. The PayPal button form's <form… tag has this onsubmit attribute to launch a JavaScript Ajax function named SendFormClickedEmail().

    onsubmit="return SendFormClickedEmail()"
    
  2. A JavaScript Ajax function named SendFormClickedEmail() is launched when the PayPal button form is submitted. The function is located on the page with the PayPal button. When launched, the function sends email information to a PHP email-sending script on your server.

  3. An email is sent to you by the PHP email-sending script.

That's the essence — when the form is submitted, the Ajax sends information to a PHP script for an email to send to you.

Implementation Steps

These implementation steps are in reverse order from the How It Works steps.

The reason for the reverse order is that the email-sending PHP script needs to be installed before anything else can work. And the JavaScript Ajax function needs to be on the page before the PayPal button can launch it.

1. The Email-sending PHP Script

Here is the email-sending PHP script. Customization notes and other comments follow.

<?php
/*
Send Email With POST Information as Content
Version 1.0
July 13, 2019

Will Bontrager Software LLC
https://www.willmaster.com/
*/

/* ** *** ** * ** *** ** */
/* Customization section */

// Specify the email destination address 
//     and the email subject line.

$Destination = "name@example.com";
$Subject = "From PayPal Button";

/* End of customization section */
/* ** *** ** * **** * ** *** ** */

if( empty($_POST) ) { echo 'Inappropriate access.'; exit; }
$content = array();
foreach( $_POST as $k => $v )
{
    $afterKeyCharacter = ':';
    if( preg_match('/cookie/i',$k) )
    {
        $ta = array();
        foreach( preg_split('/; */',trim($v)) as $cookie )
        {
            if( strpos($cookie,'=') === false ) { continue; }
            list($cie,$val) = explode('=',$cookie,2);
            $ta[] = "$cie=$val";
        }
        $v = implode("\n\n",$ta);
        $k = "Cookies --\n";
        $afterKeyCharacter = '';
    }
    $content[] = "$k$afterKeyCharacter\n$v\n\n";
}
mail($Destination,$Subject,"Script: {$_SERVER['PHP_SELF']}\n\nData --\n\n".implode('',$content),"From: $Destination");
exit;
?>

Customization notes:

In the customization section, about 15 lines down from the top, you'll see these two lines.

$Destination = "name@example.com";
$Subject = "From PayPal Button";

Replace name@example.com with the email address to send the email to.

If you wish a different subject line for the email, replace From PayPal Button with your preferred subject line.

Installation notes:

The PHP script needs to be installed on the same domain as the web page with the PayPal button. Requiring the same domain is an Ajax security consideration.

The PHP script can be named anything with a .php file name extension. For the instructions in this article, it is assumed to be named paypalClickEmail.php and it's location on the server to be /php/paypalClickEmail.php

When you install this email-sending PHP script, make a note of it's server location.

2. The JavaScript Ajax Function

Here is the JavaScript with the Ajax functionality. A customization note and other comments follow.

NOTE: This JavaScript and accompanying instructions were updated on 16July2019 at 21:27 UTC to accomodate URL parameter and cookie name restrictions.

<script type="text/javascript">
function SendFormClickedEmail()
{
    var URLofMailSendingScript = "/php/paypalClickEmail.php";
    var MustHaveCookieNamed = "Test_cookie";
    var MustHaveParameter = false;
    if( MustHaveCookieNamed.length && document.cookie.indexOf(MustHaveCookieNamed)<0 ) { return true; }
    var query_string = location.search.length ? location.search.substring(1) : "";
    if( MustHaveParameter && (!query_string.length) ) { return true; }
    var http = new XMLHttpRequest();
    if(! http) { return true; }
    var params = new Array();
    params.push( "URL=" + encodeURIComponent(document.URL) );
    params.push( "Referrer=" + encodeURIComponent(document.referrer) );
    params.push( "URL Parameter=" + encodeURIComponent(query_string) );
    params.push( "Cookie=" + encodeURIComponent(document.cookie) );
    http.onreadystatechange = function() { if(http.readyState == 4) { if(http.status == 200) { return true; } } }
    http.open("POST",URLofMailSendingScript,true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.send(params.join("&"));
    return true;
}
</script>


Customization note:

At the first three lines of the function, you'll see this.

var URLofMailSendingScript = "/php/paypalClickEmail.php";
var MustHaveCookieNamed = "Test_cookie";
var MustHaveParameter = false;

The value of each of those variables may be updated.

Variable URLofMailSendingScript
If the email-sending PHP script was installed on your server in a different location, then replace /php/paypalClickEmail.php value between the quotation marks with the correct location.

Variable MustHaveCookieNamed
If a cookie must be present before an email will be sent, then replace Test_cookie value between the quotation marks with the correct cookie name. On the other hand, if there is to be no cookie limitation, then remove Test_cookie value from between the quotation marks (keep the quotation marks with nothing between them).

Variable MustHaveParameter
If a URL parameter must be present before an email will be sent, then replace value false with value true (no quotation marks here). If there is no URL parameter restriction, leave the false value as is.

Installation note:

Put the JavaScript anywhere on the web page that has the PayPal button. Immediately above the closing </body> tag is fine.

3. The Form Tag onclick Attribute

The onsubmit="return SendFormClickedEmail()" attribute needs to be inserted into the form tag that submits the information when the PayPal button is clicked.

Before the onclick attribute is inserted, the form tag looks something like this.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

After the onclick attribute is inserted, the form tag looks something like this (the inserted attribute colored blue in this example).

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" onsubmit="return SendFormClickedEmail()">

A note about non-PayPal button forms:

You can put the onsubmit="return SendFormClickedEmail()" attribute into any <form… tag. It doesn't have to be a PayPal button form.

That's the only difference between PayPal button and non-PayPal button forms in these Implementation Steps.

What You Have

What you have now is functionality with Ajax that will send the page's URL and any cookies to the PHP script, then continue with taking the buyer to PayPal. The PHP script will send you an email with information the Ajax sent to it.

In this way, you have a choice. You may use cookies or you may use a URL parameter like https://example.com/buy.html?AFF=ID (or you can use both)

Oh, yes, one more thing. Referrer information from browsers is not reliable. Some browsers won't send it. The email sent to you from the email-sending PHP script will include the referrer information if it's available. Otherwise, the referrer information will be blank.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC