简体   繁体   中英

Multiple php entries sent in single email

I currently have a form that will allow for multiple entries with a single submit using modfied code found here .

Everything is working great. The entries are put into a csv table. I am very happy with it.

Upon submit these entries are sent out by email. The problem is that each entry is sent in a separate email which is not ideal. I am looking for a way to have all entries from a single submit button to be grouped into a single email to be sent.

Does anyone know how to do this? Essentially multiple messages / entries in single mail() form.

My current mail code looks like:

<?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>

Thank you, Eric

Check this:

$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);  
<?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);
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM