【发布时间】:2016-05-03 05:52:19
【问题描述】:
我有一个 HTML 注册/联系表单,目前通过电子邮件发送字段,没问题。 我遇到的问题是将上传的文件附加到电子邮件中。
使用我目前用于文件的 PHP,正在发送电子邮件,但 MIME 信息在电子邮件中,我收到错误消息,表明该消息“结构错误,无法完全检查”并且该文件未附加到电子邮件中。
<?php
if (isset($_POST['submit'])) {
//if ($_SERVER['submit']=="POST"){
$to = "me@mymail.com"; // Email address
$from = $_POST['text_96']; // sender's Email
$fullname = $_POST['text_95']; // users fullname
$email = $_POST['text_96']; // users email address
$dob = $_POST['date_12']; // Date of birth
$addr = $_POST['textarea_31']; // Address
$maths = $_POST['text_93']; // Maths
$english = $_POST['text_31']; // English
$holidayst = $_POST['date_74']; // Holiday1
$holidayend = $_POST['date_91']; // Holiday2
$distance = $_POST['number_58']; // Dist
$awayhome = $_POST['selectlist_90']; // AFW [boolean]
$reasontext = $_POST['textarea_60']; // 100 word answer
$subject = "New application submission"; // subject of email
$mssg = "New submission from " . $email . "\n\n" . "Full name: " . $fullname . "\nDOB: " . $dob . "\nAddress:\n" . $addr . "\n\nMaths: " . $maths . "\nEnglish: " . $english . "\nHoliday start: " . $holidayst . "\nHoliday end: " . $holidayend . "\nDistance to travel: " . $distance . "km" . "\nAFH?: " . $awayhome . "\nAppr choices: " . $apprent . "\n\nReason for applying: " . $reasontext . "\n";
foreach ($_POST['checkbox'] as $key => $val) { // Gets list of appr
$apprent .= $val . ' '; //
}
// -- -- -- \\
$fileatt = $_FILES['file'];
//$fileatttype = "application/pdf";
//$fileattname = "newname.pdf";
$headers = "From: $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}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; 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: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $mssg .
$data . "\n\n" .
"-{$mime_boundary}-\n";
if (mail($to, $subject, $message, $headers)) {
$message = NULL;
$headers = NULL;
header('Location: application_submitted.html');
} else {
header('Location: index.php');
//\\ -- -- -- //
//$headers = "From:" . $from;
//@mail($to,$subject,$message,$headers);
// You can also use header('Location: thank_you.php'); to redirect to another page.
//$message = NULL;
//$headers = NULL;
//header('Location: application_submitted.html');
//if (@mail($to, $subject, $message, $headers))
//$message = NULL;
//$headers = NULL;
//header('Location: application_submitted.html');
//else
//echo "Failed to send";
}
}
?>
请问如何在此电子邮件中附加文件并发送而不出现上述错误?
非常感谢您提供的任何帮助和帮助!
【问题讨论】:
-
使用phpmailer。从 PHP 7 开始,至少可以说使用多个标题(附件需要)是有问题的
-
@ChrisG 这从来都不是一个简单的选择 :-) 无论如何,我也喜欢 Swift Mailer。
-
@jeroen:确实如此。 Swift Mailer 和 PHPMailer 可能是当今最常用的。还好你加了那个,我都忘了:)
-
@ChrisG 谢谢。我以为我必须使用 PHPmailer 之类的东西,但对自己撒谎更容易;)谢谢你们的建议,我会试一试
标签: php html email attachment mime