///Sending Cookies By Email

Sending Cookies By Email

Sending Cookies By Email

An interesting question arrived in my email box a few days ago.

The sender is running an affiliate program that uses cookies to determine which affiliates are credited with sales.

The question was how to use Master Form V3 (http://willmaster.com/mfv3) to send an email containing the affiliate cookie.

The method described here can be used with many, if not most, “form to email” web page form handling programs. The parts that apply specifically to Master Form V3 are so noted.

How It Works

An IFRAME tag is put on the post-sale “thank you” page.

The content of the IFRAME is a web page that obtains the cookie (or cookies) and automatically submits them to a CGI program on the server. It uses JavaScript to read cookies and insert their value into a hidden form field. The form is then automatically submitted.

The CGI program emails the cookies — just the value (if only one cookie) or both the name and the value (if several cookies).

How To Do It

It requires four steps.

The first three steps create a web page that retrieves the cookie (or cookies), inserts the information into a form, and automatically submits it to a form handling program. This is the web page that will be in the IFRAME.

The fourth step integrates the system into the post-sale “thank you” page by putting the web page created in the first three steps into an IFRAME.

Step 1: Retrieving the Cookie or Cookies

This JavaScript can retrieve one cookie, when you know the name of the cookie. It can also retrieve all available cookies — for when you don’t know the name of the cookie or when you just want to retrieve them all.

Create a new web page to insert the JavaScript into. The JavaScript can be in the HEAD or BODY area.

No changes need to be made to this JavaScript.

<script type="text/javascript" language="JavaScript">

<!-- Copyright 2005 Bontrager Connection, LLC

function GetCookieByName(n) {
var ckcontent = '';
if(document.cookie.length < 1 || n.length < 1) { return; }
var ckname = n + '=';
var ckbegin = document.cookie.indexOf(ckname);
if(ckbegin == -1) { return; }
var ckend = 0;
ckbegin += ckname.length;
ckend = document.cookie.indexOf(";",ckbegin);
if(ckend < ckbegin) { ckend = document.cookie.length; }
ckcontent = document.cookie.substring(ckbegin,ckend);
return ckcontent;
}

function GetAllCookies() {
var ckcontent = '';
if(document.cookie.length < 1) { return; }
var allcks = document.cookie.split(';');
for(var i = 0; i < allcks.length; i++) {
while(allcks[i].indexOf(" ") == 0) {
allcks[i] = allcks[i].substr(1);
}
}
ckcontent = allcks.join("\n");
return ckcontent;
}

// —>
</script>

Step 2: Inserting Cookies Into a Form Field

Paste this form somewhere below the JavaScript, in the BODY area of the new web page you created in step 1.

Change the action=”___________” to the URL of your form handling program.

The redirect emailtemplate hidden fields are for Master Form V3. If you’re using a different form handling program, use hidden fields your program expects.

<form name="MyForm" 

action="/cgi-bin/MasterFormV3.cgi"
method="POST">
<input
type="hidden"
name="redirect"
value="/blank.html">
<input
type="hidden"
name="emailtemplate"
value="contact/cookiemail.txt">

<!-- The following writes form hidden field
name="onecookie" with the value of
a cookie named "AffiliateCookie" —>
<script type="text/javascript" language="JavaScript"><!--
var acookie = GetCookieByName("AffiliateCookie");
document.write('<input name="onecookie" value="');
document.write(acookie);
document.write('" type="hidden">');
//--></script>

<!-- The following writes form hidden field
name="allcookies" with the name and
value of all available cookies. —>
<script type="text/javascript" language="JavaScript"><!--
var cookies = GetAllCookies();
document.write('<input name="allcookies" value="');
document.write(cookies);
document.write('" type="hidden">');
//--></script>

</form>

An HTML comment above each of the two JavaScript sections notes what the JavaScript does. Keep one or both.

If you keep the one that retrieves the value of a cookie named “AffiliateCookie” and writes it into a hidden field, you’ll need to change “AffiliateCookie” in the JavaScript code with the name of the cookie you want to retrieve.

No modifications need to be made to the JavaScript that writes the name and value of all available cookies into a hidden form field.

If you use Master Form V3 —

Here is an example email template you can use with the above form:



To: will@example.com

Subject: Cookie enclosed

AffiliateCookie cookie: [[onecookie]]

All cookies —

[[allcookies]]

Step 3: Automatically Submitting the Form

Put this JavaScript below the form of step 2. It will automatically submit the form to the form handling CGI program.

The JavaScript is assuming the form’s name is “MyForm”. If you change the name of the form in step 2, the name will also need to be changed in this JavaScript. (The name is between two periods on the second line.)

<script type=”text/javascript” language=”JavaScript”><!–

setTimeout(“document.MyForm.submit()”,1000);

//–></script>

The setTimeout() function is used to wait a second, to ensure all cookie actions have had time to complete, before automatically submitting the form.

At this point, you have a web page composed of the code in each of the first three steps. When this web page is loaded, it will automatically submit the form to the form handling program.

Load the web page into your browser now to verify it works as it should.

Troubleshooting —

If the form isn’t submitted, there may be a JavaScript error. The Firefox browser from http://www.mozilla.org/products/firefox/ is also an excellent JavaScript debugging tool. Simply load the web page, then use menu “Tools | JavaScript Console.” It will load a window with any JavaScript errors accompanied by a short explanation.

If the form is submitting, but the form handling program gives an “Internal Server Error,” see http://willmaster.com/internalservererror

If the form appears to be running okay, but no email arrives, see http://willmaster.com/emaildebug

When the web page automatically submits the form and you receive email from the form processing program (whether or not cookie information is included), go to the step 4, the last step.

Step 4: Integrating Into “Thank You” Page

To cause cookie values to be inserted into the form, create an IFRAME on the post-sale “thank you” page. The SRC of the IFRAME is the URL to the web page created in the previous three steps.

For this example, we’ll make a rather small IFRAME so it’s invisible on the web page. The IFRAME works just as well whether large or small.

Here is the IFRAME (supposing the web page created in the previous steps is at http://example.com/cookieharvest.html)

<iframe 

src="http://example.com/cookieharvest.html"
height="5"
width="5"
scrolling="no"
frameborder="0">
</iframe>

That will create an IFRAME 5 pixels square, with no border and no scrollbars. (Use a larger IFRAME for initial testing so any error messages the form handling program may send are visible.)

Make the background color of the cookieharvest.html web page identical to the post-sale “thank you” page containing the IFRAME. Otherwise, the difference in background colors may make the IFRAME obvious.

When the form in the IFRAME is automatically submitted, the top-left 5 pixels of the confirmation page the form handling program generates (or redirects to) will appear with the IFRAME. If that top-left corner of the confirmation page is empty of text and images, and the background color is also identical to the post-sale “thank you” page, the IFRAME may never be noticed.

Of course, the IFRAME may be larger, obvious, and serve a purpose in addition to sending cookies by email. As an example of such a purpose, the confirmation page the form handling program generates (or redirects to) might contain a special thank you with an invitation to an exclusive customer’s area.

2010-05-26T16:50:27+00:00 March 14th, 2005|JavaScript|0 Comments

About the Author:

Leave A Comment