看着并尝试了一切,但似乎无法弄清楚这一点。我有一个表单,用户通过复选框选择他们想要的某些文件。这将被传递,他们应该发送所有文件。一切正常,但它不会发送任何pdf格式的文件。它发送所有其他格式,而不是pdfs ...
我正在使用下面的代码。它使用一个数组来保存文件名,我尝试只使用一个pdf,但它仍然无法正常工作。想到也许pdf太大了所以我只使用了一个小页面,但仍然没有。请帮忙!
<?
function mail_attachment($files, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$uid = md5(uniqid(time()));
$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/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
foreach ($files as $filename) {
$file = $path.$filename;
$name = basename($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
set_magic_quotes_runtime(0);
$content = chunk_split(base64_encode($content));
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types 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."--";
return mail($mailto, $subject, "", $header);
}
?>
更新:尝试使用swiftmail建议但同样的事情。除了附加pdf之外,新的电子邮件实例工作正常。所以这条线有效:
$message->attach(
Swift_Attachment::fromPath('file.docx')->setFilename('file.docx')
);
但这条线不是:
$message->attach(
Swift_Attachment::fromPath('file.pdf')->setFilename('file.pdf')
);
答案 0 :(得分:0)
更改:
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
为:
$header .= "Content-Type: application/pdf; filename=\"".$filename."\"\r\n\r\n";
<?
function mail_attachment($files, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$uid = md5(uniqid(time()));
$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/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
foreach ($files as $filename) {
$file = $path.$filename;
$name = basename($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
set_magic_quotes_runtime(0);
$content = chunk_split(base64_encode($content));
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Type: application/pdf; filename=\"".$filename."\"\r\n\r\n"; $header .= $content."\r\n\r\n";
}
$header .= "--".$uid."--";
return mail($mailto, $subject, "", $header);
}
?>
附件不在标题中。他们应该只声明MIME标头
<?php
$sendto = "email@example.com";
$message = "Message here\r\n Hello, this is a test!!";
$filename = "file.pdf";
$handle = fopen($filename, 'rb');
$contents = fread($handle,filesize($filename));
fclose($handle);
$encoded = chunk_split(base64_encode($contents));
$seperator = md5(uniqid(time()));
$from = '"First Last" <username@domain.com>';
$header = '';
$header .= "From: $from\r\n";
$header .= "MIME-Version: 1.0\r\nContent-Type:"." multipart/mixed; boundary=\"$seperator\";\r\n";
$body.= "--$seperator\r\n";
$body.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$body.= "Content-Transfer-Encoding: 7bit\r\n\n";
$body.= $message."\r\n\n";
$body.= "--$seperator\r\n";
$body.= "Content-Type: application/pdf; name=\"file.pdf\"\r\n";
$body.= "Content-Transfer-Encoding: base64\r\n";
$body.= "Content-Disposition: attachment; filename=\"file.pdf\"\r\n\n";
$body.= $encoded."\r\n";
$body.= "--$seperator--\r\n";
mail($sendto,$subject, $body, $header);
?>
答案 1 :(得分:0)
了解使用$files
数组可以执行的操作。
<?php
// array with filenames to be sent as attachment
$files = array("image.jpg","file.pdf","file3.txt");
// $files = array("LakeRules.pdf","TermsConditions.pdf");
// email fields: to, from, subject, and so on
$to = "email@example.com";
$from = "email@example.com";
$cc = "";
$subject = "Files";
$message = "$emailBody";
$headers = "From: $from\r\n";
$headers .= "cc: $cc";
// 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/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
//}
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>
答案 2 :(得分:0)
好吧,仍然不知道为什么我的脚本不适用于pdf但我终于找到了解决方案。 Swiftmailer仍然提出了一个问题,直到我使用SMTP然后它工作正常。如果有人有任何见解,为什么会这样,我很想知道答案,但至少它是有效的!谢谢大家的帮助!