简体   繁体   中英

Send multiple attachments with php

I have a form that is successfully sending an uploaded image as an attachment via php, but I cannot get it to send multiple attachments.

The html:

<input id="file-3" type="file" name="image" multiple="true" required>

The php that is attaching a single file:

if (isset($email) && isset($name)) {
$to = "email@email.com"; /* <= Change this Email ID to yours */
$subject = "$name sent you a message"; /* <= Change the Subject If you want */
$random_hash = md5(date('r', time())); 

$headers = "From: $email\r\nReply-To: $email"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = null;
if ( isset($_FILES["photo"]) ) {
     $attachment = chunk_split(base64_encode(file_get_contents($_FILES["photo"]["tmp_name"])));
} 

$message = "--PHP-mixed-" . $random_hash . "\r\n";
$message .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\r\n\r\n";
$message .= "--PHP-alt-" . $random_hash . "\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "Name: " . $name . "\r\nEmail: " . $email . "Message: " . $textarea . "\r\n\r\n";
$message .= "--PHP-alt-" . $random_hash . "\r\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\t\nContent-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "";
if ( $attachment != null ) {
    $message .= "--PHP-mixed-" . $random_hash . "\r\n";
    $message .= "Content-Type: " . $_FILES["photo"]["type"] . "; name=\"" . $_FILES["photo"]["name"] . "\"\r\n";
    $message .= "Content-Transfer-Encoding: base64\r\n";
    $message .= "Content-Disposition: attachment\r\n\r\n";
    $message .= $attachment;
    $message .= "\r\n\r\n--PHP-mixed-" . $random_hash . "\r\n";

}

I have a JS file that is using ajax to submit the form, but I don't believe it has anything to do with this issue.

Thanks for any help! I'm a new to php.

Refer this http://www.codexworld.com/send-html-email-multiple-attachments-php/

function multi_attach_mail($to, $subject, $message, $senderMail, $senderName, $files){

    $from = $senderName." <".$senderMail.">"; 
    $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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

    // preparing attachments
    if(count($files) > 0){
        for($i=0;$i<count($files);$i++){
            if(is_file($files[$i])){
                $message .= "--{$mime_boundary}\n";
                $fp =    @fopen($files[$i],"rb");
                $data =  @fread($fp,filesize($files[$i]));

                @fclose($fp);
                $data = chunk_split(base64_encode($data));
                $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
                "Content-Description: ".basename($files[$i])."\n" .
                "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
        }
    }

    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $senderMail;

    //send email
    $mail = @mail($to, $subject, $message, $headers, $returnpath); 

    //function return true, if email sent, otherwise return fasle
    if($mail){ return TRUE; } else { return FALSE; }

}

//email variables
$to = 'receiver email id';
$from = 'info@codexworld.com';
$from_name = 'CodexWorld';

//attachment files path array
$files = array('screencapture-www-codexworld-com.png','Send_Email_with_pdf_attachment_in_PHP.docx');
$subject = 'PHP Email with multiple attachments by CodexWorld'; 
$html_content = '<h1>PHP Email with multiple attachments by CodexWorld</h1>
            <p><b>Total Attachments : </b>'.count($files).' attachments</p>';

//call multi_attach_mail() function and pass the required arguments
$send_email = multi_attach_mail($to,$subject,$html_content,$from,$from_name,$files);

//print message after email sent
echo $send_email?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM