-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 = 'myemail@gmail.com'; //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
  • 1
    I can't see where you're executing the mailer here. – Funk Forty Niner Aug 02 '18 at 12:34
  • 2
    $sendTo = 'myemail@gmail.com,anotheremail@gmail.com,another@gmail.com'; – suresh Aug 02 '18 at 12:35
  • @suresh, that works but i need the email from the form to be filled in there not a fixed one – Flexingipad Aug 02 '18 at 12:36
  • means you need to enter multiple mails from html page and get it from db? – suresh Aug 02 '18 at 12:36
  • I think what @FunkFortyNiner means is there is no call to `mail()` so no mail is ever sent – RiggsFolly Aug 02 '18 at 12:37
  • @FunkFortyNiner I could post the complete PHP file but that is just how the email is structured. It's just about the sendings list – Flexingipad Aug 02 '18 at 12:37
  • you'll need an additional array then. – Funk Forty Niner Aug 02 '18 at 12:38
  • its better you should post your full php code so by that we can help you. – suresh Aug 02 '18 at 12:39
  • I think what you are asking is [How do I process a HTML form in PHP](http://php.net/manual/en/tutorial.forms.php) so there is a link to the manual. SO is not a tutorial site. And we dont write code for you, we only help you fix code – RiggsFolly Aug 02 '18 at 12:39
  • Duplicate post: https://stackoverflow.com/questions/12708997/php-form-send-email-to-multiple-recipients – kiran malvi Aug 02 '18 at 12:40
  • @RiggsFolly I'm aware of that. But does printing data from the form works for an email form? Because i don't want to echo it but use it in my send list – Flexingipad Aug 02 '18 at 12:45
  • @kiranmalvi I'm not looking to add more email adresses, just the one from the form. I know I can add extra email adresses like that – Flexingipad Aug 02 '18 at 12:46
  • **Is this for real** Did you try a simple `$sendTo = $_POST['email']` ... After some sanity checking on the contents of the $_POST variable of course – RiggsFolly Aug 02 '18 at 12:49
  • @RiggsFolly $sendTo = $_POST['email'] seems to work. Now i've tried to add my own email to the $sendto but that doesn't work. Is this code invalid? $sendTo = 'myemail@gmail.com', $_POST['email']; – Flexingipad Aug 02 '18 at 13:01
  • Best answer I can give is, find out for yourself by looking for errors. Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Aug 02 '18 at 13:02
  • @RiggsFolly Okay thanks for the help, i'll try and see what i can find. – Flexingipad Aug 02 '18 at 13:05

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 = 'myemail@gmail.com, abc@example.com, xyz@example.com';  // 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