尝试附加pdf文件时,无法以PDF格式打开并说明其已损坏。我尝试了在网络上找到的几种解决方案,但它没有用。该文件正在附加和发送,但是当您打开PDF时它已损坏虽然正在下载但是尺寸无效(1kb),其中原始大小约为0.40mb
非常感谢任何帮助。
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\n";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from_name.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
}
/*
// Only accept POSTs from authenticated source
if ($_POST['HandshakeKey'] != 'outsource-phil') {
echo "<h1>You are not who you say you are, mister man.</h1>";
die();
}
*/
// EDIT FROM HERE DOWN TO
// CUSTOMIZE EMAIL
//*
// File to attach
$filename = "outsource.pdf";
$path = $_SERVER['DOCUMENT_ROOT']. "/wp-content/uploads/2014/02/";
// Who email is FROM
$from_name = "COmpany Name";
$from_mail = "company@domain.com";
$replyto = "company@domain.com";
// Whe email is going TO
$mailto = $_POST['Field3'];
// Subject line of email
$subject = "Your file has arrived!";
// Content of email message (Text only)
$requester = $_POST['Field12']; // Comes from Wufoo WebHook
$message = "Hey $requester,
Your custom email message
goes here";
// Call function to send email
mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message);
答案 0 :(得分:0)
编辑#1:在自己做了一些研究之后,我发现使用外部库来设置带附件的电子邮件要容易得多。我使用PHPMailer完成了这项工作,可以从https://github.com/Synchro/PHPMailer下载。
以下代码应该可以满足您的需求。我强烈建议使用PHPMailer等库。
// File to attach
$filename = "outsource.pdf";
$path = $_SERVER['DOCUMENT_ROOT']. "/wp-content/uploads/2014/02/";
// Who email is FROM
$from_name = "COmpany Name";
$from_mail = "company@domain.com";
$replyto = "company@domain.com";
// Whe email is going TO
$mailto = $_POST['Field3'];
// Subject line of email
$subject = "Your file has arrived!";
// Content of email message (Text only)
$requester = $_POST['Field12']; // Comes from Wufoo WebHook
$message = "Hey $requester,
Your custom email message
goes here";
/*
* The following code requires PHPMailer
*/
require_once('PHPMailer-master/class.phpmailer.php');
$mail = new PHPMailer();
$mail->AddReplyTo($replyto, $from_name);
$mail->SetFrom($from_mail, $from_name);
$mail->AddAddress($mailto, $requester);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAttachment($path . $filename);
if (!$mail->Send()) {
echo "Mail error!";
} else {
echo "Mail success!";
}
您似乎使用以下代码覆盖PDF文件:
$str = "Business Plan: \nSome more text";
$fp = fopen("outsource.pdf", 'w+');
fwrite($fp, $str);
fclose($fp);
w+
中的fopen()
选项会创建一个新的空白文件。您似乎想要发送原始文件,在这种情况下您不需要这些代码行。你想在这里完成什么?