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

时间:2015-09-09 03:55:01

标签: php forms email

我有一个包含简单复选框列表的表单。我需要通过电子邮件发送给所有那些基于已检查的框的表格。下面是我意识到代码已经关闭的基本逻辑位。

class Article < ActiveRecord:Base

extend FriendlyId
friendly_id :title, use: :slugged

def normalize_friendly_id(string)
  string
end

所以我们的想法是,如果服务复选框包含 Electrical ,那么电子邮件变量将包含$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"; } .';

如果服务复选框包含 Electrical AND Plumbing ,则电子邮件变量将包含$email_to = 'david@gmail.com,roger@gmail.com'

任何帮助都将不胜感激。

这是我在收到你的建议后试图实施的实际代码 - 但仍然没有工作 - 我试过了三个建议?

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

这是形式......

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

?>

4 个答案:

答案 0 :(得分:4)

$ _ 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";
    }

.=将第二个地址附加到第一个地址

答案 1 :(得分:0)

你可以使用一个数组。

$emails = array();

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

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

当要发送电子邮件时,我只是迭代数组。

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

答案 2 :(得分:0)

在使用前添加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";
    }
}

答案 3 :(得分:0)

$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";
    }