Wallyl81
Wallyl81

Reputation: 29

Send emails to different addresses based on drop-menu selection

I have a very basic skills, but this shouldn't be too hard. I have a very basic html form:

                <div class="form-input">
                    <div class="form-title">LOCATION</div>
                    <select id="form-select">
                        <option>GENERAL ENQUIRY</option>
                        <option>SHOP 1</option>
                        <option>SHOP 2</option>
                        <option>SHOP 3</option>
                        <option>SHOP 4</option>
                        <option>SHOP 5</option>
                        <div class="down-box"><i class="typcn typcn-chevron-down"></i></div>
                    </select>

                </div>

                <div class="form-input">
                    <div class="form-title">NAME</div>
                    <input id="form-name" required type="text">
                </div>

                <div class="form-input">
                    <div class="form-title">EMAIL</div>
                    <input id="form-email" required type="text">
                </div>

                <div class="form-input">
                    <div class="form-title">TELEPHONE</div>
                    <input id="form-telephone" required type="text">
                </div>

                <div class="form-input">
                    <div class="form-title">MESSAGE</div>
                    <textarea id="form-msg" type="text"></textarea>
                </div>

                <div class="form-input">
                    <div class="form-title">&nbsp;</div>
                    <button id="form-send">SEND</button>
                </div>

            </div><!--end of form holder-->

and a very basic mail.php to send email from the form:

    <?php
if ($_POST) {
    $name  = $_POST['name'];
    $email = $_POST['email'];
    $telephone = $_POST['telephone'];
    $text  = $_POST['text'];
    $option = $_POST['option'];
    $headers = $option . "\r\n" . $name . "\r\n" . $email . "\r\n" . $telephone;


    //send email   
    mail("[email protected]", "Mail Enquiry", $text, $headers);
}

?>

Now, I want to modify this php to send the email to different addresses based on the selection from the drop menu. How do I achieve this? (Eg. if a user picks SHOP1 email goes to shop1@email..., SHOP2 email to shop2@email.. etc.. etc..)

Thank you to everyone in advance!

Upvotes: 1

Views: 107

Answers (2)

Arne
Arne

Reputation: 1135

First of all, all your inputs, selects, and textareas have no name attribute, so no values will be sent from them to your $_POST[x];.


HTML:

As @Syscall said in a comment, the name and values in your select are missing. Here's an example of what it should look like:

<select id="form-select" name="sendTo">
  <option value="general-enquiry">GENERAL ENQUIRY</option>
  <option value="shop-1">SHOP 1</option>
  <option value="shop-2">SHOP 2</option>
  [...]
</select>

I highly recommend not to work with e-mail addresses in the values, otherwise it can be processed quickly and abused for spaming or other things.


PHP:

In PHP you can then work with an array, a if or a switch to send the message to the desired e-mail address.

As an example here with a switch:

switch ($_POST['sendTo']) {
  case 'shop-1':
    $sendTo = '[email protected]';
    break;

  case 'shop-2':
    $sendTo = '[email protected]';
    break;

  [...]

  default:
    $sendTo = '[email protected]';
    break;
}

mail($sendTo, 'Mail Enquiry', $text, $headers);

I hope that could help :)


Update #1

On request, once the entire necessary code.

HTML:

<form method="post">

  <input type="hidden" name="sendMail" value="1" />

  <label for="sendTo">Location</label>
  <select id="sendTo" name="sendTo">
    <option value="general-enquiry">General Enquiry</option>
    <option value="shop-1">Shop 1</option>
    <option value="shop-2">Shop 2</option>
    <option value="shop-3">Shop 3</option>
    <option value="shop-4">Shop 4</option>
    <option value="shop-5">Shop 5</option>
  </select>

  <label for="name">Name <em>(required)</em></label>
  <input id="name" type="text" name="name" placeholder="Enter your full name ..." required />

  <label for="email">Email <em>(required)</em></label>
  <input id="email" type="text" name="email" placeholder="Enter your best email ..." required />

  <label for="telephone">Telephone <em>(required)</em></label>
  <input id="telephone" type="text" name="telephone" placeholder="Enter your phone number ..." required />

  <label for="message">Message</label>
  <textarea id="message" name="message" placeholder="If you have a message for us or one of our shops, please enter them here ..."></textarea>

  <input type="submit">Send</input>

</form>

PHP:

if ($_POST['sendMail']) {
  if (empty($_POST['name'])) {
    return 'Please enter your full name!';
  }

  if (empty($_POST['email'])) {
    return 'Please enter your email!';
  }

  if (empty($_POST['telephone'])) {
    return 'Please enter your phone number!';
  }

  $subject   = 'Mail Enquiry';
  $name      = $_POST['name'];
  $email     = $_POST['email'];
  $telephone = $_POST['telephone'];

  $message .= 'Name: ' . $name . "\r\n";
  $message .= 'Email: ' . $email . "\r\n";
  $message .= 'Telephone: ' . $telephone . "\r\n";

  if ($_POST['message']) {
    $message .= "\r\n";
    $message .= 'Message: ' . "\r\n";
    $message .= $_POST['message'];
  }

  switch ($_POST['sendTo']) {
    case 'shop-1':
      $sendTo = '[email protected]';

      break;

    case 'shop-2':
      $sendTo = '[email protected]';

      break;

    case 'shop-3':
      $sendTo = '[email protected]';

      break;

    case 'shop-4':
      $sendTo = '[email protected]';

      break;

    case 'shop-5':
      $sendTo = '[email protected]';

      break;

    default:
      $sendTo = '[email protected]';

      break;
  }

  $header .= 'To: ' . $sendTo . "\r\n";
  $header .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";

  if ($sendTo && $subject && $message && $header) {
    mail($sendTo, $subject, $message, $header);
  }
}

Upvotes: 2

Ben Dubuisson
Ben Dubuisson

Reputation: 757

  1. You need to set a name to your inputs, so <input id="form-name" required type="text"> becomes <input id="form-name" name="name" required type="text"> if you want to access it through $_POST['name']

  2. You need values for your select.

    <select id="form-select" name="shop-select">
      <option>GENERAL ENQUIRY</option>
      <option value="shop1">SHOP 1</option>
      <option value="shop2">SHOP 2</option>
      <option value="shop3">SHOP 3</option>
      <option value="shop4">SHOP 4</option>
      <option value="shop5">SHOP 5</option>
    <div class="down-box"><i class="typcn typcn-chevron-down"></i>
    

AND

    <?php
    if ($_POST) {
        $name  = $_POST['name'];
        $email = $_POST['email'];
        $telephone = $_POST['telephone'];
        $text  = $_POST['text'];
        $option = $_POST['option'];
        $shop = $_POST['shop-select']
        $headers = $option . "\r\n" . $name . "\r\n" . $email . "\r\n" . 
        $telephone;

       //send email   
       mail(getEmail($shop), "Mail Enquiry", $text, $headers);
}

function getEmail($shop)  
{
    //an array of shops name and email that you get from somewhere
    // store it in database or as a constant somewhere ? or you can declare it here...
    $shops = [
        "shop1" => "someemail@email",
        // etc
    ];

    if (isset($shops[$shop]) && filter_var($shops[$shop], FILTER_VALIDATE_EMAIL)) {
       return $shops[$shop];
    }
        return "[email protected]";
}

?>

NEXT you'd want to do some input sanitization, and if your project is big enough, probably use a framework so all that stuff is already done for you...

Upvotes: 1

Related Questions