我有一个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";
}
}
?>
如何在不收到上述错误的情况下将文件附加到此电子邮件并发送给它?
非常感谢您对此的任何帮助和帮助!
答案 0 :(得分:1)
您的代码太过破碎,无法修复,但这里有两个主要错误供您关注:
首先,您不使用$_FILES['file']
引用上传的文件,其位置存储在$_FILES['file']['tmp_name']
中。这是一个有用的教程:PHP 5 File Upload on W3schools。
其次,标题应使用\r\n
分隔。 mail()函数文档提供了有用的信息以及安全提示。