drox90250557 2015-09-01 14:03
浏览 43
已采纳

PHP联系表单与多个收件人[关闭]

I have a PHP-based contact-form, and it works fine, but I need it to send 3 or 4 different emails. How can I do that?

Here is my code:

<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.

";
 
     
 
    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)."
";
 
    $email_message .= "Last Name: ".clean_string($last_name)."
";
 
    $email_message .= "Email: ".clean_string($email_from)."
";
 
    $email_message .= "Telephone: ".clean_string($telephone)."
";
 
    $email_message .= "Comments: ".clean_string($comments)."
";
 
     
 
 
// create email headers
 
$headers = 'From: '.$email_from."
".
 
'Reply-To: '.$email_from."
" .
 
'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> 

So what needs to be done? I've tried, but I can't make it work.

</div>
  • 写回答

1条回答 默认 最新

  • drrrdo0802 2015-09-01 14:15
    关注

    To send an email to multiple recipients :

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

    Source: PHP.net

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算