【问题标题】:Multiple php entries sent in single email在单个电子邮件中发送多个 php 条目
【发布时间】: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>

谢谢你, 埃里克

【问题讨论】:

  • 运行循环并将数据附加到单个字符串。
  • 如果您说每个条目发送一封邮件,那么您必须在某处已经有一个循环序列(例如forforeach)。您需要提供包含此循环的代码,我们才能找出错误所在。
  • 你是对的,它循环通过为每个条目添加一个新行。我已经提供了整个代码。

标签: php email


【解决方案1】:

检查一下:

$headers = "From:" . $fromemail."\r\n";
$headers .= 'To: mmmm@domain.com, nnn@domain.com' . "\r\n"; 
$headers .= 'Cc: test@example.com,blabla@domain.com' . "\r\n"; 
$headers .= 'Bcc: blablabla@domain.com,me@domain.com,you@domain.com,sam@domain.com' . "\r\n";

mail($submissionemail,$subject,$message,$headers);  

【讨论】:

  • 这看起来像是在尝试发送给多个人。如果我错了,请纠正我。这不是我要找的,我理解这方面。我想要一个包含多个条目(例如 entry1、entry2、entry3 ......)的表单在单个电子邮件中发送,而不是三个单独的电子邮件。这有意义吗?
  • 对不起 :) 有 1 种方法:在循环中执行 mail()
【解决方案2】:
<?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;

$message = "Text about this email and other stuff.\n\n";
$data = array();
date_default_timezone_set('America/New_York');
for( $i = 0; $i <= $count; $i++ )
{   
    $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];

    $data[] = "$today, $area0, $contractor0, $hours0, $project0, $town0, $street0, $from0, $to0, $construction0, $mpt0, $direction0, $police0 \n";

    $message .= "Date: $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.";
}

if(!empty($data)) {
    $data = implode('', $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";

    $headers = "From:" . $fromemail;
    mail($submissionemail,$subject,$message,$headers);

    $fh = fopen("data.csv", "a");
    fwrite($fh, $data);
    fclose($fh);
}

?>

【讨论】:

  • num8er!!!!!!据我所知,你是个天才!你这么快就解决了。非常感谢您惊人的快速和有用的帮助。感谢您的时间和耐心。
  • 很抱歉把这个备份起来。我刚刚注意到在第一次输入后下拉列表不再记录。文本字段仍然有效。有任何想法吗?你可以在这里查看我的测试站点。 www.selectbylocation.com.
  • 我看到当我添加项目时,我得到了具有相同索引的相同字段:
  • 我的意思是去掉方括号内的数字
  • 我在 javascript 代码中看到您不会为选择框迭代字段名称,所以修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
相关资源
最近更新 更多