0

我想创建一个联系表格,其中电子邮件与自动生成的附件文件 (.txt) 一起发送。附件应包括联系表单中存在的输入字段的所有值(无论它们是否为空)。这可以通过使用 PHP mail() 来完成吗?我的代码现在只能将联系表单内容作为电子邮件正文发送:

<?php
define("WEBMASTER_EMAIL", 'info@email.com'); 
error_reporting (E_ALL); 


if(!empty($_POST))
{
$_POST = array_map('trim', $_POST); 
$name = htmlspecialchars($_POST['name']);
$email = $_POST['email'];
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);

$error = array();


if(empty($name))
{

$error[] = 'Please enter your name';

}


if(empty($email))
{

$error[] = 'Please enter your e-mail';


}

 elseif(!filter_var($email, FILTER_VALIDATE_EMAIL))
{ 

$error[] = 'e-mail is incorrect';

}


if(empty($message) || empty($message{15})) 
{

$error[] = "Please enter message more than 15 characters";

}

if(empty($error))
{  

$body = '
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><td>Name: </td><td>&nbsp;</td><td>'.$name.'</td><tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>Email: </td><td>&nbsp;</td><td>'.$email.'</td><tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>Subject: </td><td>&nbsp;</td><td>'.$subject.'</td><tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>Message: </td><td>&nbsp;</td><td>'.stripslashes($message).'</td><tr>
</table>
</body>
</html>
';


$mail = mail(WEBMASTER_EMAIL, 'Message sent from website', $body,
    "MIME-Version: 1.0" . "\r\n"
."Content-type: text/html; charset=UTF-8" . "\r\n"
."From: ".$email." \r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());

if($mail)
{
echo 'OK';
}

}
else
{
echo '<div class="notification_error">'.implode('<br />', $error).'</div>'; 
}
}  
?>
4

1 回答 1

0

要使用表单值生成文件,请参阅此示例

如果您想使用带有文本和附件的 php mail() 发送电子邮件,您必须稍微弄乱标题。添加Content-Type: multipart/mixed;到标题中,请参见示例

于 2012-10-17T08:48:15.680 回答