Click Here!
Evrsoft.com
Article Sections: | Internet Marketing | Web Design | Web Development | Business | Internet and Businesses Online | Self Improvement |  
>> Home > Internet Marketing > Site Promotion II

More Autoresponders With PHP

By Robert Plank
Posted Friday, November 19, 2004

Last week, we learned how to make simple text-only, one-shot autoresponders.

Link: (http://www.jumpx.com/tutorials/3)

After the article was published, I was asked to teach a method of creating follow-up responders. You know the type... someone subscribes and receives their initial message. After a few days, or a few weeks, or even months, another message in the autoresponder series is mailed out. This is useful if you're presenting a course that is split it up into many different lessons, which you want to give someone at a certain pace.

Well, I thought about showing you how to do that this week, but we're really not at that point yet. If I showed you how to do that, chances are I'd lose most of you entirely. I promise that we will get to that in the coming weeks, though.

I was also asked, however, if it were possible to create HTML autoresponders. And those are quite easy to make.

If you haven't read last week's tutorial, I recommend you do.

Now, this should be the code you have for you autoresponder (your personalized values will vary). Remember that our script was called "some-script.php" in our example.

Replace this line:

$headers = "Content-Type: text/plain; charset=us-asciinFrom: $myname nReply-To: nReturn-Path: nX-Mailer: PHP";

With this one:

$headers = "Content-Type: text/html; charset=iso-8859-1nFrom: $myname nReply-To: nReturn-Path: nX-Mailer: PHP";

If you look closely, all that we've done is basically changed the file format of the text we're sending. Upload it to your server and run the script like you did last time, entering your e-mail address in and submitting the form.

You should notice that the text looks slightly different. Let's make it completely different.

Replace this code:

$body = "Hi. This is the body of my message. Notice how I can continue typing right on the next line!";

With this:

$body = "Look, it's an HTML message!";

Reupload and try now. Look at what you've just done! An HTML autoresponder, by changing just a few lines of code.

Example here: (http://www.jumpx.com/tutorials/4/signup.html)

(I've modified it slightly.)

It might also interest you that an e-mail message can be sent using a variety of file types... not only in text and HTML but also in Rich Text Format (list more here)

Now that we've gotten that out of the way, let's move on to putting attachments in your e-mail autoresponder. Compared to a lot of the stuff we've gone over so far, this is really complicated.

I have an example here: (http://www.jumpx.com/tutorials/4/signup2.html)

At first glance, this seems like the same example I showed you just now. But if you put in your e-mail address and check your mail, you'll see that along with the e-mail is an attachment: gumby.jpg. Open it up and you'll see that it's a working JPEG image!

This is how you can make those autoresponders that mail the customer the product or e-book once they've bought. Or it would work as a really creative way to distribute an article or publication to someone. They might even think you personally sent the e-mail and the attachment.

Making it isn't going to be easy, though.

Let's start off by creating a whole new script. Let's call it "myscript.php". When I made my example I just copied the first few lines from some-script.php because we're going to use them again. So, let's do that:

$myredirect = "(http://www.jumpx.com/tutorials/3/thankyou.html)"; $myname = "Gumby";
$mymail = "null@jumpx.com";
$subject = "Attachment sample";

Of course you can change all of this... but this just sets the variables I'll use later my thank you page, from name, from address, and subject.

Next, I'll add my message.

$message = "Hey! Boy, have I got a file for you.

-Gumby";

This is going to be the plaintext message that accompanies the attachment. We could have gone without one, but since this is an autoresponder after all, I think we'd better keep a message in with the attachment.

Notice how I called this variable $message and not $body. This will come up again in a bit.

Next, we'll have to read the file we plan on sending. I haven't showed you file reading before, but hopefully it won't be too big of a deal. But I'll go over it line by line.

It's best to think of file reading in this way. Let's say that you are the computer. And all your papers are stored away in a filing cabinet. You want to know what's on a certain piece of paper, but first you have to take that piece of paper out and read all the way through it to know what's on that particular piece of paper. First:

$myfile = "gumby.jpg";

This just declares a variable that gives us the file we want to open. You don't *have* to do this, but I normally do just because it looks nicer.

$fp = fopen($myfile,"r");

We just opened up the file "gumby.jpg" (see how we called $myfile?) and assigned it to the file pointer called $fp. This file pointer is just a way of representing "gumby.jpg". I don't know how else to explain it in words.

What we want to do next is read all the data in "gumby.jpg" and stash it in a variable we can use later. So, we do this:

$contents = fread($fp,filesize($myfile));

What this does is look at our file pointer, $fp. The second parameter in this function (where it says "filesize($myfile)") tells PHP how far in the file to read. The filesize() function calculates the exact size of a file. So, let's say our file is 12 kilobytes in size. We want to sweep through all 12 KB of the file.

The contents of the file will be stored in a variable called $contents.

And lastly, we have to put that piece of paper away. So:

fclose($fp);

Closes that file.

So, now we've read an entire file into a variable. Just for fun, open up notepad or some other text editor you have handy, and open up an image file. Any image file.

Blechh!

Look at all those weird and funky characters. We need to make that look nicer if we plan on sending it over e-mail.

E-mail attachments usually use base 64 encoding. I won't go into details about it right now, but base 64 encoding is just a way of substituting all those weird characters in for nice readable letters.

There's a write-up on base 64 here if you're REALLY curious. But it isn't at all necessary reading.

(http://www.freesoft.org/CIE/RFC/2065/56.htm)

Conveiniently, though, PHP has a base64 encoding function. So, what we'll do is take $contents, which contains everything in that image file, and encode it into base64, storing the new data into a variable called $myimage.

$myimage = base64_encode($contents);

Now we have the file we want to send in the proper "encoded" format. And now for the fun part, the headers.

$headers = "From: $myname nReply-To: nReturn-Path: nX-Mailer: PHPn";

This line should look familiar to you. It's similar to the header line we had in some-script.php but without the Content-Type stuff. (You know, the chunk that told us if we wanted the message in plaintext or HTML.) We'll add the Content-Type information later.

One thing to note, is that since this is a multi-part MIME message, our entire message (even the "body") will be contained in the headers. So things might look a bit messy. You've been warned.

$headers .= "MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="myboundary"; Content-Transfer-Encoding: 7bitn";

I've split up the contents in $header for easier reading, though. If you see a ".=" and not just a "=", it means that instead of completely filling the variable with what we're adding, we're ADDING-ON to that variable.

In other words, I could have shown you $headers as one big long ugly string of text but I split it up so that I could explain each piece of it.

Look at that piece of code above. There isn't much you need to know, just that these are needed if you plan on doing a multi-part MIME message (which this is). One thing to notice is the part where it says boundary="myboundary";

You might be wondering what the heck those " are for. They're just a way of showing a quotation mark when you're already inside a set of quotation marks.

Example. We've used the echo function before. You can easily have a line of code that says: echo "John said hi. Hello there, John said.";

And it would output:
John said hi. Hello there, John said.

But what if you wanted to show quotes in there? You'd just do this: echo "John said hi. "Hello there," John said.";

And it would output:
John said hi. "Hello there," John said.

Easy? I hope so.

The purpose of the boundary is to separate each "chunk" of the e-mail message. Because when this gets to its destination, your recipient's mail client needs a way of knowing which part is the attachment and which part is your message, right?

Remember for now that our boundary line will be defined by the word "myboundary."

$headers .= "--myboundary
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bitnn";

Aha! And there it is, myboundary in all its glory. Where we say --myboundary is where we define each part of the message. You can call your boundary anything from bacon12345 to EatAtJoes. But for this example I just decided on myboundary.

At the start of each "chunk" of the message, you must set the content-type. You've seen this part before.

And now, for the actual message.

$headers .= $message."n";

That just plopped the message we wrote earlier (you know the one... "Hey! Boy, have I got a file for you.") into the message. Nothing really to see there, but note the extra line I added in afterwards. It's important to keep that there.

And now, to lay down our other chunk which contains the file:

$headers .= "--myboundary
Content-Type: image/jpeg; name=$myfile;
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$myimage
--myboundary--";

Pretty easy to understand there. The type is a JPEG, and we're just going to give it the same name as the actual file (gumby.jpg), it's base64 encoded, and it's an attachment. Then we go 2 lines down and give it out base64 encoded image. After that, on the next line, we give out our boundary again. Notice though, this time, the extra dashes on the end. It's --myboundary-- now, not just --myboundary. That means we're at the end of the message and there are no more chunks to deliver.

And the rest is simple!

if ($email != "") { mail($email,$subject,$body,$headers); } header("Location:$myredirect"); die();

If you decided not to go step by step with me here, or you just want the whole script to use, you can view the source of it here:

(http://www.jumpx.com/tutorials/4/myscript.phps)

I've taught you how to send an image as an attachment. But, one last thing before we're finished.

I know... that was a lot to take in one article. But there is just one last thing.

Chances are if you plan on using an autoresponder like this you'd use it to send an e-book, an application, whatever... and you probably wouldn't want it in the form of a JPEG file, am I right?

So we'll just make a simple modification here and set it up to send a zip file instead.

I've made a zip file containing this article and the files you need for it in a zip file called "tutorial4.zip" you can download it at:

(http://www.jumpx.com/tutorials/4/tutorial4.zip)

That is, if you need it.

I'm going to be sending tutorial4.zip as an attachment, instead of using gumby.jpg. Well, first of all, we have to change the file we plan on using. Find this line:

$myfile = "gumby.jpg";

And replace it with:

$myfile = "tutorial4.zip";

Next, we have to change the actual type of the file we're sending. So, find this:

Content-Type: image/jpeg; name=$myfile;

And replace it with:

Content-Type: application/zip; name=$myfile;

Now try it. Here's the example of file attachments as zip's: (http://www.jumpx.com/tutorials/4/signup3.html)

Remember that "image/jpeg", "application/zip" and all these are MIME types. If you go to the end of this page you'll see a short list of some: (http://www.infomotions.com/waves/mimetypes.html)

I don't know where you can find a longer list of MIME types, but you really have the potential to go anywhere with this. Attach HTML files on-the-fly, text files, word documents, MP3s, PDFs, SWFs... just go crazy.

Hopefully next week's tutorial won't be as brutal. :-)

About the Author
None

 






Click Here!


 

.

  Articles are submitted to EDN and licensed from various content sites.
  To report abuse, copyright issues, article removals, please contact [violations (at@) evrsoft.com]

  Copyright © Evrsoft Developer Network. Privacy policy - Link to Us

Contact Evrsoft