PHP联系表单有多个配方

时间:2015-09-01 14:03:42

标签: php forms contact

我有一个基于PHP的联系表单,它工作正常,但我需要它发送3或4个不同的电子邮件。我怎么能这样做?

这是我的代码:

<table class="table table-striped row-highlight table-condensed">
    <thead>
        <tr>
            <th class="shortcut-base"><a>Base Key</a></th>
            <th class="shortcut-binding"><a>Key Binding</a></th>
            <th class="shortcut-cmd-id"><a>Command ID</a></th>
            <th class="shortcut-cmd-name"><a>Command Name</a></th>
            <th class="shortcut-orig"><a>Origin</a></th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
    <head>
        <title>Contact us</title>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="style.css">
    </head>
    <body id="thankyou">
<?php
 
if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
 
    $email_to = "shovo654@gmail.com";
    $email_subject = "Your email subject line";
 

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
 
        echo "These errors appear below.<br /><br />";
 
        echo $error."<br /><br />";
 
        echo "Please go back and fix these errors.<br /><br />";
 
        die();
 
    }
 
    // validation expected data exists
 
    if(!isset($_POST['first_name']) ||
 
        !isset($_POST['last_name']) ||
 
        !isset($_POST['email']) ||
 
        !isset($_POST['telephone']) ||
 
        !isset($_POST['comments'])) {
 
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
 
    }
 
    $first_name = $_POST['first_name']; // required
 
    $last_name = $_POST['last_name']; // required
 
    $email_from = $_POST['email']; // required
 
    $telephone = $_POST['telephone']; // not required
 
    $comments = $_POST['comments']; // required
 
     
 
    $error_message = "";
 
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(!preg_match($string_exp,$first_name)) {
 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
 
  }
 
  if(!preg_match($string_exp,$last_name)) {
 
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
 
  }
 
  if(strlen($comments) < 2) {
 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
 
  }
 
  if(strlen($error_message) > 0) {
 
    died($error_message);
 
  }
 
    $email_message = "Form details below.\n\n";
 
     
 
    function clean_string($string) {
 
      $bad = array("content-type","bcc:","to:","cc:","href");
 
      return str_replace($bad,"",$string);
 
    }
 
     
 
    $email_message .= "First Name: ".clean_string($first_name)."\n";
 
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
 
    $email_message .= "Email: ".clean_string($email_from)."\n";
 
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
 
    $email_message .= "Comments: ".clean_string($comments)."\n";
 
     
 
 
// create email headers
 
$headers = 'From: '.$email_from."\r\n".
 
'Reply-To: '.$email_from."\r\n" .
 
'X-Mailer: PHP/' . phpversion();
 
@mail($email_to, $email_subject, $email_message, $headers);  
 
?>
 
 
 
<!-- include your own success html here -->

        <div class="tahnkyou_page">
            <div class="inner_thank">
                <p>Thank you for contacting us. We will be in touch with you very soon.</p>
                <a href="http://superseoranker.com/demo/fixed2/contactform.php">Home</a>
            </div>
        </div>
    </body>
</html>
 
<?php
 
}
 
?>

<div class="effect_form">
<form name="contactform" method="post" action="send_form_email.php">
<fieldset >
<div class='container fastname'>
    <input  type="text" name="first_name" maxlength="50" placeholder="Fast Name*">
</div>
<div class='container lastname'>
    <input  type="text" name="last_name" maxlength="50" placeholder="Last Name*">
</div>
<div class='container email'>
    <input  type="text" name="email" maxlength="80" placeholder="Email*">
</div>
<div class='container phone'>
    <input  type="text" name="telephone" maxlength="30" placeholder="Phone">
</div>

<div class='container message'>
    <textarea  name="comments" maxlength="1000" cols="25" rows="6" placeholder="Message*"></textarea>
</div>

                           <div class="effect_form_bottom">
                               <div class="eff_form_left">
                                   <p class="phone">An AGE technician will contact you within 24 hours with a free no obligation quote</p>
                                   <p class="lock">Your privacy is important, we do not provide information to third parties.</p>
                               </div>
                               <div class="eff_form_right">
                                   <input type='submit' name='Submit' value='Get Free Quote' />
                               </div>
                           </div>

</fieldset>
</form>
                    </div> 

那么需要做什么?我试过了,但是我无法使它发挥作用。

1 个答案:

答案 0 :(得分:0)

向多个收件人发送电子邮件:

// multiple recipients
$email_to  = 'aidan@example.com' . ', '; // note the comma
$email_to .= 'wez@example.com';
[...]
mail($email_to, $email_subject, $email_message, $headers);

来源:PHP.net