-2

I have an HTML form where you can enter your email and a message. After you submit this form i would like an email to be sent to my email (fixed) and to the email that is filled in trough the form.

I got my email to work but i can't seem to get the email from my HTML form into the send list in PHP. Any suggestions?

Code (html):

<form id="contact-form" method="post" action="js/contact.php" role="form">
  <div class="messages"></div>
  <div class="controls">
    <div class="row">
      <div class="row">
    <div class="col-md-6">
      <div class="form-group">
        <label for="form_email">Email</label>
        <input id="form_email" type="email" name="email" class="form-control" placeholder="Vul hier je email adres in" required="required" data-error="Valid email is required.">
        <div class="help-block with-errors"></div>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <div class="form-group">
        <label for="form_message">message</label>
        <textarea id="form_message" name="message" class="form-control" placeholder="Vul hier een eventuele opmerking of message in." rows="4" required="required" data-error="Please,leave us a message."></textarea>
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-md-12">
      <input type="submit" class="btn btn-success btn-send" value="Versturen">
    </div>
  </div>
</div>
</form>

Code PHP:

<?php

$from = 'info@mywebsite';

$sendTo = '[email protected]'; //Here the email from the form should be   added

$subject = 'Nieuwe reservering';

$fields = array('kosten' => 'kosten' , 'name' => 'Naam', 'surname' => 'Achternaam', 'phone' => 'Telefoonnummer', 'kamer' => 'Kamer', 'aankomst' => 'aankomst', 'vertrek' => 'vertrek', 'email' => 'Email', 'message' => 'Bericht');

$okMessage = 'Je bericht is verzonden!';

$errorMessage = 'Oei er ging iets fout, geeft niks. Probeer het later opnieuw.';

 error_reporting(E_ALL & ~E_NOTICE);

try {

    if(count($_POST) == 0) throw new \Exception('Form is empty');
    $emailText ="<table>";

    $emailText .="<p>Bedankt voor uw reservering, hieronder de ingevulde info:</p><br><br>";
    foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .= "<tr><td>{$fields[$key]}</td>";
            $emailText .= "<td>$value</td></tr>";
        }
    }
    $emailText .="<br><br><p>Voor vragen of wijzigingen mail naar: info@bmyemail</p>";

    $emailText .="</tr></table>";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers = array('Content-type:text/html;charset=UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
        );

    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e){
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&         strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
     $encoded = json_encode($responseArray);

     header('Content-Type: application/json');

     echo $encoded;
 } else {
     echo $responseArray['message'];
 }
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

2 Answers2

-1

After your email is done, you just need to send it twice :

<?php
    mail($addr1, $subject, $message, $header);
    mail($addr2, $subject, $message, $header);
?>

You can also concatenate differents addresses (I've managed to send a mail to different addresses by changing my input to a textarea and writing one address per line).

Good luck!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Chocorean
  • 807
  • 1
  • 8
  • 25
-1

You can specify all recipients like this: (check official guideline here)

$sendTo = '[email protected], [email protected], [email protected]';  // note the comma
//actual message
$message = "Message Content";

mail($sendTo, $subject, $message));

Hope it helps! :)

Atlas_Gondal
  • 2,512
  • 2
  • 15
  • 25
  • Thanks! But if i for example want to chance abc@example for the email that is filled in trough the form how would I do that? – Flexingipad Aug 02 '18 at 12:48
  • you can create an array or concatenate multiple recipients like this: `$to = $fist_user_email . ", " . $second_user_email . ", " . $third_user_email; ` – Atlas_Gondal Aug 02 '18 at 12:51