使用phpMailer发送的附件只有在它们是图像时才能打开

时间:2016-07-21 08:14:29

标签: php email phpmailer

<?php
require ("PHPMailerAutoload.php");
$mail = new PHPMailer;
$err='';
if(isset($_POST["submit"])){
   $uploadfile=tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'])); 
   move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile); 
   $date=date('Y-m-d');
   if(empty($_POST['name']) ||  
      empty($_POST['email']) ||
      empty($_POST['phone']) ||   
      empty($_POST['message'])) {
         echo "No arguments Provided!";
         return false;
   }
   $name = $_POST['name'];
   $phone = $_POST['phone']; 
   $email_address = $_POST['email'];
   $message = $_POST['message'];

   $to = "receiver@domain.com";
   $from="sender@domain.com";
   $password="password2";

   $mail->isSMTP();
   $mail->Host = 'smtp.gmail.com';
   $mail->SMTPAuth = true;
   $mail->Username = $from;
   $mail->Password = $password;
   $mail->SMTPSecure = 'tls';

   $mail->addAttachment($uploadfile,'My uploaded file');

   $mail->From = $from;
   $mail->FromName = $name;

   $mail->isHTML(true);
   $mail->Subject = 'Enquiry from website';
   $mail->Body = "Lots of irrelevant HTML is in here.";
   if(!$mail->send()) {
      echo 'Message could not be sent.'; 
      echo 'Mailer Error: ' . $mail->ErrorInfo;
      exit;
   }
   $err = 'MAIL has been sent successfully !!';
   $mail->ErrorInfo;
}

1 个答案:

答案 0 :(得分:1)

在PHPMailer中添加附件时,您的附件名称需要扩展名

$mail->addAttachment($uploadfile,'My uploaded file.pdf');

编辑:

在PHPMailer中添加附件时,您的附件名称需要扩展名

$ext = pathinfo($uploadfile, PATHINFO_EXTENSION);
$mail->addAttachment($uploadfile,'My uploaded file.'.$ext);