我正在编写一个应用程序,用于发送包含CSV作为附件的电子邮件。但是,它不起作用。 mail函数不返回false(暗示它成功);但是,电子邮件没有发送。
之前我已经从服务器发送/接收过电子邮件,所以我不相信这是问题所在。但是,我没有任何通过PHP发送附件的经验,所以我怀疑问题就在那里。我在一定程度上遵循了本教程:http://www.texelate.co.uk/blog/post/56-send-an-email-attachment-with-php/
以下是一些代码段:
$file = tmpfile();
// code omitted
$fileArray = array(); // this gets filled with more arrays/lines of data
// code omitted
try {
foreach($fileArray as $line) {
var_dump($line);
fputcsv($file, $line);
}
// begin mailing
$toEmail = 'myemail';
$subject = "Insurance Registration";
fseek($file, 0);
$attachData = fread($file, 1024);
$fileName = $lastName . "_" . date('Y');
$random = md5(time());
$boundary = "==Multipart_Boundary_x{$random}x";
$headers = "From: Test \nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed\n" . "boundary=\"{$boundary}\"";
$message = "Testing!.\n\n " . "-{$boundary}\n " . "Content-Type= text/plain; charset=\"iso-8859-1\n" . "Content-Transfer-Encoding 7bit\n\n " . "Testing Again " . "\n\n ";
$attachData = chunk_split(base64_encode($attachData));
$message .= "--{$boundary}\n" . "Content-Type: text/plain;\n" . "name=\"{$fileName}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileName}\"\n" . " . Content-Transfer-Encoding: base64\n\n" . $attachData . "\n\n" . "-{$boundary}-\n";
} catch (Exception $e) {
echo $e->getMessage();
}
if(mail($toEmail, $subject, $message, $headers)) {
} else {
$hasError = true;
}
if($hasError) {
echo "<h2>Error Submitting form. Please check your input and try again</h2>";
} else {
header('Location: success.php');
}
我被重定向到成功页面,但从未收到过电子邮件。谢谢!