所以我被要求创建一个表单,一旦填写,就会通过表单条目向我的客户端发送一封电子邮件,这必须包含带有所包含数据的.csv文件附件。
最初我通过将其硬编码到普通页面来测试它。
<form class="main-form" method="post">
<ul>
<li><div>Email: </div><input type="email" name="email" size="20" /></li>
<br />
<input type="submit">
</ul>
</form>
<?php
$email=$_REQUEST['email'];
if ($email =="") {
//Do nothing
} else {
$to = "mail@test.co.uk";
$from = "test";
$subject = "test";
//Message Body
$text = "test";
//The Attachment
$cr = "\n";
$data = "Email"
$data .= "$email". $cr;
$fp = fopen('test.csv','a');
fwrite($fp,$data);
fclose($fp);
$attachments[] = Array(
'data' => $data,
'name' => 'diploma_apprenticeship_form_sub.csv',
'type' => 'application/vnd.ms-excel'
);
//Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Add the headers for a file attachment
$headers = "MIME-Version: 1.0\n" .
"From: BRS\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$text . "\n\n";
//Add attachments
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ;
}
$message .= "--{$mime_boundary}--\n";
mail($to, $subject, $message, $headers);
}
?>
此工作正常,并按照我的要求通过电子邮件发送附件。然而,他们现在想要它作为短代码,以便他们可以在wordpress构建的任何地方发布它。所以我添加了短代码并将代码发布到php函数中,当wordpress检测到[form]
时调用该函数。但现在它不起作用。
function main_form() {
return <<<HTML
<form class="main-form" method="post">
<ul>
<li><div>Email: </div><input type="email" name="email" size="20" /></li>
<br />
<input type="submit">
</ul>
</form>
HTML;
// Then rest of php as it was above
}
表单输出正常,但我不再收到电子邮件。最初我认为$_REQUEST[];
没有获取数据,但我将$ email设置为固定字符串,但它仍然不起作用。
有人有什么想法吗?
答案 0 :(得分:0)
将您的HTML更改为以下内容:
<form class="main-form" method="post" action="file.php">
您的表单将被提交后,您将被重定向到“file.php”。
您可以在此处找到一个有效的示例:http://www.w3schools.com/php/php_forms.asp
答案 1 :(得分:0)
原来我在PHP的其余部分之前使用了一个return语句,因此它正在打印HTML并退出该函数。通过将返回放在函数的末尾,它现在可以工作了!