2013-03-20 27 views
1

我为使用PHP邮件与附件发送邮件,但我不知道为什么不附着在身体发 其他味精发送正确并且没有错误显示PLZ给最好的解决方案为这个为什么php邮件不发送附件?

$to = '[email protected]'; 
$msg = "This a body of a mail"; 
require_once("functions/class.phpmailer.php"); 
$mailer = new PHPMailer(); 

$mailer->From = "[email protected]"; 
$mailer->Subject = "attachment file"; 
$mailer->AddAddress($to); 
$mailer->ContentType = 'text/html'; 
$mailer->CharSet = "UTF-8"; 
$mailer->Body = $msg; 
$mailer->IsHTML(true); 
$mailer->AddAttachment("images/20130319182911.zip","20130319182911.zip"); 
$mailer->Send(); 
echo "Message Sent OK<p></p>\n"; 
+0

什么是Zip文件的大小? – 2013-03-20 08:20:39

回答

1

这是我最喜欢的邮件附件类。如果你喜欢,可以使用它。

class AttachmentEmail { 
    private $from = '[email protected]'; 
    private $from_name = 'Your Name'; 
    private $reply_to = '[email protected]'; 
    private $to = ''; 
    private $subject = ''; 
    private $message = ''; 
    private $attachment = ''; 
    private $attachment_filename = ''; 

    public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') { 
     $this -> to = $to; 
     $this -> subject = $subject; 
     $this -> message = $message; 
     $this -> attachment = $attachment; 
     $this -> attachment_filename = $attachment_filename; 
    } 

    public function mail() { 
     if (!empty($this -> attachment)) { 
      $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ; 
      $path = dirname($this -> attachment); 
      $mailto = $this -> to; 
      $from_mail = $this -> from; 
      $from_name = $this -> from_name; 
      $replyto = $this -> reply_to; 
      $subject = $this -> subject; 
      $message = $this -> message; 

      $file = $path.'/'.$filename; 
      $file_size = filesize($file); 
      $handle = fopen($file, "r"); 
      $content = fread($handle, $file_size); 
      fclose($handle); 
      $content = chunk_split(base64_encode($content)); 
      $uid = md5(uniqid(time())); 
      $name = basename($file); 
      $header = "From: ".$from_name." <".$from_mail.">\r\n"; 
      $header .= "Reply-To: ".$replyto."\r\n"; 
      $header .= "MIME-Version: 1.0\r\n"; 
      $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
      $header .= "This is a multi-part message in MIME format.\r\n"; 
      $header .= "--".$uid."\r\n"; 
      $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 
      $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
      $header .= $message."\r\n\r\n"; 
      $header .= "--".$uid."\r\n"; 
      $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here 
      $header .= "Content-Transfer-Encoding: base64\r\n"; 
      $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
      $header .= $content."\r\n\r\n"; 
      $header .= "--".$uid."--"; 

      if (mail($mailto, $subject, "", $header)) { 
       return true; 
      } else { 
       return false; 
      } 
     } else { 
      $header = "From: ".($this -> from_name)." <".($this -> from).">\r\n"; 
      $header .= "Reply-To: ".($this -> reply_to)."\r\n"; 

      if (mail($this -> to, $this -> subject, $this -> message, $header)) { 
       return true; 
      } else { 
       return false; 
      } 

     } 
    } 
    } 

而且使用它像

$sendit = new AttachmentEmail('[email protected]', 'Testing attachment!', 'Hi', '/home/test/test.jpg'); 
$sendit -> mail(); 
+1

考虑一个像PHPmailer一样的全功能类。它更安全,功能更丰富。 – Ghigo 2013-03-20 08:36:33

0

Gmail中删除.zip附件当它检测到特定的文件时,如EXE。添加zip附件为20130319182911.piz,它应该到达。 这不是phpmailer问题,而是Gmail政策。

+0

[根据Gmail帮助](http://support.google.com/mail/answer/6590?hl=zh-CN)*无法发送包含受密码保护的zip文件的zip文件*,但应该对一个简单的* zip *文件没有问题。 – 2013-03-20 08:19:17

+0

因为什么时候gmail封锁了zip文件?我昨天使用过,效果很好。请阅读http://support.google.com/mail/answer/6590?hl=zh-CN – 2013-03-20 08:19:21

+0

我会更好地解释。 – Ghigo 2013-03-20 08:29:41