PHP Mail Attachment
Send an Email Attachment with 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 any file to it.
Code Block 1: Basic header details are self explanitory. Specify who the email is being sent to ($to), who the mail is from ($from) and what the subject is ($subject).
Code Block 2: The Content-Type field for multipart entities requires one parameter, “boundary”, which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters (“-”, decimal code 45) followed by the boundary parameter value from the Content-Type header field. $hash[0] will store this value.
$hash[1] stores the encapsulation boundary for the email content which must be applied before each message part.
- <?php
- $filedir = 'files';
- $attachments = NULL;
- foreach ($fileArray AS $key => $value)
- {
- /* @Local function to get file type
- -----------------------------------------------------------*/
- $filetype = get_filetype($ext);
- $attachments .= <<<MAILATTACHMENT
- --$hash[1]
- Content-Type: $filetype; name="$value"
- Content-Transfer-Encoding: base64
- Content-Disposition: attachment
- $filechunks
- MAILATTACHMENT;
- }
- ?>
Code Block 3: Set up details for the file(s) to be attached. The actual file(s) is/are trivial in this case since the file(s) can be located on the server or directly from an upload. Whether one or twenty attachements, all will be part of the $hash[1] boundary.
Once the file(s) has/have been established, the correct MIME type associated with the file(s) (based on the file’s extention) must be obtained. The local function ‘get_filetype’ is not included in this tutorial but the end result for $filetype in this example is ‘image/jpg’ for the first file and ‘application/zip’ for the second file. For more information on MIME, please read up on it at Wikipedia.
Please note that any and all MIME content must never contain any tabs. The left-alignment of the $attachements variable inside the foreach loop must be <strong>without</strong> tabs and aligned completed left.
- <?php
- $headers = <<<MAILHEADERS
- From: $from
- Reply-To: $from
- MIME-Version: 1.0
- Content-Type: multipart/mixed; boundary="$hash[0]"
- MAILHEADERS;
- $message = <<<MAILBODY
- This is a multi-part message in MIME format.
- --$hash[0]
- Content-Type: multipart/alternative; boundary="$hash[1]"
- --$hash[1]
- Content-Type: text/plain; charset="utf-8"
- Content-Transfer-Encoding: quoted-printable
- Congrats. You have an attachment!
- --$hash[1]
- Content-Type: text/html; charset="utf-8"
- Content-Transfer-Encoding: quoted-printable
- <h2>Contrats!</h2>
- <p><strong>You</strong> have an attachment.</p>
- $attachments
- --$hash[0]
- MAILBODY;
- ?>
Code Block 4: Build the email headers and the email body.
Code Block 5: Finally, send the email using the internal PHP mail function.


