根据php中的选定选项向多个收件人发送电子邮件?

时间:2014-03-09 04:54:29

标签: php html email

大家晚上好,

我目前正在尝试根据他们在发送前在电子邮件表单中选择的所选选项向相应的收件人发送电子邮件。例如,您打开表单,输入您的姓名等等。您会看到选项"我是客户,我是新闻界等等#34;我选择"我是媒体" ..现在应该发送到press@domain.com,这取决于他们的选择。

我不是最好的PHP,并且已经尝试了一段时间,我觉得我非常接近,但可以在最后的细节中关闭。我之前也看过一些以前的答案,我尽量做很少的工作,因为我知道这并不太难。有人会介意,告诉我我做错了什么,以免它发送?我觉得我已经定义了正确的事情,但不确定发生了什么。

这是我的PHP ..

<?php

// We use the name2 field as bait for spambots.  It's display is off,
// so humans wouldn't know to enter anything into it.  Robots would, 
//  so we ignore submissions with info in name2.

$mail_sent = false;

if(sizeof($_POST) && $_POST["name2"] == "") // receiving a submission
{   
    define("SUBJECT",   "Visitor Message from Website");

    // production recipient:
    define("RECIPIENT", ".$department.");

    // prep our data from the form info
    $senderName     = $_POST['name'];
    $senderEmail    = $_POST['email'];
    $department     = $_POST['department'];
    $subject        = SUBJECT; 
    $messageBody    = $senderName . ' ('.$senderEmail.') wrote in '.$department.':

' . $_POST['message'];

    if($department == 'customer') { //if customer was selected
    $to = 'customer@gmail.com';
    }

    else if($department == 'distribution') { //if distribution was selected
    $to = 'distribution@email.com';
    }

    else if($department == 'press') { //if press was selected
    $to = 'press@email.com';
    }

    else if($department == 'career') { //if career was selected
    $to = 'career@email.com';
    }

    else if($department == 'other') { //if other was selected
    $to = 'other@email.com';
    }

    // From 
    $header         = "from: $senderName <$senderEmail>";

    // To
    $to = RECIPIENT;

    // Send it!
    $send_contact = mail($to, $subject, $messageBody, $header);

    // Check success of send attempt
    if($send_contact){
        // show thankyou screen
        $mail_sent = true;
    }
    else {
        // send failed.
        echo "ERROR";
    }
}

?>

这是形式本身!

    <form action="contact-form.php" id="contactForm" method="post" accept-charset="utf-8">
        <input id="name" type="text" name="name" minlength="2" placeholder="Your Name&hellip;" required>
        <input id="email" type="email" name="email" placeholder="Your Email&hellip;" required>
        <input id="name2" type="text" name="name2" placeholder="Your Last Name&hellip;" required>
        <select id="department" type="text" name="department">
          <option value="customer" name="customer">I am a customer</option>
          <option value="distribution" name="distribution">department in distribution</option>
          <option value="press" name="press">I am with the press</option>
          <option value="career" name="career">department in a career position</option>
          <option value="other" name="other">Other</option>
        </select>
        <textarea name="message" placeholder="Your Message&hellip;" required></textarea>
        <input type="submit" value="Send away!">
    </form>

非常感谢任何帮助。祝每个人都过得愉快!

1 个答案:

答案 0 :(得分:1)

以下是有效的,但是这行if(sizeof($_POST) && $_POST["name2"] == "")在我的测试中无效。

我建议您将其更改为if(!empty($_POST) && $_POST["name2"] != "")(这是我的回答)。

这基本上是说,&#34;如果不是空的并且name2不等于什么,那么继续&#34;

<?php
// We use the name2 field as bait for spambots.  It's display is off,
// so humans wouldn't know to enter anything into it.  Robots would, 
//  so we ignore submissions with info in name2.

$mail_sent = false;

if(!empty($_POST) && $_POST["name2"] != ""){

 // receiving a submission
    $to = $_POST['department'];

    // prep our data from the form info
    $senderName     = $_POST['name'];
    $senderEmail    = $_POST['email'];
    $department     = $_POST['department'];
    $subject        = "Visitor Message from Website"; 
    $messageBody    = $senderName . ' ('.$senderEmail.') wrote in '.$department.': ' . $_POST['message'];

    if($department == 'customer') { //if customer was selected
    $to = 'customer@gmail.com';
    }

    else if($department == 'distribution') { //if distribution was selected
    $to = 'distribution@email.com';
    }

    else if($department == 'press') { //if press was selected
    $to = 'press@email.com';
    }

    else if($department == 'career') { //if career was selected
    $to = 'career@email.com';
    }

    else if($department == 'other') { //if other was selected
    $to = 'other@email.com';
    }

    // From 
    $header = "From: $senderName <$senderEmail>";

    // echo $to; // my testing purpose


    // Send it!

    $send_contact = mail($to, $subject, $messageBody, $header);

    if($send_contact){
        $mail_sent = true;
    }
    else {
        echo "ERROR";
    }


// end brace for if(!empty($_POST) && $_POST["name2"] != "")
 }

?>