How to send website form to email

Asked

Viewed 197 times

0

I need to send the form from a website to the customer’s email.

Image of the form: Imagem do formulário: all validations are working correctly but when I ask to send the message that the server was not found appears; The site is already hosted on the Hostnet server and the email is outlook: [email protected]

below the PHP code:

<?php
// Check for empty fields
if(empty($_POST['name'])  		||
   empty($_POST['email']) 		||
   empty($_POST['phone']) 		||
   empty($_POST['message'])	||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
	echo "No arguments Provided!";
	return false;
   }
	
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
	
// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address";	
mail($to,$email_subject,$email_body,$headers);
return true;			
?>

Below code Javascript validation:

    $(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "http://bootstrapseven.com/demos/enhenyero/html/submit.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Sua menságem foi enviada com sucesso. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contact-form').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Desculpe " + firstName + ", parece que meu servidor de e-mail não está respondendo. Por favor, tente novamente mais tarde!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contact-form').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});
  • What exactly is the message that appears? You can post a print?

  • The error message that appears is the one in Java Script, in Function error //fail message, I think that Javascript is unable to access the PHP file.

  • Try to display on the console console.log() what returns from Ajax to see. Puts inside the error block.

  • Menságem: Xmlhttprequest cannot load http://bootstrapseven.com/demos/enhenyero/html/submit.php. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://www.mcampelotopografia.com.br' is therefore not allowed access.

  • The problem is that the page you tah ordering is not authorizing access.

  • If you want to send an email, why not use in Ajax the PHP page you posted above?

  • I think I get it now. PHP above is the same as the URL in Ajax. But I noticed that the email in "from" is [email protected]\n. I think you should put in a valid e-mail. Generally, providers only send emails whose sender’s domain is the same as the hosting domain (e.g., your website: www.meusite.com.br. The email must be @meusite.com.br)

  • Yes. Ajax also does not access other domains. Gives authorization error.

  • Ok I will create an email from the provider and test again

  • Just another question, which url should I put to the PHP file

  • He is on www/Submit.php

  • in the same place as the index.html page

  • In the URL you only paste the PHP file name if it is in the same Ajax folder.

  • Thus: url: "submit.php",

  • I checked the hosting plan here and does not support PHP =(

  • probably this is the reason for the mistake, or I’m wrong ?

  • Wow. Most servers support PHP, including Windows. Run a test: create a test.php file with <?php echo "este servidor suporta PHP" ?> and tries to open. If it appears the above message is pq supports.

  • To send email through the site you need some programming environment, such as PHP or ASP, for example.

  • Thanks, @Davidsamm but my provider does not have PHP support I will have to do otherwise.

Show 14 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.