php - 如何在 PHP 中附加两个或多个文件并发送邮件

标签 php attachment email-attachments

<分区>

下面的代码只发送一个附件,但我需要附加并发送两个文件(一个rar文件和pdf)

$email_to = "$email"; // The email you are sending to (example)
$email_from = "online@example.co.in"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt = "text body of message"; // Message that the email has in it
$fileatt = "files/example.rar"; // Path to the file (example)
$fileatt_type = "application/x-rar-compressed"; // File Type
$fileatt_name = "example.rar"; // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: $email_from"; // Who the email is from (example)
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to,$email_subject,$email_message,$headers);

最佳答案

<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) {

    for($i=0; $i < count($_FILES['csv_file']['name']); $i++){

        $ftype[]       = $_FILES['csv_file']['type'][$i];
        $fname[]       = $_FILES['csv_file']['name'][$i];

    }


    // array with filenames to be sent as attachment
    $files = $fname;

    // email fields: to, from, subject, and so on
    $to = "example@gmail.com";
    $from = "example@gmail.com"; 
    $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
    for($x=0;$x<count($files);$x++){
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // send

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



}

?>

关于php - 如何在 PHP 中附加两个或多个文件并发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22245082/

相关文章:

javascript - 如何使用我的表单中存在的名称上传网络摄像头快照?

php - 来自 MYSQL 的值变为空

php - 插件更新后 Azure WordPress 站点返回 500 错误

c# - 使用本地数据库在 WPF 中存储/处理文件附件的最佳方式是什么?

使用 Intent 的 Android 多个电子邮件附件

django - 在Django中,如何将已上传到内存中的文件保存为邮件附件?

java - 无法以编程方式在电子邮件中附加图片

php - 如何在没有密码的情况下从 php 脚本运行 sudo su?

java - 如何以编程方式访问 GMAIL 附件?

java - 如何将大文件(在线存储)附加到电子邮件而不将整个文件复制到内存