发送有条件的电子邮件如果不能正常工作

时间:2014-09-04 15:57:33

标签: php forms if-statement sendmail

当字段montantdenande小于1001且超过该数字(1001)时,我正在尝试将表单中的信息发送到email1@email.com电子邮件。但是,信息始终会发送到第一封电子邮件:

<?php
if($f_montantdemande < 1001) {
$send_to = "email1@email.com";


}   
Else{
$send_to = "email2t@email.com";
}   

$send_subject = "test form ";


$f_name = cleanupentries($_POST["name"]);
$f_surname = cleanupentries($_POST["surname"]);
$f_email = cleanupentries($_POST["email"]);
$f_phone = cleanupentries($_POST["phone"]);
$f_codepostal = cleanupentries($_POST["codepostal"]);
$f_revenubrut = cleanupentries($_POST["revenubrut"]);
$f_montantdemande = cleanupentries($_POST["montantdemande"]);
$f_message = cleanupentries($_POST["message"]);
$from_ip = $_SERVER['REMOTE_ADDR'];
$from_browser = $_SERVER['HTTP_USER_AGENT'];

function cleanupentries($entry) {
    $entry = trim($entry);
    $entry = stripslashes($entry);
    $entry = htmlspecialchars($entry);

    return $entry;
}

$message = "Test form \nDemande en ligne en date du " . date('d-m-Y') . 
"\n\nNom: \n" . $f_name . 
"\n\nPrenom: \n" . $f_surname . 
"\n\nCourriel: \n" . $f_email .
"\n\nTelephone: \n" . $f_phone . 
"\n\nCode postal \n" . $f_codepostal .
"\n\nRevenu brut: \n" . $f_revenubrut . " $ " .
"\n\nMontant demandé: \n" . $f_montantdemande .
"\n\nRaison de la demande: \n" . $f_message . 
"\n\n\nTechnical Details:\n" . $from_ip . "\n" . $from_browser;

$send_subject .= " - {$f_surname} {$f_name}";

$headers = "From: " . $f_email . "\r\n" .
      "Reply-To: " . $f_email . "\r\n" .
       "X-Mailer: PHP/" . phpversion();

if(empty($_POST['comment'])) {
   // le champ est vide
}
if (!$f_email) {
   echo "no email";
   exit;
}else if (!$f_name){
    echo "no name";
    exit;
}else if (!$f_surname){
    echo "no surname";
    exit;
}else if (!$f_phone){
    echo "no phone";
    exit;
}else if (!$f_revenubrut){
    echo "no revenubrut";
    exit;
}else if (!$f_montantdemande){
    echo "no montantdemande";
    exit;
}else if (!$f_codepostal){
    echo "no codepostal";
    exit;
}else if (!$f_message){
    echo "no message";
    exit;
}else{
    if (filter_var($f_email, FILTER_VALIDATE_EMAIL)) {
       mail($send_to, $send_subject, $message, $headers);
       echo "true";
}else{
    echo "invalid email";
    exit;
   }
}



?>

1 个答案:

答案 0 :(得分:0)

将条件语句移到变量声明下面。

<?php

$send_subject = "test form ";

$f_name = cleanupentries($_POST["name"]);
$f_surname = cleanupentries($_POST["surname"]);
$f_email = cleanupentries($_POST["email"]);
$f_phone = cleanupentries($_POST["phone"]);
$f_codepostal = cleanupentries($_POST["codepostal"]);
$f_revenubrut = cleanupentries($_POST["revenubrut"]);
$f_montantdemande = cleanupentries($_POST["montantdemande"]);
$f_message = cleanupentries($_POST["message"]);
$from_ip = $_SERVER['REMOTE_ADDR'];
$from_browser = $_SERVER['HTTP_USER_AGENT'];

if ($f_montantdemande < 1001) {
    $send_to = "email1@email.com";
}   
else {
    $send_to = "email2t@email.com";
}