如何使用php在邮件中附加多个文件?

如何使用php在邮件中附加多个文件?,php,html,css,database,Php,Html,Css,Database,您可以使用以下功能发送多封电子邮件附件: <?php header('Content-type: application/json'); $status = array( 'type'=>'success', 'message'=>'Thank you for contact us. As early as possible we will contact you ' ); $name = @trim(stripslashes($_POST['name'

您可以使用以下功能发送多封电子邮件附件:

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Thank you for contact us. As early as possible  we will contact you '
);  

$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'reflexivezhcet@gmail.com';//replace with your email

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;
function多附件邮件($to、$subject、$message、$senderMail、$senderName、$files){
$from=$senderName.“;
$headers=“From:$From”;
//边界
$semi_rand=md5(time());
$mime_boundary=“==Multipart_boundary_x{$semi_rand}x”;
//附件的标题
$headers.=“\n时间版本:1.0\n”。“内容类型:多部分/混合;\n”。“边界=\”{$mime\U边界}\”;
//多部分边界
$message=“-->$mime\u boundary}\n.”内容类型:text/html;字符集=\“UTF-8\”\n。
“内容传输编码:7bit\n\n”。$message.\n\n”;
//准备附件
如果(计数($files)>0){

对于($i=0$i@rahul如果你意识到你的语言有问题,那么请改变它。因此你认为它没有问题,但你确实因为它而请求原谅?你的语言有什么需要原谅的地方?可能是你选择的表达方式?这似乎是请求原谅的最明显的原因。请详细说明。
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('path/to/file1.etx', 'path/to/file2.etx');
$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>";