Home > Backend Development > PHP Tutorial > How to send multiple files as attachments in an email using PHP?

How to send multiple files as attachments in an email using PHP?

Susan Sarandon
Release: 2024-11-02 21:51:02
Original
760 people have browsed it

How to send multiple files as attachments in an email using PHP?

Attaching and Sending Multiple Files via Email in PHP

The original task was to modify code that could only send one file attachment to send two or more files. This revised code addresses this requirement by allowing multiple file attachments to be sent in an email:

<code class="php"><form action="#" method="POST" enctype="multipart/form-data">
    <input type="file" name="csv_file[]" /><br>
    <input type="file" name="csv_file[]" /><br>
    <input type="file" name="csv_file[]" /><br>
    <input type="submit" name="upload" value="Upload" /><br>
</form>

<?php

if($_POST) {
    // Create arrays for file information
    $ftype = $_FILES['csv_file']['type'];
    $fname = $_FILES['csv_file']['name'];

    // Define files to be attached
    $files = $fname;

    // Email fields
    $to = "recipient@example.com";
    $from = "sender@example.com";
    $subject = "Attachments";
    $message = "Email with attached files";
    $headers = "From: $from";

    // Set MIME boundary
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

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

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

    // Prepare each attachment
    foreach ($files as $key => $value) {
        $file = fopen($value, "rb");
        $data = fread($file, filesize($value));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fname[$key]\"\n" .
        "Content-Disposition: attachment;\n" . " filename=\"$fname[$key]\"\n" .
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // Send email
    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
        echo "<p>Email sent to $to!</p>";
    } else {
        echo "<p>Email could not be sent!</p>";
    }
}

?></code>
Copy after login

The above is the detailed content of How to send multiple files as attachments in an email using PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template