我这次正在尝试发送带有附件代码的电子邮件似乎是正确的,而无需发送带有附件的电子邮件。 但是我得到了错误,因为PHP解析错误:语法错误,第77行文件意外结束 缺少什么???
<?php
if(isset($_FILES) && (bool) $_FILES) {
$AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
$files = [];
$server_file = [];
foreach($_FILES as $name => $file) {
$file_name = $file["name"];
$file_temp = $file["tmp_name"];
foreach($file_name as $key) {
$path_parts = pathinfo($key);
$extension = strtolower($path_parts["extension"]);
if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
$server_file[] = "uploads/{$path_parts["basename"]}";
}
for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
}
$headers = "From: $from";
$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";
$message .= "--{$mime_boundary}'n";
$FfilenameCount = 0;
for($i = 0; $i<count($server_file); $i++) {
$afile = fopen($server_file[$i],"rb");
$data = fread($afile,filesize($server_file[$i]));
fclose($afile);
$data = chunk_split(base64_encode($data));
$name = $file_name[$i];
$message .= "Content-Type: {'"application/octet-stream'"};'n" . " name='"$name'"'n" .
"Content-Disposition: attachment;'n" . " filename='"$name'"'n" .
"Content-Transfer-Encoding: base64'n'n" . $data . "'n'n";
$message .= "--{$mime_boundary}'n";
}
if($_POST['submit'] !='')
{
$name = htmlspecialchars($_REQUEST['name']);
$email = htmlspecialchars($_REQUEST['email']);
$mobile = htmlspecialchars($_REQUEST['mobile']);
$company = htmlspecialchars($_REQUEST['company']);
$qty = htmlspecialchars($_REQUEST['qty']);
//$upload = htmlspecialchars($_REQUEST['upload']);
$msg = htmlspecialchars($_REQUEST['msg']);
}
$to="example@gmail.com";
$subject = "Order Information";
$message .= "Name: " . $name . "'n";
$message .= "Email: " . $email . "'n";
$message .= "ContactNo: " . $mobile . "'n";
$message .= "Company: " . $company . "'n";
$message .= "Quantity: " . $qty . "'n";
//$message .= "Upoload: " . $upload . "'n";
$message .= "Message: " . $msg . "'n";
if(mail($to, $subject, $message, $headers))
{ echo 'thank you'; }
else{ echo 'error';}
?>
你得到Syntax error, unexpected end
,因为你错过了这一行的结束括号:
if(isset($_FILES) && (bool) $_FILES) {
另请注意,当您修复此解析错误时,您将收到一些未定义的索引通知,因为您在if($_POST['submit'] !='')
内部定义了$name, $email
等并在检查外部使用。
完全修改的代码:
<?php
if(isset($_FILES) && (bool) $_FILES) {
$AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
$files = [];
$server_file = [];
foreach($_FILES as $name => $file) {
$file_name = $file["name"];
$file_temp = $file["tmp_name"];
foreach($file_name as $key) {
$path_parts = pathinfo($key);
$extension = strtolower($path_parts["extension"]);
if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
$server_file[] = "uploads/{$path_parts["basename"]}";
}
for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
}
$headers = "From: $from";
$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 .= "--{$mime_boundary}'n";
$FfilenameCount = 0;
for($i = 0; $i<count($server_file); $i++) {
$afile = fopen($server_file[$i],"rb");
$data = fread($afile,filesize($server_file[$i]));
fclose($afile);
$data = chunk_split(base64_encode($data));
$name = $file_name[$i];
$message .= "Content-Type: {'"application/octet-stream'"};'n" . " name='"$name'"'n" .
"Content-Disposition: attachment;'n" . " filename='"$name'"'n" .
"Content-Transfer-Encoding: base64'n'n" . $data . "'n'n";
$message .= "--{$mime_boundary}'n";
}
}
/** Your submit block **/
if(isset($_POST['submit']))
{
$name = htmlspecialchars($_REQUEST['name']);
$email = htmlspecialchars($_REQUEST['email']);
$mobile = htmlspecialchars($_REQUEST['mobile']);
$company = htmlspecialchars($_REQUEST['company']);
$qty = htmlspecialchars($_REQUEST['qty']);
$msg = htmlspecialchars($_REQUEST['msg']);
$to="example@gmail.com";
$subject = "Order Information";
$message .= "Name: " . $name . "'n";
$message .= "Email: " . $email . "'n";
$message .= "ContactNo: " . $mobile . "'n";
$message .= "Company: " . $company . "'n";
$message .= "Quantity: " . $qty . "'n";
$message .= "Message: " . $msg . "'n";
if(mail($to, $subject, $message, $headers)) {
echo 'thank you';
}
else {
echo 'error';
}
}
?>
更新 1:
我不知道你为什么要在这行末尾使用$message
:
$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";
它还会给你未定义的变量通知,只需删除它。
试试这段代码:
<?php
if(isset($_FILES) && (bool) $_FILES) {
$AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
$files = [];
$server_file = [];
foreach($_FILES as $name => $file) {
$file_name = $file["name"];
$file_temp = $file["tmp_name"];
foreach($file_name as $key) {
$path_parts = pathinfo($key);
$extension = strtolower($path_parts["extension"]);
if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
$server_file[] = "uploads/{$path_parts["basename"]}";
}
for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
}
$headers = "From: $from";
$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";
$message .= "--{$mime_boundary}'n";
$FfilenameCount = 0;
for($i = 0; $i<count($server_file); $i++) {
$afile = fopen($server_file[$i],"rb");
$data = fread($afile,filesize($server_file[$i]));
fclose($afile);
$data = chunk_split(base64_encode($data));
$name = $file_name[$i];
$message .= "Content-Type: {'"application/octet-stream'"};'n" . " name='"$name'"'n" .
"Content-Disposition: attachment;'n" . " filename='"$name'"'n" .
"Content-Transfer-Encoding: base64'n'n" . $data . "'n'n";
$message .= "--{$mime_boundary}'n";
}
if($_POST['submit'] !='')
{
$name = htmlspecialchars($_REQUEST['name']);
$email = htmlspecialchars($_REQUEST['email']);
$mobile = htmlspecialchars($_REQUEST['mobile']);
$company = htmlspecialchars($_REQUEST['company']);
$qty = htmlspecialchars($_REQUEST['qty']);
//$upload = htmlspecialchars($_REQUEST['upload']);
$msg = htmlspecialchars($_REQUEST['msg']);
}
$to="amar.ghodke30@gmail.com";
$subject = "Order Information";
$message .= "Name: " . $name . "'n";
$message .= "Email: " . $email . "'n";
$message .= "ContactNo: " . $mobile . "'n";
$message .= "Company: " . $company . "'n";
$message .= "Quantity: " . $qty . "'n";
//$message .= "Upoload: " . $upload . "'n";
$message .= "Message: " . $msg . "'n";
if(mail($to, $subject, $message, $headers))
{ echo 'thank you'; }
else{ echo 'error';}
} // This one is missing :)
?>
文件末尾缺少"}"。
此行之前缺少一个右括号...
if($_POST['submit'] !='')
您尚未关闭第一个 if 条件块。