PHPmailer发送未知格式的附件文件

时间:2013-09-09 10:04:33

标签: php phpmailer

<?php
$file           =   $_FILES["file"]["tmp_name"];
$file1          =   $_FILES["file1"]["tmp_name"];

$attachment_path    =   $file;
$attachment_path1   =   $file1;

$body   = "some text";

include("class.phpmailer.php");

//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set who the message is to be sent from
$mail->SetFrom('mail@example.com', $username);
//Set who the message is to be sent to
$mail->AddAddress('mail@example.com', 'Ticket Relief');
//Set the subject line
$mail->Subject = 'Inquiry from website by Form Fillup';
//Replace the plain text body with one created manually
$mail->Body = $body;
//Attach an image file
$mail->AddAttachment($attachment_path, "attachment1", "base64", "application/octet-stream");
$mail->AddAttachment($attachment_path1, "attachment1", "base64", "application/octet-stream");

//Send the message, check for errors
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Email has been sent! Please <a href='index.php'>Click Here </a> to go back to home page";
}

&GT;

和html代码

<form action="functions.php" method="post" enctype="multipart/form-data">

                   <div class="outer1">
                        <label> Please Choose File</label>
                        <input type="file" class="span4" name="file">
                    </div>
                    <div class="outer1">
                        <label> Please Choose File</label>
                        <input type="file" class="span4" name="file1">
                    </div>                     
                   <div class="outer1">
                        <input type="submit" class="submit btn btn-success btn-large pull-right" value="Submit">
                   </div>
                </form>         
  1. 我正在接收电子邮件但无法打开附件。它的格式未知。
  2. 如果文件超过200kb则发送邮件需要40-45秒,如果超过600kb需要超过1分钟才能处理
  3. 需要有关这些问题的帮助

1 个答案:

答案 0 :(得分:0)

首先,验证上传实际上是否成功。试试这个:

if(isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK && 
   isset($_FILES['file1']) && $_FILES['file1']['error'] == UPLOAD_ERR_OK

) {
  $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
  $mail->AddAttachment($_FILES['file1']['tmp_name'], $_FILES['file1']['name']); 
}
else {
  // Do something else, upload is not successful
}

您应该将文件名设置为第二个参数,而不仅仅是attachment1

之类的字符串
AddAttachment($path, $filename, ...
                      //^ put here the file name like $_FILES['file1']['name']