根据php中选择的选项发送电子邮件到多个收件人? [英] Send email to multiple recipients based on selected option in php?

查看:143
本文介绍了根据php中选择的选项发送电子邮件到多个收件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

傍晚大家,

我一直在尝试根据他们在电子邮件表单中选择的选项向相应收件人发送电子邮件发送。例如,您打开表单,输入您的姓名等等。您会看到选项我是客户,我是新闻等,我选择我是新闻..它现在应该发送到press@domain.com,这是基于他们的选择。

I've been currently trying to send an email to the according recipient based on the selected option they've chose on the email form before sending. For example, you open the form, type your name in and so on.. And you see the option "I'm a customer, I'm the press, etc" and I choose "I'm the press".. It should now send to press@domain.com and that's it based on their selection.

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

I'm not the best with PHP and been trying this for awhile and I feel like I'm pretty close but can close in the last very details. I've also looked at some previous answers and I'm trying to do as little reworking as possible since I know this isn't too tough. Would anyone mind, tell me what I'm doing wrong so that it's not sending? I've felt like I've defined the right things, but not sure what's happening.

这是我的PHP ..

Here's my 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>

任何帮助都非常感激。

Any help is kindly appreciated. Have a great week everyone!

推荐答案

以下工作,但是这行 if(sizeof($ _ POST )&& $ _POST [name2] ==)在我的测试中没有工作。

The following works, however this line if(sizeof($_POST) && $_POST["name2"] == "") did not work in my tests.

我建议你改变如果(!empty($ _ POST)&& $ _POST [name2]!=)(这在我的回答中)

I suggest you change it to if(!empty($_POST) && $_POST["name2"] != "") (which is in my answer).

这基本上说的是,如果不是空,而且name2不等于任何东西,然后继续

What this essentially says is, "if not empty AND name2 does not equal nothing, then proceed"

<?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"] != "")
 }

?>

这篇关于根据php中选择的选项发送电子邮件到多个收件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆