PHP email address - want to send different emails

Currently:

I have a PHP form below where users select one of the checkboxes where a notification comes from where they want to book.

Purpose:

I wish now that when someone clicks on Canterbury, an email is sent to email at 1@gmail.com , when to Broadstairs, email2@gmail.com , and when it comes to email3 @ gmail. com

I'm going in circles trying to make a change, but I ended up in a mess.

FYI I don't mind changing the checkboxes in the dropdown, but I'd rather keep it that way.

Current PHP section:

<?php
ini_set('display_errors',0);
error_reporting(E_ALL);
$error = '';

if(isset($_POST['send']))
{
        $name = $_REQUEST['Name'];
        $telephone = $_REQUEST['Telephone'];
        $email = $_REQUEST['Email'];
        $message = $_REQUEST['Message'];
        $headers = "From: $email";
        $subject = "Web Contact Data";
        $selectedProjects  = 'None';
            if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
                $selectedProjects = implode(', ', $_POST['projects']);
            }

        $fields = array();
        $fields["Name"] = $name;
        $fields["Telephone"] = $telephone;
        $fields["Email"] = $email;
        $fields["Message"] = $message;
        $fields["Location"] = $selectedProjects;

         $to = "email4@gmail.com" ; // change all the following to $_POST

        $body = "You have recieved the following information:\n\n";
        foreach($fields as $key => $value)
        {
            $body .= sprintf("%20s: %s\n",$key,$value);
        } // end foreach($fields as $key => $value)

        $subject2 = "Thank you for contacting us.";
        $autoreply = "<html><body><p>Dear " . $name . ",</p><p>Message goes here.</p></body></html>"; 
        $headers2  = 'MIME-Version: 1.0' . "\r\n";
        $headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers2 .= 'From: email4@gmail.com' . "\r\n";

        $send=false;
        if($name == '')
        {
            $error = "You did not enter your name, please try again.";
            selected_values();
        }
        $send=false;
        if($telephone == '')
        {
            $error = "You did not enter your telephone number, please try again.";
            selected_values();
        }
        elseif(!preg_match("/^[[:alnum:]][a-z0-9_.'+-]*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$/", $email))
        {
            $error = "You did not enter a valid email address, please try again.";
            selected_values();
        }
        else
        {
            $send = mail($to, $subject, $body, $headers);
            $send2 = mail($email, $subject2, $autoreply, $headers2);

            if(!isset($error) && !$send)
            {
                $error = "We have encountered an error sending your mail, please notify [email]email4@gmail.com[/email].";
            }
            else
            {
                unset($_REQUEST['Name']);
                unset($_REQUEST['Email']);
                unset($_REQUEST['Telephone']);
                unset($_REQUEST['Message']);
            }
    } // end else
}// end of if(isset($_POST['send']))
?>

      

And the HTML form is like this:

<form method="post" action="./lead.php">
<ul>
    <li>
        <?php
        if(isset($error))
            echo '<div class="register_error">'.$error.'</div>';
        if(isset($send) &&  $send== true){
            =echo '<div class="contact-send-green">Thank you for your message.</div>';
        }
        if(!isset($_POST['send']) || isset($error))
        ?>
    </li>
    <li>
        <input type="checkbox" name="projects[]" value="Broadstairs">
        <label for="type2">Broadstairs</label>
        <br /><br />
        <input type="checkbox" name="projects[]" value="Canterbury">
        <label for="type2">Canterbury</label>
        <br /><br />
        <input type="checkbox" name="projects[]" value="Deal">
        <label for="type2">Deal</label>
    </li>
    <li>
        <textarea name="Message" rows="5" cols="29"><?php if(isset($_REQUEST['Message'])) echo $_REQUEST['Message']; ?></textarea>
    </li>
    <li>
        <input type="submit" name="send" value="Send Message" id="send-email">
    </li>
</ul>

      

+3


source to share


3 answers


There are 3 problems with the script you provided

  • You only want to select one email address, however you use the name projects[]

    as the checkbox names - this will create an array, however with HTML5; if you use the name multiple times, even without it []

    , it will create an array.
  • Given the above, you should definitely define how you mentioned the change to the field, <select>

    or alternatively the buttons <radio>

    .
  • You should not use $_REQUEST

    unless absolutely necessary, you provide light oppurtunity for SQL injection.

Here is an example of how I would do it.

Html

<form method="post" action="./lead.php">
<ul>
    <li>
        <?php
            if(isset($error)) echo '<div class="register_error">'.$error.'</div>';
            if(isset($send) &&  $send== true) echo '<div class="contact-send-green">Thank you for your message.</div>';
        ?>
    </li>
    <li>
        <label for="Broadstairs"><input type="radio" name="projects" id='Broadstairs' value="Broadstairs"> Broadstairs</label>
        <label for="Canterbury"><input type="radio" name="projects" id='Canterbury' value="Canterbury">Canterbury</label>
        <label for="Deal"><input type="radio" name="projects" id='Deal' value="Deal">Deal</label>
    </li>
    <li>
        <textarea name="Message" rows="5" cols="29"><?=(strlen($_POST['Message'])>0) ? $_POST['Message'] : ''; ?></textarea>
    </li>
    <li>
        <button type='submit'>Send Message</button>
    </li>
</ul>

      



And your PHP

<?php

if (!empty($_POST)) {
    // Dispatch message to selected office
    $name       = $_POST['Name'];
    $telephone  = $_POST['Telephone'];
    $email      = $_POST['Email'];
    $message    = $_POST['Message'];
    $subject    = "Web Contact Data";

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: My website <my-email@example.com>' . "\r\n";

    if ($_POST['projects'] == 'Canterbury') $to = "example@gmail.com" ;
    elseif ($_POST['projects'] == 'Broadstairs') $to = "example@gmail.com" ; 
    else $to = "example@gmail.com" ;

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


    // Dispatch "Thank You" email
    $subject = "Your message has been received!";

ob_start();
?>
<html>
<body>
<p>Dear <?=$name?></p>
<p>Thank you for contacting us,</p>
<p>We have received your message and will respond to you as soon as possible.</p>
<p>This is an automatic response to your inquiry and it is not necessary to reply.</p>
<p>Thank you,</p>
</body>
</html>
<?
$message = ob_get_flush();

    mail($email, $subject, $message, $headers)
}

?>

      

I would recommend Google Recaptcha .

Hope it helps

+3


source


For a single selection, a simple switch () {} will do the magic.

switch($_POST['projects']){
   case "Broadstairs":
      $to = "email@email.com";
      break;
   case "Canterbury":
      $to = "email@email.com";
      break;
   case "Deal":
      $to = "email@email.com";
      break;
}

      

And then remove the [] from the input names, since you don't need an array of them. (Thus:)



<input type="checkbox" name="projects" value="Broadstairs">

      

And then maybe considering using JS / jQuery to check that only 1 is being selected at a time?

+4


source


You need to add a condition if

, elseif

and else

to your code.

Replace with the $to = "email1@email.com" ;

following code

if($_POST['projects'] == 'Broadstairs')
{
    $to = "email2@email.com";
}
else if($_POST['projects'] == 'Canterbury')
{
$to = "email3@email.com";
}
else if($_POST['projects'] == 'Deal')
{
    $to = "email4@email.com";
}
else
{
    $to = "email5@email.com";
}

      

0


source







All Articles