[英]why php mailer not send attachement?
I m using php mailer to send mail with attachment but i don't know why attachment not send else msg in body send properly and no error show plz give best solution for this 我正在使用php mailer发送带有附件的邮件,但我不知道为什么附件不发送其他消息,但正文正确发送并且没有错误显示plz为此提供了最佳解决方案
$to = 'ok123@gmail.com';
$msg = "This a body of a mail";
require_once("functions/class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->From = "noreply@gmail.com";
$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";
This is my favorite mail attachment class. 这是我最喜欢的邮件附件类。 Use it if you like it. 如果喜欢,请使用它。
class AttachmentEmail {
private $from = 'yours@email.com';
private $from_name = 'Your Name';
private $reply_to = 'yours@email.com';
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;
}
}
}
}
And use it like 并像这样使用
$sendit = new AttachmentEmail('test@example.com', 'Testing attachment!', 'Hi', '/home/test/test.jpg');
$sendit -> mail();
Gmail removes .zip
attachment when it detects particular files inside, like EXEs. 当Gmail检测到其中的特定文件(例如EXE)时,它将删除.zip
附件。 Add zip attachment as 20130319182911.piz
and it should arrive. 将压缩附件添加为20130319182911.piz
,它应该会到达。 It's not a phpmailer problem, but a Gmail policy. 这不是phpmailer问题,而是Gmail政策。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.