Home >Backend Development >PHP Tutorial >How to Send Multiple File Attachments in Emails Using PHP?

How to Send Multiple File Attachments in Emails Using PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 18:21:02415browse

How to Send Multiple File Attachments in Emails Using PHP?

How to Attach Multiple Files to an Email in PHP

In PHP, you can attach multiple files to an email and send them simultaneously. This is useful for sharing large or important documents.

Multipart MIME Format

To attach multiple files to an email, you need to use the Multipart MIME format. MIME (Multipurpose Internet Mail Extensions) allows you to send different types of data in a single email message.

PHP Code for Multiple File Attachments

Here's an example PHP code that demonstrates how to attach multiple files to an email:

<code class="php">if ($_POST) {
    // Get the file names
    $files = $_FILES['csv_file']['name'];

    // Email fields
    $to = "[email protected]";
    $from = "[email protected]"; 
    $subject = "My subject"; 
    $message = "My message";
    $headers = "From: $from";

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

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

    // Multipart boundary 
    $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"; 
    $message .= "--{$mime_boundary}\n";

    // Preparing attachments
    foreach ($files as $file) {
        $file_data = file_get_contents($file);
        $file_data = chunk_split(base64_encode($file_data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$file\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $file_data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

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

HTML Form for File Upload

This code can be used in conjunction with an HTML form that allows users to select multiple files:

<code class="html"><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></code>

The enctype="multipart/form-data" attribute must be added to the form to enable file uploads.

By implementing these methods, you can easily attach multiple files to an email and send them from a PHP script.

The above is the detailed content of How to Send Multiple File Attachments in Emails Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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