仅在填写的电子邮件和电话号码填写正确时发送电子邮件

时间:2016-07-20 09:18:21

标签: php jquery html5 email contact-form

我在sendmail.php文件中有以下代码。

<?php

if($_POST) {

    // Enter the email where you want to receive the message
    $emailTo = 'example@gmail.com';

    // Form fields
    $clientName = addslashes(trim($_POST['name']));
    $clientEmail = addslashes(trim($_POST['email']));
    $number = addslashes(trim($_POST['number']));
    $message = addslashes(trim($_POST['message']));

    // Email Ssubject
    $subject = 'Query from My Domain';

    // Compose message to send
    $sendMessage = 'Hi' . "\n\n";
    $sendMessage .= $message . "\n\n";
    $sendMessage .= 'From: ' . $clientName . "\n";
    $sendMessage .= 'Email: ' . $clientEmail . "\n";
    $sendMessage .= 'Contact number: ' . $number . "\n";

    $array = array();
    $array['nameMessage'] = '';
    $array['emailMessage'] = '';
    $array['numberMessage'] = '';
    $array['messageMessage'] = '';

    if($clientName == '') {
        $array['nameMessage'] = 'Please enter your full name.';
    }
    if (filter_var($clientEmail, FILTER_VALIDATE_EMAIL) == false) {
        $array['emailMessage'] = 'Please insert a valid email address.';
    } 
    if (!preg_match('/^(\+?)+([0-9]{10,})$/', $number)) {
        $array['numberMessage'] = 'Please enter a valid contact number.';
    }
    if($message == '') {
        $array['messageMessage'] = 'Please enter your message.';
    }
    if($clientName && $clientEmail && $number && $message != '') {
        // Headers
        $headers = "From: " . $clientName . ' <' . $clientEmail . '>' . "\r\n";
        $headers .= "CC: " . 'My Boss <boss@gmail.com>' . "\r\n";
        $headers .= PHP_EOL;
        $headers .= "MIME-Version: 1.0".PHP_EOL;
        $headers .= "Content-Type: multipart/mixed;".PHP_EOL;
        $headers .= " boundary=\"boundary_sdfsfsdfs345345sfsgs\"";

        // Send mail
        mail($emailTo, $subject, $sendMessage, $headers);
    }

    echo json_encode($array);

}   else {
        header ('location: index.html#contact');
}

?>

在此通过电子邮件和电话号码输入验证,这些验证工作得很好。我的问题是操纵下面的这一部分,以便表格仅在填写的电子邮件和电话号码正确时发送电子邮件。如果我没有弄错的话,我需要在下面的代码中摆弄$ clientEmail和$ number,我尝试了许多不起作用的东西。

if($clientName  != '' && $clientEmail && $number && $message != '') {
    // Headers
    $headers = "From: " . $clientName . ' <' . $clientEmail . '>' . "\r\n";
    $headers .= "CC: " . 'My Boss <boss@gmail.com>' . "\r\n";
    $headers .= PHP_EOL;
    $headers .= "MIME-Version: 1.0".PHP_EOL;
    $headers .= "Content-Type: multipart/mixed;".PHP_EOL;
    $headers .= " boundary=\"boundary_sdfsfsdfs345345sfsgs\"";

    // Send mail
    mail($emailTo, $subject, $sendMessage, $headers);
}

1 个答案:

答案 0 :(得分:1)

完成所有验证后,在开始构建电子邮件标题之前,请执行以下操作:

$isValid = empty($array['nameMessage']) && empty($array['emailMessage']) &&
           empty($array['numberMessage']) && empty($array['messageMessage']);
如果表单有效,

$isValid将为true。现在在发送邮件之前检查它:

if($isValid) {
    // build headers and send mail
    ...
    mail(...);
    // maybe you should echo a success message and exit here 
    // if there is nothing else to do
}else{
    echo json_encode($array); //echo the error messages
}