简体   繁体   English

将电子邮件地址添加到php $ email_to变量中

[英]Add email address to php $email_to variable

I have a form that has a simple check box list. 我有一个带有简单复选框列表的表单。 I need the form sent via email to all those based on the boxes that have been checked. 我需要根据已选中的框通过电子邮件发送给所有表单的表单。 Below is the basic logic bit I realize the code is way off. 下面是我认识到代码尚待解决的基本逻辑位。

$email_to = 'david@gmail.com,'.'

    if ($_POST['services'] == 'Electrical'){
        echo ",roger@gmail.com";
    }

    if ($_POST['services'] == 'Plumbing'){
        echo ",bill@gmail.com";
    }

    if ($_POST['services'] == 'Painting'){
        echo ",donnie@gmail.com";
    }
.';

So the idea is that if the services check box includes Electrical , then the email variable will include $email_to = 'david@gmail.com,roger@gmail.com' 因此,想法是,如果服务复选框包含Electrical ,则电子邮件变量将包含$email_to = 'david@gmail.com,roger@gmail.com'

And if the services check box includes Electrical AND Plumbing , then the email variable will include $email_to = 'david@gmail.com, roger@gmail.com, bill@gmail.com' 并且,如果“ 服务”复选框包含“ 电气和管道” ,则电子邮件变量将包含$email_to = 'david@gmail.com, roger@gmail.com, bill@gmail.com'

Any help would be appreciated. 任何帮助,将不胜感激。

HERE IS THE ACTUAL CODE THAT I TRIED TO IMPLEMENT AFTER RECEIVING YOUR SUGGESTIONS - BUT STILL DOESNT WORK - AND I TRIED ALL THREE SUGGESTIONS? 收到您的建议后,我尝试执行的实际代码是-仍然无效-我尝试了所有三个建议吗?

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
if(isset($_POST['email'])) {

$email_from = $_POST["email"];
$email_to = 'nettemple@gmail.com,{$email_from}';
    if($_POST['services'] == "Ecomtek"){ 
        $email_to .= ",sol@sungazer.com"; 
        }
    if($_POST['services'] == "E-Payroll"){ 
        $email_to .= ",sarah@nettemple.net"; 
        }
    if($_POST['services'] == "Humana"){ 
        $email_to .= ",dmeyers@scad.edu";
        }

$email_subject = "WEB INQUIRY";
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

// 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); 

?>

THIS IS THE FORM.... 这是表格...

<form name="htmlform" method="post" action="form_process2.php">

<input type="checkbox" name="services[]" value="Ecomtek" />&nbsp;Ecomtek<br />
<input type="checkbox" name="services[]" value="E-Payroll" />&nbsp;E-Payroll<br />
<input type="checkbox" name="services[]" value="Humana" />&nbsp;Humana<br />
<input type="checkbox" name="services[]" value="John Conti" />&nbsp;John Conti<br />
<input type="checkbox" name="services[]" value="Kentucky RX Card" />&nbsp;Kentucky RX Card<br />
<input type="checkbox" name="services[]" value="Lifelock" />&nbsp;Lifelock<br />
<input type="checkbox" name="services[]" value="Logans" />&nbsp;Logan's<br />
<input type="checkbox" name="services[]" value="Norton Healthcare" />&nbsp;Norton Healthcare<br />
<input type="checkbox" name="services[]" value="Office Depot" />&nbsp;Office Depot<br />
<input type="checkbox" name="services[]" value="ProWaste" />&nbsp;ProWaste<br />
<input type="checkbox" name="services[]" value="Thorntons" />&nbsp;Thorntons<br />


  <label for="firstname">*FirstName</label>
  <input  type="text" name="firstname" maxlength="150" size="50" class="formbox">

  <label for="lastname">*Last Name</label>
  <input  type="text" name="lastname" maxlength="150" size="50" class="formbox">

  <label for="company">*Company</label>
  <input  type="text" name="company" maxlength="150" size="50" class="form box">

  <label for="email">*Email Address</label>
  <input  type="text" name="email" maxlength="180" size="50" class="form box">

</form>

$_POST['services'] is an array so: $ _POST ['services']是一个数组,因此:

$email_to = 'david@gmail.com';

    if (in_array('Electrical',$_POST['services'])){
        $email_to .= ",roger@gmail.com";
    }

    if (in_array('Plumbing',$_POST['services'])){
        $email_to .= ",bill@gmail.com";
    }

    if (in_array('Painting',$_POST['services'])){
        $email_to .= ",donnie@gmail.com";
    }

.= appends the 2nd address to the first .=将第二个地址附加到第一个

You could use an array for this. 您可以为此使用数组。

$emails = array();

if($_POST['services'] == 'Plumbing') {
    $emails[] = 'user@gmail.com';
}

if($_POST['services'] == 'Painting') {
    $emails[] = 'user@gmail.com';
}

When going to send the email, I would just iterate the array. 在发送电子邮件时,我只需要迭代数组即可。

foreach( $emails as $email )
    mail($email, 'Subject', ....);

Added isset($_POST['services']) to validate variable before use, and for appending the string you can use .= 添加了isset($_POST['services'])以在使用前验证变量,并且可以使用.=附加字符串.=

$email_to = 'david@gmail.com';
if(isset($_POST['services']){
    if ($_POST['services'] == 'Electrical'){
        $email_to .= ",roger@gmail.com";
    }
    if($_POST['services'] == 'Plumbing'{
        $email_to .= ",bill@gmail.com";
    }
    if($_POST['services'] == 'Painting'){
        $email_to .= ",donnie@gmail.com";
    }
}
$email_to = 'david@gmail.com';

    if (in_array('Electrical',$_POST['services'])){
        $email_to .= ",roger@gmail.com";
    }

    if (in_array('Plumbing',$_POST['services'])){
        $email_to .= ",bill@gmail.com";
    }

    if (in_array('Painting',$_POST['services'])){
        $email_to .= ",donnie@gmail.com";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM