0

I have a problem with a form. Submition seems to work (the fading animation works) but I don't receive emails. Is there any error in the following code?

<div id="section-4"> 
        <div class="twelve columns">
        <h3>Would you like to cooperate?
        Get in touch</h3>
        <p></p>
<div id="success">
    <span class="green textcenter">
        <p>Your message was sent successfully! I will be in touch as soon as I can.</br><a href="http://www.ewelinawoloszyn.com">Refresh the page here.</a></p>
    </span>
</div>

<div id="error">
    <span>
        <p>Something went wrong, try refreshing and submitting the form again.</p>
    </span>
</div>

                <form id="contact" name="contact" method="post" novalidate="novalidate">
    <fieldset>

        <label for="email" id="email">Email<span class="required">*</span>
        </label>
        <input type="text" name="email" id="email" size="30" value="" required="">
        <label for="Message" id="message">Message<span class="required">*</span>
        </label>
        <textarea name="message" id="message" required=""></textarea>
        <label for="Answer" id="answer">Name the small house pet that says “<i>meow</i>“<span class="required">*</span>
        </label>
        <input type="text" name="answer" value="" required=""></br>
        <input id="submit" type="submit" name="submit" value="Send">
    </fieldset>
</form></div>
</div>

        <hr/>
<footer>
All rights reserved.
</footer></div><script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.32/jquery.form.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>
<script type="text/javascript">
jQuery.validator.addMethod('answercheck', function (value, element) {
        return this.optional(element) || /^\bcat\b$/.test(value) || /^\bkitten\b$/.test(value) || /^\bkot\b$/.test(value) || /^\bkotek\b$/.test(value);
    }, "Type the correct answer ;)");

// validate contact form
$(function() {
    $('#contact').validate({
        rules: {

            email: {
                required: true,
                email: true
            },
            message: {
                required: true
            },
            answer: {
                required: true,
                answercheck: true
            }
        },
        messages: {

            email: {
                required: "No email, no message"
            },
            message: {
                required: "Please write something to send this form.",
                minlength: "This message is too short."
            },
            answer: {
                required: "Sorry, wrong answer!"
            }
        },
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                type:"POST",
                data: $(form).serialize(),
                url:"process.php",
                success: function() {
                    $('#contact :input').attr('disabled', 'disabled');
                    $('#contact').fadeTo( "slow", 0.15, function() {
                        $(this).find(':input').attr('disabled', 'disabled');
                        $(this).find('label').css('cursor','default');
                        $('#success').fadeIn();
                    });
                },
                error: function() {
                    $('#contact').fadeTo( "slow", 0.15, function() {
                        $('#error').fadeIn();
                    });
                }
            });
        }
    });
});
</script>

Here's Php code:

<?php

    $to = "youremail@gmail.com";
    $from = $_REQUEST['email'];
    $headers = "From: $from";
    $subject = "You have a message.";

    $fields = array();

    $fields{"email"} = "email";
    $fields{"message"} = "message";

    $body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

    $send = mail($to, $subject, $body, $headers);

?>

Any suggestions are more than welcome. I'm using Html, jQuery, and Php (Php just for the form).I have the problem since I've changed host. I've tried a few solutions from similar submitions without luck. Perhaps I need to update jQuery?

Regards, Neko

Neko
  • 65
  • 11
  • Any PHP errors? [See this post on how to enable errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – lumio Sep 06 '17 at 15:10
  • Are you sure your host allows emails? – Roxoradev Sep 06 '17 at 15:16
  • 1
    does $send end up as true or false? And remember that _sending_ email does not guarantee _receiving_ email. There is a lot that happens in between. You can only take care of the sending part. (Although there are things you can do to help your email get through spam filters etc, in terms of its content and the mailserver config, but that's a bigger sysadmin topic). First thing to do is check whether `mail()` returns true or not. You haven't even done that basic test that by the looks of things. – ADyson Sep 06 '17 at 15:16
  • 1
    BTW If you're letting users input any old "from" address, and that doesn't match the domain the mail is coming from, I suspect a lot of recipient mailservers are likely to treat that as being as bit suspicious. It's technically possible, but it doesn't look good. – ADyson Sep 06 '17 at 15:18
  • Thanks guys Ill try to test it – Neko Sep 06 '17 at 16:34
  • Ok here're my errors from the php file: Notice: Undefined index: email in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/process.php on line 6 Notice: Undefined index: name in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/process.php on line 7 Notice: Undefined index: email in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/process.php on line 15 Notice: Undefined index: message in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/process.php on line 15 – Neko Sep 06 '17 at 16:39
  • how to check if mail() returns true or not? – Neko Sep 06 '17 at 16:46
  • "how to check if mail() returns true or not?" it's the value that it returns...like any function which returns a value. `$success = mail(`...etc. Then you check the value of `$success` using an `if` statement. The manual at http://php.net/manual/en/function.mail.php gives you details of what it returns, and lots of sample code. – ADyson Sep 06 '17 at 19:49
  • as for "Undefined index: email", obviously we don't know your line numbers but it probably means that `$_REQUEST["email"]` does not exist, if the line where you call that is line 7 or 15 of process.php? I don't even know if the code you've shown is from process.php or not? It's not clear from the question. – ADyson Sep 06 '17 at 19:52
  • Yes the Php code is from process.php file – Neko Sep 07 '17 at 17:12

0 Answers0