我有一个非常通用的表单。我希望用户输入他们的电子邮件地址,选择他们想要接收的任何文档,并希望生成一封电子邮件,其中包含他们选择的文档的下载链接。我过去也这样做过,但我的脑子一片空白。感谢您提供的任何帮助!谢谢
这是表格:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Form</title>
</head>
<body>
<form method="post" action="autoreply.php">
<label class="description" for="email">Your Email Address</label>
<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
<br />
Document 1:
<input type="checkbox" name="document[]" value="document1" />
<br />
Document 2:
<input type="checkbox" name="document[]" value="document2" />
<br />
Document 3:
<input type="checkbox" name="document[]" value="document3" />
<br />
<br />
<input type="hidden" name="send" value="1" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
这是我的PHP:
<?php
if(isset($_POST) && ($_POST['send'] == 1)){
$documents = array(
'document1' => 'http://www.example.com/document1.doc',
'document2' => 'http://www.example.com/document2.doc',
'document3' => 'http://www.example.com/document3.doc'
);
$to = '$email';
$subject = 'the subject';
$message = "hello'n'n";
if(isset($_POST['document']) && count($_POST['document']) > 0){
foreach($_POST['document'] as $doc){
if(isset($documents[$doc])){
$message .= "Here is ".$documents[$doc]."'n";
}
}
}
$headers = 'From: webmaster@example.com' . "'r'n" .
'Reply-To: webmaster@example.com' . "'r'n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
您应该使用附件来包含到文档中,或者如果文档是纯文本,则将它们包含在消息中。
当你想包含附件时,最方便的是使用现成的mailer类,比如swift mailer。
如果你想在你的消息中包括文档,你应该使用这样的东西:
if(isset($_POST['document']) && count($_POST['document']) > 0){
foreach($_POST['document'] as $doc){
if(isset($documents[$doc])){
$message .= "Here is ".$documents[$doc]."'n'n";
$message .= file_get_contents( filename_of($documents[$doc]) );
}
}
}
当然,您必须编写一个函数来从文档名称中获取文件名。