使用if else发送邮件

时间:2013-10-06 13:33:52

标签: php email if-statement email-integration

我有一个表单,从下拉菜单中获取US I状态,并根据状态值打开pdf。我想使用PHP发送基于该值的不同电子邮件。我想给他们所有50多个州的选择,但由于我只需要关注50个州中的三个,我可以硬编码电子邮件,即。 myAAemail@mydomain.com,myBBemail@mydomain.com和myCC@mydomain.com。然后,我想将状态名称或值放在电子邮件中,但这不是优先事项。以下是我的if else语句如何添加电子邮件?我确定我不必提及我是PHP的新手。

}
if ($_POST['recipient'] == 'AA') {
    header("Location: aa.pdf");
} else if ($_POST['recipient'] == 'BB') {
    header("Location: bb.pdf");          
}else if ($_POST['recipient'] == 'CC') {
    header("Location: cc.pdf");          
}else {
    echo "Error processing form"; 
}

?>

1 个答案:

答案 0 :(得分:0)

您可以使用电子邮件定义关联数组:

$emails = array(
    'AA' => 'email1@mydomain.com',
    'BB' => 'email2@mydomain.com',
    'CC' => 'email3@mydomain.com',
    'DD' => 'email4@mydomain.com',
    'EE' => 'email5@mydomain.com',
    // . . .
);

并将其用作:$mail_address = $emails[$_POST['recipient']];

}
if ($_POST['recipient'] == 'AA') {
    header("Location: aa.pdf");
} else if ($_POST['recipient'] == 'BB') {
    header("Location: bb.pdf");          
}else if ($_POST['recipient'] == 'CC') {
    header("Location: cc.pdf");          
}else {
    //Mark1
    if ( isset($_POST['recipient']) ) {
        echo "Error processing form"; 
    }
}

if ( isset($emails[$_POST['recipient']]) ) {
    $address = $emails[$_POST['recipient']];
    $subject = 'The mail about ' . $_POST['recipient'];
    $message = "Hello, I'm writing the mail about " . $_POST['recipient'];

    mail($address, $subject, $message);
} 

?>