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.
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.
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.
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:
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ủ