从 PHP 表单处理程序中删除附件

问题描述 投票:0回答:1

我的网站上有一个网络表单,其中包含许多数据字段,包括文件上传字段。

我的网站上有另一个页面需要更简单的表单,因此我使用了较长的表单,并将其缩小为仅包含用户名和用户电子邮件。

我已经测试了新的(简单)表单,效果很好。

但是,文件附件语言仍在表单处理程序中,并且仍然发送空附件。

我需要一些帮助来删除所有文件附件语言。

我尝试自己剥离这些部分,但每次尝试,表格都无法发送。

我已经包含了表单处理程序的工作版本,其中仍然包含文件附件语言。

表单处理程序:

<?php

$name = $_POST['userName'];
$email = $_POST['userEmail'];

$composition =

"\r\nName: "           . $name .
"\r\nEmail Address: "  . $email;

$subject ="Email Subject here";

$fromname ="$name";
$fromemail = "$email";

$mailto = '[email protected]';

//The Content Action

$content = file_get_contents($tmpName);
$content = chunk_split(base64_encode($content));

// A random hash will be necessary to send mixed content

$separator = md5(uniqid(time()));

// Carriage return type (RFC)

$eol = "\r\n";

// Main header (multipart mandatory)

$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed;". $eol. " boundary=\"" . $separator . "\"" . 
$eol;

// $headers .= "Content-Transfer-Encoding: 7bit" . $eol;

$headers .= "This is a MIME encoded message." . $eol . $eol;

// Composition action

$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $composition . $eol;

// The attachment action

$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream;". $eol. " name=\"" . $fileName . "\"" . 
$eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;

$body .= "Content-Disposition: attachment;". $eol . " filename=\"" . $fileName."\"" . $eol . 
$eol ; 
$body .= $content . $eol;
$body .= "--" . $separator . "--";

mail($mailto, $subject, $body, $headers);

header('location: /somefolder/somepage');

?>
php webforms
1个回答
0
投票

只需删除代码中附件操作块中的行

另一方面,最好通过以下方式修改你的代码:

  1. 使用
    utf-8
    字符集而不是
    iso-8859-1
  2. 添加行 $subject= "=?UTF-8?B?".base64_encode($subject).'?=';这样主题行也可以包含 utf-8 字符

所以修改后的代码将是(我添加了一些非英文字符来显示效果):

<?php

$name = $_POST['userName'];
$email = $_POST['userEmail'];

$composition =

"\r\nName: "           . $name .
"\r\nEmail Address: "  . $email;

$subject ="Email Subject here 中文字";

$fromname ="$name";
$fromemail = "$email";

$mailto = '[email protected]';

//The Content Action

$content = file_get_contents($tmpName);
$content = chunk_split(base64_encode($content));

// A random hash will be necessary to send mixed content

$separator = md5(uniqid(time()));

// Carriage return type (RFC)

$eol = "\r\n";

// Main header (multipart mandatory)

$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: text/plain; charset='utf-8'" . $eol;


$body .= "This is the message 中文字 " . $composition. $eol;


$subject= "=?UTF-8?B?".base64_encode($subject).'?=';    

mail($mailto, $subject, $body, $headers);

header('location: /somefolder/somepage');
?>
© www.soinside.com 2019 - 2024. All rights reserved.