Sending email is one of the most common tasks PHP is used for. Sending binary data in those emails is not quite as common, but it is worth studying just the same. In order to send binary data through email, you must prepare it first as most email systems will destroy the data if it is not encoded. The reason for this is that email was only meant to work with a given set of characters. Any characters outside of that range will not be transported by email properly. This is where the base64_encode function comes in. It encodes data into a character set that will properly transport through email. In this section we will see how to send a simple binary attachment through email.
But, before we jump into sending binary data with the PHP mail functions, let's first take a quick look at how to send a simple email with PHP. For sending mail, PHP provides the 'mail()' function. In order to use the mail function, we pass it a series of strings containing information such as the recipient, subject, and message. We can also specify additional headers for the email such as a reply-to address and content types. First, let's take a look at the prototype for the mail function, and then we will examine an example of how to utilize it.
bool mail (string to, string subject, string message [, string additional_headers [,string additional_parameters]])
As you can see, the first three parameters are mandatory. That is all that is needed for a simple email. The following example will only use the first three parameters. When we show you how to send an email with an attachment of binary data, we will utilize the additional headers.
<?php $to = 'someone@something.com'; $subject = 'Test Message'; $message = 'This is just a test message.'; mail ($to, $subject, $message); ?>
This is PHP in its simplest form. It will send an email with the specified subject and message to the email address in the $to variable. Now that we have seen how email is sent with PHP, let's take a look at how to send an attachment as well. As mentioned, we will need to encode the attachment so that it will properly transport through the email.
To accomplish the task of encoding the binary data properly, we can use the 'base64_encode()' function. This function will accept the data as a string and return a string that is encoded properly. To properly format this encoded data, you should then pass it to the 'chunk_split()' function we studied earlier. Using the default parameters for 'chunk_split()' will yield a string that is broken at every 76th character, with the '\r\n' character combination inserted. Now, let's see the prototype for each of these two functions, and then we will look at an example of sending an attachment with PHP.
string base64_encode ( string data)
string chunk_split ( string body [, int chunklen [, string end]])
Beyond encoding the binary data, in order to send the mail we must also specify some additional headers to the mail function. We must let it know that we will be sending MIME mail. This let's the program that sends the email know to expect the message to not be just plain text. We will also specify the content type in the headers. In the content type header we will tell the mail program how different parts of the message will be separated. This separator is just a unique string that will come before and after each part of the message.
Then, within the body of the message we will specify what type of data each part is. Now, let's take a look at a small script that will send an email with a jpg image attachment.
<?php $to = 'someone@something.com'; $subject = 'Test Message'; $message = 'This is just a test message.'; $jpgimage = '/path/to/image.jpg';
$border = "--==================_785422457==";
/* This places the contents of the file into a string and then encodes it with base64_encode. It also uses chunk_split to format the text properly */ $fd = fopen($jpgimage, "r"); $contents = fread($fd, filesize($jpgimage)); $encoded=chunk_split(base64_encode($contents)); fclose($fd);
You obviously will need to change the values in this script to reflect your personal settings.
We will not cover the details of processing an email with PHP, but if you were to do so you would use the 'base64_encode()' to decode anything encoded with 'base64_decode()'.