0
當我將附件更改爲html文件時,下面的代碼工作得非常好,但是當我將附件更改爲圖像時,例如screenshot.png,它無法發送消息。在php中的圖像附件未能發送?
<?php
$file_path = "screenshot.png"; // server path where file is placed
$file_path_type = "image/png"; // File Type
$file_path_name = "screenshot.png"; // this file name will be used at reciever end
$from = "[email protected]"; // E-mail address of sender
$to = "[email protected]"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.<br><br>Thank You!<br><a href='http://7tech.co.in'>7tech.co.in Team</a>";
$headers = "From: ".$from;
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_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";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if(@mail($to, $subject, $message, $headers)) {
echo "File send!";
} else {
echo 'Failed';
}
?>
你們可以指出error.I've試圖在1-2地方太cahnge內容類型,但它不是我working.Am缺少什麼?
嘗試在整個腳本中用'\ r \ n'替換'\ n' – bitWorking 2013-04-20 20:30:48
感嘆。爲什麼每個人都會花費很多精力來編寫代碼,以便在像phpMailer和Swiftmailer這樣的類存在時使用php發送電子郵件附件。他們是免費的,開源的,安全的,你可以用三行代碼完成整個事情。最重要的是,他們出色地開箱工作,不需要混淆所有base64和mime邊界的東西。認真地說,只需扔掉所有的代碼,並下載phpMailer。你會感謝我的。 – Spudley 2013-04-20 22:01:20