Update Buat Tags PHP Supaya Tidak Bisa Download Langsung
May 17

Sometimes you may prefer to send information as an e-mail attachment rather
than as an e-mail message. For example, you may want to send the 30-page
service contract to all your customers as an attachment, rather than as a
30-page e-mail message.
To send a message as an attachment, you send a mail header instructing that
the e-mail be sent as an attachment. The header is as follows:
Content-disposition: attachment; filename=test.txt
The header tells the e-mail software to send the message as an attachment
with the filename of test.txt. The following example shows how to send a
short message as an attachment, although it’s very unlikely that you’d actually
want to do this:
$to = “janet@valade.com”;
$subj = “Testing an attachment”;
$mess = <<< END
This is the test message.
This message should arrive as an attachment.
Let’s see what happens.
END;
$headers = “Content-disposition: attachment;
filename=test.txt\n”;
$headers .= “cc:sales@mycompany.com\n”;
$mailsend = mail($to,$subj,$mess,$headers);
This e-mail message has two headers: the Content-disposition header and
the cc header. The headers are written together into one string, stored in the
variable $headers. Each header ends with \n. With some e-mail software, to
get the same effect you may need to use \r\n at the end.
If you want to send e-mail as an attachment, it’s probably because you want
to send the contents of a file. To do this, you simply read the file into a variable
that you then send as the message. Reading data from a file is discussed
in more detail in Chapter 12.
For the purpose of sending e-mail, you can store the read file as one long string.
PHP provides the file_get_contents function for that purpose, as follows:
$mess = file_get_contents(“filename”);
Listing 13-3 shows a script that sends a text file as an attachment.

<?php
/* Script name: mailTest
* Description: Sends a text file as an e-mail
* attachment.
*/
$filename = “mydata.txt”;
$mess = file_get_contents($filename);
$to = “janet@valade.com”;
$subj = “Sending mydata as an attachment “;
$headers = “Content-disposition: attachment;
filename=mydata.txt\n”;
if(!$mailsend = mail($to,$subj,$mess,$headers))
{
echo “Mail not sent\n”;
}
else
{
echo “Mail sent\n”;
}
?>
This script reads the contents into a string with file_get_contents. The
header is stored in $headers. Then the mail is sent with the mail function.
An if statement tests whether the mail function succeeds, printing the
appropriate message.
You may want to send another type of file as an attachment, not just a text
file. To do this, you need to send headers that tell the e-mail software what
type of file you’re sending. One such header is the Content-type header,
which you can send as follows:
Content-type: contenttype
The content type for plain text is text/plain, the type of file in the previous
examples. In most cases, text/plain is the default. Another type of text file
is an HTML file, which has the content type of text/html.
Other types of files you may want to send are binary files. For example, image
files, audio files, and video files are binary files. Some of the file types for these
files are as follows:
image/gif
image/jpeg
audio/x-wav
audio/vnd.rn-realaudio
video/mpeg
video/avi
You also may want to send application files. Some are text files, and some are
binary files. For example, an RTF file is a text file, whereas a Word document or an Excel spreadsheet are binary files. A general type for binary files is
application/octet-stream. If the file is binary and you’re unsure what
application generated it, try application/octet-stream.
Binary files should be encoded when sent over e-mail, to ensure that they arrive
in good shape. PHP provides functions that encode files for you, as follows:
$mess = chunk_split(base64_encode($string));
The variable $string contains the contents of the binary file stored with the
fread function.
In addition, if you send an encoded file, you need to let the mail software know
about that as well, with the following header:
Content-Transfer-Encoding: base64
The script in Listing 13-4 shows how to send a graphics file (binary and
encoded) as an e-mail attachment.
Listing 13-4: A Script to Send a Graphics File as an E-Mail Attachment
<?php
/* Script name: mailGraphic
* Description: Sends a graphic file as an e-mail
* attachment.
*/
$filename = “logo.gif”;
$fh = fopen($filename,”rb”);
$fileContent = fread($fh,filesize($filename));
fclose($fh);
$mess = chunk_split(base64_encode($fileContent));
$to = “janet@valade.com”;
$subj = “Sending an image as an attachment”;
$headers = “Content-disposition: attachment;
filename=logo.gif\n”;
$headers .= “Content-type: image/gif\n”;
$headers .=”Content-Transfer-Encoding: base64\n”;
if(!$mailsend = mail($to,$subj,$mess,$headers))
{
echo “Mail not sent\n”;
}
else
{
echo “Mail sent\n”;
}
?>
This script encodes the file contents to store in $mess. It includes the additional
headers needed for transferring information that’s not text.

source : php4 for dummies

Berikan Komentar