联系表格发送时电子邮件无效

Posted

技术标签:

【中文标题】联系表格发送时电子邮件无效【英文标题】:Invalid email upon contact form send 【发布时间】:2016-09-21 12:12:52 【问题描述】:

我在发送时收到“无效电子邮件”,即使电子邮件是正确的。我对这段代码有疑问。有人可以看一眼吗?

这是 html

<form id="contact-form" action="email.php" method="post" class="clearfix">
    <div class="contact-box-hide">
        <div class="col-sm-6">
            <input type="text"  class="form-control" id="first_name" name="first_name" required placeholder="Navn">
            <span class="first-name-error"></span>
        </div>
        <div class="col-sm-6">
            <input type="text"  class="form-control" id="last_name" name="last_name" required placeholder="Efternavn">
            <span class="last-name-error"></span>
        </div>
        <div class="col-sm-6">
            <input type="email" class="form-control"  id="email" name="contact_email" required placeholder="Email">
            <span class="contact-email-error"></span>
        </div>
        <div class="col-sm-6">
            <input type="text"  class="form-control" id="subject" name="contact_subject" required placeholder="Hvor skal oplægget afholdes?">
            <span class="contact-subject-error"></span>
        </div>

    <!--<div class="col-sm-6">
            <label class=".radio-inline">
                <input type="radio" class="form-control" id="choice" name="options" required placeholder="Emne">
                <span class="contact-subject-error"></span>Option 1
            </label>
            <label class=".radio-inline" style="border-left: 0px;" >
                <input type="radio"  class="form-control" id="choice" name="options" required placeholder="Emne">
                <span class="contact-subject-error"></span>Option 2
            </label>                    
        </div>
        -->
        <!--<div class="col-sm-20">
            <input type="radio" class="form-control" id="contact_type" name="nummer 1" checked>
            <span class="contact-x-error"></span>
        </div>
        <div class="col-sm-20">
            <input type="radio" class="form-control" id="contact_type" name="nummer 1" checked>
            <span class="contact-x-error"></span>
        </div>
        -->
        <div class="col-sm-10">
            <textarea class="form-control" rows="5" id="message" name="message" required placeholder="Besked"></textarea>
            <span class="contact-message-error"></span>
        </div>
        <div class="col-sm-2">
            <button id="contact-submit" class="btn custom-btn col-xs-12" type="submit" name="submit"><a href=""></a><i class="fa fa-angle-right"></i></button>
            <span id="contact-loading" class="btn custom-btn col-xs-12"> <i class="fa fa-refresh fa-spin"></i> </span>
        </div>
    </div><!-- /.contact-box-hide -->
    <div class="contact-message"></div>
</form>     

这里是 email.php(.js 调用的contact.php 是相同的):

<?php
    if($_REQUEST['first_name'] == '' || $_REQUEST['contact_email'] == '' ||  $_REQUEST['message'] == ''):
        return "error";
    endif;
    if (filter_var($_REQUEST['contact_email'], FILTER_VALIDATE_EMAIL)):
        $subject = 'Fra Kriger til Kaemper - kontaktformular'; // Subject of your email
        // Receiver email address
        $to = 'geerrift@gmail.com';  //Change the email address by yours

        // prepare header
        $header = 'From: '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "\r\n";
        $header .= 'Reply-To:  '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "\r\n";
        // $header .= 'Cc:  ' . 'example@domain.com' . "\r\n";
        // $header .= 'Bcc:  ' . 'example@domain.com' . "\r\n";
        $header .= 'X-Mailer: PHP/' . phpversion();
        $message .= 'Name: ' . $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . "\n";
        $message .= 'Email: ' . $_REQUEST['contact_email'] . "\n";
        $message .= 'Subject: ' . $_REQUEST['contact_subject'] . "\n";
        $message .= 'Message: '. $_REQUEST['message'];
        // Send contact information
        $mail = mail( $to, $subject , $message, $header );
        echo 'sent';
    else:
        return "error";
    endif; 
?>

这是.js:

$('#contact-submit').click(function () 
    $('.first-name-error, .last-name-error, .contact-email-error, .contact-subject-error, .contact-message-error').hide();
    var first_nameVal = $('input[name=first_name]').val();
    var last_nameVal = $('input[name=last_name]').val();
    var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]1,3\.[0-9]1,3\.[0-9]1,3\.[0-9]1,3\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]2,))$/igm;
    var emailVal = $('#contact_email').val();
    var contact_subjectVal = $('input[name=contact_subject]').val();
    var messageVal = $('textarea[name=message]').val();

    //validate 

    if (first_nameVal == '' || first_nameVal == 'First Name *') 
        $('.first-name-error').html('<i class="fa fa-exclamation"></i> Vi har brug for dit fornavn.').fadeIn();
        return false;
    
    if (last_nameVal == '' || last_nameVal == 'Last Name *') 
        $('.last-name-error').html('<i class="fa fa-exclamation"></i> Vi har brug for dit efternavn.').fadeIn();
        return false;
    
    if (emailVal == "" || emailVal == "Email Address *") 

        $('.contact-email-error').html('<i class="fa fa-exclamation"></i> Vi har brug for din mailadresse.').fadeIn();
        return false;

     else if (!emailReg.test(emailVal)) 

        $('.contact-email-error').html('<i class="fa fa-exclamation"></i> Ugyldig mailadresse.').fadeIn();
        return false;
    
    if (contact_subjectVal == '' || contact_subjectVal == 'Subject *') 
        $('.contact-subject-error').html('<i class="fa fa-exclamation"></i> Subject is required.').fadeIn();
        return false;
    
    if (messageVal == '' || messageVal == 'Message *') 
        $('.contact-message-error').html('<i class="fa fa-exclamation"></i> Skriv venligst en henvendelse.').fadeIn();
        return false;
    

    var data_string = $('.contact-form').serialize();

    $('#contact-submit').hide();
    $('#contact-loading').fadeIn();
    $('.contact-error-field').fadeOut();

    $.ajax(
        type: "POST",
        url: "php/contact.php",
        data: data_string,

        //success
        success: function (data) 

            $('.contact-box-hide').slideUp();
            $('.contact-message').html('<i class="fa fa-check contact-success"></i><div>Your message has been sent.</div>').fadeIn();
        ,
        error: function (data) 

            $('.btn-contact-container').hide();
            $('.contact-message').html('<i class="fa fa-exclamation contact-error"></i><div>Something went wrong, please try again later.</div>').fadeIn();
        

    ) //end ajax call
    return false;
);

如果我遗漏了什么,请告诉我,我会立即提供!

【问题讨论】:

您能分享一封无效的电子邮件吗? 我已经尝试了几个。没有工作。 【参考方案1】:

改变

var emailVal = $('#contact_email').val();

var emailVal = $('#email').val();

var emailVal = $('input[name="contact_email"]').val();

在您的电子邮件字段中,您的 ID 为 email,名称为 contact_email。在 jQuery 中使用哈希 (#) 时,您搜索的是 ID,而不是名称。所以你的脚本无法找到电子邮件值。

【讨论】:

你能指定这个位的确切位置吗?它是在 php 中还是在 .js 中? (我在这方面完全是新手)更新:找到了! 它在 javascript (js) 中。 第二个问题是我没有收到表格。这些通常会有延迟还是我应该立即期待?

以上是关于联系表格发送时电子邮件无效的主要内容,如果未能解决你的问题,请参考以下文章

联系表格 7 WP 主题在提交时未发送输入的信息

如何在 Docker 容器中从 WordPress(联系表格)发送电子邮件?

php WordPress - 联系表格7 - “尝试发送邮件时出错。请稍后再试。”

我正在尝试通过联系表格发送电子邮件

发送电子邮件前打印一些数据(联系表格7)

Django联系表格邮件发送[重复]