我已经实现了一个php邮件功能,用于发送带附件的邮件,但我收到了0 kb附件的邮件。我正在使用的代码如下:
<?php
$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment
$email_from = ""; // Who the email is from
$email_subject = ""; // The Subject of the email
$email_txt = ""; // Message that the email has in it
$email_to = ""; // Who the email is too
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_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" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
我无法弄清楚这段代码的问题。请有人帮帮我!!
答案 0 :(得分:4)
也许我是一个理想主义的傻瓜,但是PHP的mail()函数一直存在问题,而且似乎一直存在。
我倾向于建议所有新项目使用邮件程序库,例如Swiftmailer,(或类似),因为它们倾向于处理边缘情况比滚动自己更好,而且它们一直都是对可能被邮寄的奇怪事物进行了更广泛的测试。
除了PHP本身包含的PHP库外,还有大量的PHP库资源,许多框架也提供了Mail类。它已经到了使用框架和库生成一个更坚固的应用程序的地步,而不是重新发明许多轮子,并且自己动手去做所有事情。