PHP如果值,那么其他电子邮件?

时间:2013-04-11 15:30:17

标签: php email if-statement header carbon-copy

我的活动页面有一个PHP表单。现在它完美地通过电子邮件发送给我自己,我可以将它直接发送给我喜欢的任何人。但是,如果从我表单上的投递箱中选择某个特定事件,我只想将其发送给特定的人,然后将其发送给我。这样,举办活动的组织可以收到已完成表格的副本。如果可能的话,我还想向发送给它的人发送定制的消息。例如:

如果填写表格的人选择了“Heart Walk”活动,那么表格结果应该发送到“Bob”“bob@email.com”,上面写着“嗨Bob,你收到了一个新的心脏行走活动志愿者。下面是注册的副本。“

当然,我得到一份副本。

我可以为$ sendtoemail命令执行if / then语句或数组吗?然后通过以下内容将其复制给我:

$headers .= "Cc: me@myemail.com\r\n";

最好的方法是什么?谢谢你的帮助! :)

编辑:

这是我的代码:

<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}

$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

//send an email from the server TO YOUR EMAIL
$fromname = $_REQUEST['firstname'];
$fromemail = $_REQUEST['email'];
$subject = "NMVN EVENT REGISTRATION";

$message = " <b>EVENT:</b> ".$_REQUEST['pickevent'];
$message .= " <br><br> <b>First Name:</b> ".$_REQUEST['firstname'];
$message .= " <br> <b>Last Name:</b> ".$_REQUEST['lastname'];
$message .= " <br> <b>Cell Phone:</b> ".$_REQUEST['cellphone'];
$message .= " <br> <b>Alternative Phone:</b> ".$_REQUEST['altphone'];

//This is the person who is going to receive the email
$sendtoname = "Mike Gandy";
$sendtoemail = "me@myemail.com";

//Email header stuff
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromname <$fromemail>\r\n";
$headers .= "To: $sendtoname <$sendtoemail>\r\n";
$headers .= "Reply-To: $fromname <$fromemail>\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: Created by Mike Gandy";

//this next line creates and sends the email
mail($sendtoemail, $subject, $message, $headers);

?>

我在考虑更换它:

$sendtoname = "Mike Gandy";
$sendtoemail = "me@myemail.com";

有了这个:

if ($_REQUEST['pickevent'] == 'HeartWalk'))
//if "HeartWalk" is filled out, send to email
  {
  //send email
  $sendtoname = "Bob";
  $sendtoemail = "bob@email.com";
  }
elseif ($_REQUEST['pickevent'] == 'RaceCure'))
//if "RaceCure" is filled out, send to email
  {
  //send email
  $sendtoname = "Susan";
  $sendtoemail = "susan@email.com";
  }  
elseif ($_REQUEST['pickevent'] == 'Bowling'))
//if "Bowling" is filled out, send to email
  {
  //send email
  $sendtoname = "John";
  $sendtoemail = "john@email.com";
  }

但另外我需要要求选择一个事件,因此“else”将是一个弹出窗口,上面写着“选择一个事件!”。这有帮助吗?

最后编辑:

好的,所以我得到了验证工作。谢谢。我正在取代它:

$sendtoname = "Mike Gandy";
$sendtoemail = "me@myemail.com";

有了这个:

switch(strtolower($event){
    case 'HeartWalk':
        $sendtoname('Bob')
        $sendtoemail('bob@email.com');
        break;
    case 'RaceCure':
        $sendtoname('Susan')
        $sendtoemail('susan@email.com');
        break;
    case 'Bowling':
        $sendtoname('John')
        $sendtoemail('john@email.com');
        break;
}

添加:

$headers .= "Cc: somebody@domain.com" . "\r\n";

并将我的消息的开头改为:

$message = "Dear $sendtoname, <br><br>A new volunteer has registered for your "$_REQUEST['pickevent'];
$message .= " event. Below is a copy of their registration: ".$_REQUEST['blank'];
$message .= " <br><br> <b>First Name:</b> ".$_REQUEST['firstname'];

并将其包含在我的表格中:

<input type="hidden" name="blank" value="">

是?否?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用switch语句。

switch(strtolower($event){
    case 'heart walk':
        $sendtoemail('bob@email.com');
        break;
    case 'some other walk':
        $sendtoemail('theotherguy@email.com');
        break;
    default:
        $sendtoemail('info@email.com');
        break;
}

答案 1 :(得分:1)

  1. 使用select HTML标记创建下拉菜单:

    <select name="event">
    <option value="heartwalk">Heart Walk</option>
    <option value="other">Other</option>
    </select>
    
  2. 处理帖子时,请使用$_REQUEST["event"]作为决定:

    switch ($_REQUEST["event"])
    {
    case 'heartwalk':
        $sendtoemail = "heartwalk@example.com";
        break;
    default:
        $sendtoemail = "other@example.com";
        break;
    }
    
  3. 要进行验证,您可以在提交表单时在客户端上开始JavaScript验证。例如,请参阅:https://www.tutorialspoint.com/javascript/javascript_form_validations.htm

  4. 当然,也建议在服务器端进行一些验证。根据您对系统的强大程度,您可能只是没有做任何事情来将浏览器重定向回到表单并在页面上显示消息。