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

根据php中选定的选项向多个收件人发送电子邮件?,php,html,email,Php,Html,Email,各位晚上好, 我目前正在尝试根据发送前在电子邮件表单上选择的选项向相应的收件人发送电子邮件。例如,打开表单,输入姓名等。。你可以看到“我是客户,我是媒体等”选项,我选择“我是媒体”。。它现在应该发送到press@domain.com这是基于他们的选择 我不是PHP的高手,我已经尝试了一段时间,我觉得我已经很接近了,但是我可以在最后的细节中接近。我也看了一些以前的答案,我正试图做尽可能少的返工,因为我知道这不是太难。有谁介意,告诉我我做错了什么,这样它就不会发送了?我觉得我定义了正确的事情,但不确

各位晚上好,

我目前正在尝试根据发送前在电子邮件表单上选择的选项向相应的收件人发送电子邮件。例如,打开表单,输入姓名等。。你可以看到“我是客户,我是媒体等”选项,我选择“我是媒体”。。它现在应该发送到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>

我是顾客
经销部
我是新闻界的
事业部
其他

任何帮助都将不胜感激。祝大家一周愉快

下面的代码可以工作,但是如果(sizeof($\u POST)和&$\u POST[“name2”]==”)在我的测试中不起作用,那么这行代码可以工作

如果(!empty($\u POST)和&$\u POST[“name2”!=”)(这在我的答案中),我建议您将其更改为

这实际上是说,“如果不是空的,并且name2不等于零,那么继续”



您遇到了什么错误?sizeof($\u POST)是否有效,应为$\u POST['name2']侧注:此
$subject=subject-
主题
需要用引号封装。我在质疑
define(“收件人”、“部门”)旁注#2:要使用
发送给多个收件人,您需要使用(基本语法)=>
,然后根据下面给出的答案进行回答。从技术上讲,我没有收到任何错误,而且“发送”很好,当我从“$department”中删除句点时。这是我所做的重复和错误,无法确定确切的错误。我要不要把我的话题从<代码>$subject=subject
$subject=“subject”?是,将其更改为
$subject=“subject”否则将出现解析错误@多比你可能是最棒的人,因为你在周末花时间在聊天室帮我。谢谢你,伙计。非常欢迎@DoPeT@I很高兴能帮上忙并找到解决办法。祝你一切顺利
<?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"] != "")
 }

?>