当前位置: 首页 > 知识库问答 >
问题:

如何在PHP中附加两个或多个文件并发送邮件[重复]

万俟旭
2023-03-14

下面的代码只发送一个附件,但我需要附加并发送两个文件(一个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);

共有3个答案

范俊逸
2023-03-14

在核心php邮件函数中检查以下多文件附件。

<?php
/** Mail with attachment */
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $bcc, $subject, $message){    
    $uid = md5(uniqid(time()));
    $mime_boundary = "==Multipart_Boundary_x{$uid}x"; 

    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Bcc: ".$bcc."\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$mime_boundary."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= nl2br($message)."\r\n\r\n";
    $header .= "--".$mime_boundary."\r\n";

    foreach($filename as $k=>$v){

        $file = $path.$v;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $header .= "Content-Type: application/octet-stream; name=\"".$v."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$v."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--".$mime_boundary."--"."\r\n";
    } 

    if (mail($mailto, $subject, "", $header)) {
        //echo "mail send ... OK"; // or use booleans here
        return true;
    } else {
        //echo "mail send ... ERROR!";
        return false;
    }
}
?>
曹和正
2023-03-14

遵循可重用性原则,您可以使用https://github.com/Synchro/PHPMailer

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name                               

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';
燕文昌
2023-03-14
<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>"; 
    } 



}

?>
 类似资料:
  • 我在发送带有多个附件的电子邮件时遇到问题。代码如下: 我收到邮件,但找不到任何附件。有人知道会出什么问题吗? 下面是我在Mail中得到的电子邮件正文:

  • 我需要将我的表单发送给多个收件人,但我想不出需要编辑哪一行。请看下文。我很感激你的帮助。 我已经尝试添加更多的值到电子邮件,但我不能使它工作。 我需要将我的表单发送给多个收件人,但我想不出需要编辑哪一行。请看下文。我很感激你的帮助。 我已经尝试添加更多的值到电子邮件,但我不能使它工作。 null null

  • 问题内容: 我想合并两个或多个视频文件(它们可能是两个mp4或两个3gp或任何其他格式)。 问题答案: 您可以使用的最通用的工具是ffmpeg(如上面@Jeremy所述),但是在手机上使用它需要做一些工作。它也是LGPL许可的,其某些编码器(特别是x264)是GPL。 如果您要连接的两个文件都使用类似的编码,并且包含在从MP4派生的文件格式中(例如3GP),那么一个更简单的解决方案是使用纯Java

  • 问题内容: 以下Java代码用于将文件附加到电子邮件。我想通过电子邮件发送 多个 文件附件。任何建议,将不胜感激。 问题答案: 好吧,自从我完成JavaMail工作已经有一段时间了,但是看起来您可以重复多次此代码: 例如,您可以编写一个方法来做到这一点: 然后从您的主要代码中,只需调用: 等等

  • 下面的脚本可以将pdf文件作为附件发送到Gmail和Outlook,但不会显示在Yahoo mail中。我的代码怎么了? 其次,当邮件被发送时,除了pdf附件本身,它不会携带邮件正文。任何关于如何添加带有pdf附件的邮件正文的建议都将不胜感激。

  • 问题内容: 我想将五个文件的内容照原样复制到一个文件中。我尝试使用cp为每个文件执行此操作。但这会覆盖从先前文件复制的内容。我也试过 它没有用。 我希望我的脚本在每个文本文件的末尾添加换行符。 例如。文件1.txt,2.txt,3.txt。将1,2,3的内容放入0.txt 我该怎么做 ? 问题答案: 您需要(concatenate的简称)命令,并使用shell重定向()进入输出文件