【发布时间】:2014-06-05 12:06:12
【问题描述】:
我目前有一个表单,允许使用 here 找到的修改代码一次提交多个条目。
一切都很好。这些条目被放入一个 csv 表中。我对此非常满意。
提交后,这些条目将通过电子邮件发送出去。问题是每个条目都在单独的电子邮件中发送,这并不理想。我正在寻找一种方法,将来自单个提交按钮的所有条目分组到要发送的单个电子邮件中。
有人知道怎么做吗?基本上是单个 mail() 形式的多个消息/条目。
我当前的邮件代码如下:
<?php
$area = $_POST['area'];
$contractor = $_POST['contractor'];
$hours = $_POST['hours'];
$project = $_POST['project'];
$town = $_POST['town'];
$street = $_POST['street'];
$from = $_POST['from'];
$to = $_POST['to'];
$construction = $_POST['construction'];
$mpt = $_POST['mpt'];
$direction = $_POST['direction'];
$police = $_POST['police'];
$submissionemail = $_POST['submissionemail'];
$count = count($area)-1;
//open the file and choose the mode
$fh = fopen("data.csv", "a");
for( $i = 0; $i <= $count; $i++ )
{
date_default_timezone_set('America/New_York');
$today = date("F j - Y - g:i a");
$area0 = $area[$i];
$contractor0 = $contractor[$i];
$hours0 = $hours[$i];
$project0 = $project[$i];
$town0 = $town[$i];
$street0 = $street[$i];
$from0 = $from[$i];
$to0 = $to[$i];
$construction0 = $construction[$i];
$mpt0 = $mpt[$i];
$direction0 = $direction[$i];
$police0 = $police[$i];
//the data
$data = "$today, $area0, $contractor0, $hours0, $project0, $town0, $street0, $from0, $to0, $construction0, $mpt0, $direction0, $police0 \n";
fwrite($fh, $data);
$toemail = "email@domain.com"; // Hard code emails, must change below
$fromemail = "email@domain.com"; // this is the sender's Email address
$subject = "Form submission";
$message = "Text about this email and other stuff.\n\nDate: $today\nArea: $area0\nContractor: $contractor0\nHours: $hours0\nProject: $project0\nTown: $town0\nStreet: $street0\nFrom: $from0\nTo: $to0\nConstruction Activity: $construction0\nMPT: $mpt0\nClosure: $direction0\nPolice: $police0\n\n Thank you.";
$headers = "From:" . $fromemail;
mail($submissionemail,$subject,$message,$headers);
}
fclose($fh);
?>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FORM</title>
<link rel="stylesheet" href="css/foundation.css" />
<link rel="stylesheet" href="css/foundation.min.css" />
<script src="js/vendor/modernizr.js"></script>
</head>
<body>
<div class="spacer"></div>
<div class="row">
<div class="large-12 columns">
<div class="columns formarea">
<br> <br>
<p>Thank you. </p>
<p>Your form has been submited and will be email to you for reference. If you have any updates please submit another form.</p>
<p>Text with information about the project and links to relvant materials.</p>
<p>Please contact ___ with any questions at email@___.com</p>
<a href="index.html" class="button [tiny small large]">Back to form</a>
</div>
</div>
</div>
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
</body>
</html>
谢谢你, 埃里克
【问题讨论】:
-
运行循环并将数据附加到单个字符串。
-
如果您说每个条目发送一封邮件,那么您必须在某处已经有一个循环序列(例如
for或foreach)。您需要提供包含此循环的代码,我们才能找出错误所在。 -
你是对的,它循环通过为每个条目添加一个新行。我已经提供了整个代码。