Chủ Nhật, 13 tháng 1, 2008

Gởi mail kèm file bằng php

To send email attachments you need to make use of MIME (Multipurpose Internet Mail Extensions) - a mechanism that allows email to go beyond a basic, limited character set. MIME has many uses but for the purposes of this tutorial we will send a multipart/mixed MIME email; this means we can send a text email and attach a PDF file to it (for information on attaching other file types please see the end of this tutorial). The MIME side of things will be exaplined as we go. Firstly, let's set the email details and the attachment details up.

Code: Set the Email and Attachment Details

<?php

$to = "$name <$email>";
$from = "John-Smith <john.smith@domain.com>";
$subject = "Here is your attachment";

$fileatt = "/public_html/pdfs/mypdf.pdf";
$fileatttype = "application/pdf";
$fileattname = "newname.pdf";

$headers = "From: $from";

?>

Downloads: 8901

The email details are obvious; for the attachment we need the path to the file and the headers for the file type (PDF in this case). The $fileattname variable determines the name of the attachment - it doesn't have to match the name of the original file. Next, we need to transfer the file into a variable which we'll call $file.

Code: Read in the Attachment

<?php

$file = fopen( $fileatt, 'rb' );
$data = fread( $file, filesize( $fileatt ) );
fclose( $file );

?>

Downloads: 4466

Now the file has been read in it needs to be converted a format that is compatible with standard email: 7-bit ASCII. Before that, the appropriate headers need to be added to the email so the recipient knows what to expect.

Code: Add the MIME content

<?php

$semi_rand = md5( time() );
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "nMIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";

$message = "This is a multi-part message in MIME format.nn" .
"--{$mime_boundary}n" .
"Content-Type: text/plain; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" .
$message . "nn";

$data = chunk_split( base64_encode( $data ) );

$message .= "--{$mime_boundary}n" .
"Content-Type: {$fileatttype};n" .
" name="{$fileattname}"n" .
"Content-Disposition: attachment;n" .
" filename="{$fileattname}"n" .
"Content-Transfer-Encoding: base64nn" .
$data . "nn" .
"--{$mime_boundary}--n";

?>

Downloads: 6965

The conversion to 7-bit ASCII takes place at the $data = chunk_split( base64_encode( $data ) ) line. The file is then attached using the appropriate headers. Finally - send the email:

Code: Send the email

<?php

if( mail( $to, $subject, $message, $headers ) ) {

echo "<p>The email was sent.</p>";

}
else {

echo "<p>There was an error sending the mail.</p>";

}

}

?>

Downloads: 3930

You can attach any kind of file you like - you need to make sure that change the $fileatttype variable to reflect the content type of the file you are attaching (e.g. image/gif for a GIF file).

0 Nhận xét:

Đăng nhận xét

Đăng ký Đăng Nhận xét [Atom]

<< Trang chủ