【问题标题】:How configured contact form for sending email如何配置用于发送电子邮件的联系表
【发布时间】:2014-10-24 07:48:17
【问题描述】:

我请求您帮助解决这个问题。我有一个带有相应联系表格的网站,但它从来没有为我工作过。消息没有到达。向他们发送联系页面send_mail.php 文件和所有相关内容。非常感谢您的帮助和详细解释,因为这个主题非常复杂。

Contact.js

    $(document).ready(function(){
            $('#send_message').click(function(e){

                //stop the form from being submitted
                e.preventDefault();

                /* declare the variables, var error is the variable that we use on the end
                to determine if there was an error or not */
                var error = false;
                var name = $('#name').val();
                var email = $('#email').val();
                var subject = $('#subject').val();
                var message = $('#message').val();

                /* in the next section we do the checking by using VARIABLE.length
                where VARIABLE is the variable we are checking (like name, email),
                length is a javascript function to get the number of characters.
                And as you can see if the num of characters is 0 we set the error
                variable to true and show the name_error div with the fadeIn effect. 
                if it's not 0 then we fadeOut the div( that's if the div is shown and
                the error is fixed it fadesOut. 

                The only difference from these checks is the email checking, we have
                email.indexOf('@') which checks if there is @ in the email input field.
                This javascript function will return -1 if no occurence have been found.*/
                if(name.length == 0){
                    var error = true;
                    $('#name_error').fadeIn(500);
                }else{
                    $('#name_error').fadeOut(500);
                }
                if(email.length == 0 || email.indexOf('@') == '-1'){
                    var error = true;
                    $('#email_error').fadeIn(500);
                }else{
                    $('#email_error').fadeOut(500);
                }
                if(subject.length == 0){
                    var error = true;
                    $('#subject_error').fadeIn(500);
                }else{
                    $('#subject_error').fadeOut(500);
                }
                if(message.length == 0){
                    var error = true;
                    $('#message_error').fadeIn(500);
                }else{
                    $('#message_error').fadeOut(500);
                }

                //now when the validation is done we check if the error variable is false (no errors)
                if(error == false){
                    //disable the submit button to avoid spamming
                    //and change the button text to Sending...
                    $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });

                        /* using the jquery's post(ajax) function and a lifesaver
                        function serialize() which gets all the data from the form
                        we submit it to send_email.php */
                    $.post("send_email.php", $("#contact_form").serialize(),function(result){
                        //and after the ajax request ends we check the text returned
                        if(result == 'sent'){
                            //if the mail is sent remove the submit paragraph
                             $('#button').remove();
                            //and show the mail success div with fadeIn
                            $('#mail_success').fadeIn(500);
                        }else{
                            //show the mail failed div
                            $('#mail_fail').fadeIn(500);
                            //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                            $('#send_message').removeAttr('disabled').attr('value', 'Submit');
                        }
                    });
                }
            });    
        });

Send_mail.php

    require_once 'google/appengine/api/mail/Message.php';
    use google\appengine\api\mail\Message;

    $name     =   $_POST['name'];  
    $email    =   $_POST['email'];
    $subject  =   $_POST['subject'];
    $message  =   $_POST['message'];
    $message_body = "...";
    $mail_options =[;
            "sender" => "administrador@formarchivos.com",
        "to" => $email,
        "subject" => $subject,
        "textBody" => $message_body
    ];

    try {
            $message = new Message($mail_options);
            $message->send();
            echo 'sent';

} catch (InvalidArgumentException $e) {
            echo $e->getMessage();
            echo 'failed';
}

Contact.html

    <!-- Contact Form -->
    <form action="send_email.php" method="post" id="contact_form">
      <h3>Ficha de contacto</h3>
      <div class="hr dotted clearfix">
        <h3>&nbsp;</h3>
      </div>
      <ul>
        <li class="clearfix">
          <h3>
            <label for="name">Nombre</label>
            <input type='text' name='name' id='name' />
          </h3>
          <div class="clear"></div>
          <h3 id='name_error' class='error'>Inserte un nombre</h3>
        </li>
        <li class="clearfix">
          <h3>
            <label for="email">Email </label>
            <input type='text' name='email' id='email' />
          </h3>
          <div class="clear"></div>
          <h3 id='email_error' class='error'>Ingrese una cuenta de correo valida</h3>
        </li>
        <li class="clearfix">
          <h3>
            <label for="subject">Tema</label>
            <input type='text' name='subject' id='subject' />
          </h3>
          <div class="clear"></div>
          <h3 id='subject_error' class='error'>Ingrese un Tema para su mensaje</h3>
        </li>
        <li class="clearfix">
          <h3>
            <label for="message">Mensaje</label>
            <textarea name='message' id='message' rows="30" cols="30"></textarea>
          </h3>
          <div class="clear"></div>
          <h3 id='message_error' class='error'>Ingrese su mensaje</h3>
        </li>
        <li class="clearfix">
          <h3 id='mail_success' class='success'>Gracias. Nosotros te responderemos tan pronto como nos sea posible.</h3>
          <h3 id='mail_fail' class='error'>Disculpenos, un error ha ocurrido. Por favor intentelo despues.</h3>
          <div id="button">
            <h3>
              <input type='submit' id='send_message' class="button" value='Enviar' />
            </h3>
          </div>
          <!--end wrapper-->
<div align=center></div></body>
</html>

这就是我所拥有的,我什么都没有mensaje.php,如果我可能不需要任何额外的文件,老实说我不知道​​错误在哪里。

【问题讨论】:

  • 看起来您的 php 文件中有语法错误。如果我没记错的话,下面一行不应该有任何分号:$mail_options =[;

标签: javascript php html css


【解决方案1】:

除了@koljanep 建议的语法错误之外,我还有一些建议/cmets:

  1. 您是否尝试过执行一项功能(甚至可能在 php 页面上)来处理您的 jQuery 错误而不是重复一堆相同的淡入淡出效果?

  2. 也许会清理您在 PHP 页面上的输入。

  3. 也许更直接的AJAX 会有所帮助。

  4. 您指定 $message = $_POST['message']; 但是你分配了$message = new Message($mail_options); 所以有一些奇怪的事情发生 那里……

Contact.js

    $(document).ready(function(){
            $('#contact_form').submit(function(e){
                $.ajax({
                        url:"Send_mail.php",
                        type: 'POST',
                        data: $("#send_message").serialize(),
                        success: function(data) {
                                // This id would have to be somewhere on your page
                                // to drop into (I'm sure you know that)
                                $("#response").html(data);
                            }
                    });

                return false;
            });    
        });

Send_mail.php

<?php
    // Do some basic sanitizing...I chose preg_replace, but you can do whatever
    $setting['name']    =   (!empty($_POST['name']))? preg_replace('/[^0-9a-zA-Z\.\']/'," ",$_POST['name']):0;
    $setting['subject'] =   (!empty($_POST['subject']))? preg_replace('/[^0-9a-zA-Z\.\']/'," ",$_POST['subject']):0;
    $setting['message'] =   (!empty($_POST['message']))? strip_tags($_POST['message']):0;

    // Check first that the email is a valid one!
    if(isset($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            // If all checks out, 
            if(!in_array(0,$setting)) {
                    require_once('google/appengine/api/mail/Message.php');
                    use google\appengine\api\mail\Message;

                    $name           =   $_POST['name'];  
                    $email          =   $_POST['email'];
                    $subject        =   $_POST['subject'];
                    $message        =   $_POST['message'];
                    $message_body   =   "...";
                    $mail_options   =   [
                                            "sender" => "administrador@formarchivos.com",
                                            "to" => $email,
                                            "subject" => $subject,
                                            "textBody" => $message.$message_body
                                        ];

                    try {
                            $sendMessage = new Message($mail_options);
                            $sendMessage->send();
                            echo 'sent';
                        }
                    catch (InvalidArgumentException $e) {
                            echo $e->getMessage();
                            echo 'failed';
                        }
                }
            else 
                $_error =   $setting;
        }
    else {
            $setting['email']   =   0;
            $_error             =   $setting;
        } ?>

<script>
    function ReturnError(IdVal) {
            $('#'+IdVal+"_error").fadeIn(500);
        }
<?php
    if(isset($_error)) {
            foreach($_error as $key => $errored) {
                    if($errored == 0) { ?>
    ReturnError('<?php echo $key; ?>');
                <?php }
                }
        } ?>
</script>

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 2020-05-20
    • 2019-10-03
    • 2020-12-07
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多