In php mail() function, how can I send attachments? I didn't find parameter to do this. any help will be appreciated.

Dani AI

Generated

PHP's built-in mail() has no special "attachment" parameter. Attachments are sent by creating a MIME multipart message (usually multipart/mixed), adding a text part and one or more base64-encoded attachment parts, and sending that whole body with the correct headers. As suggested, using a maintained library (PHPMailer, etc.) is the easiest and most reliable route; those libraries handle boundaries, encoding, and SMTP/auth for you. If you prefer not to add a library, the example below shows a minimal, self-contained approach you can drop into an existing project so only the scripts that need attachments change.

<?php
function send_mail_with_attachment($to, $subject, $message, $filePath, $from) {
    $from = str_replace(["\r","\n"],'', $from); // prevent header injection
    $subject = str_replace(["\r","\n"],'', $subject);
    if (!is_readable($filePath)) return false;

    $eol = "\r\n";
    $boundary = md5(time());

    $headers  = "From: $from" . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"" . $eol;

    $body  = "--{$boundary}" . $eol;
    $body .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol . $eol;
    $body .= $message . $eol . $eol;

    $fileData = chunk_split(base64_encode(file_get_contents($filePath)));
    $filename = basename($filePath);

    $body .= "--{$boundary}" . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"{$filename}\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment; filename=\"{$filename}\"" . $eol . $eol;
    $body .= $fileData . $eol;
    $body .= "--{$boundary}--";

    return mail($to, $subject, $body, $headers, "-f$from");
}
?>

Notes and troubleshooting: always use CRLF (\r\n) for headers and boundaries; wrap base64 with chunk_split() (76-char lines); sanitize From/Subject to prevent header injection; use -f to set the envelope sender on systems that support it; check host limits (attachment size limits are common); test with small files first and verify with common providers (Gmail, Outlook). Old tutorial links sometimes go offline (as and observed) — prefer maintained libraries for production or when you need multiple attachments, HTML email, or SMTP authentication. To avoid touching many files, place a single wrapper function or include file and call it only where attachments are needed.

Recommended Answers

All 5 Replies

Is there attachment feature in standard php mail() function? I don't want to install additional application as I have bunch of files use mail(), I don't know if this new application will affect that.

This is easy to use and install ( only copy and paste file and include it in your script ) powerfull class, but you if want to write it by yourself you've got to play with the the headers ->

Hello, MitkOK :
Your specified link:

is expired. kindly check it & reply the solution if you know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.