0

Form to send email from bootstrap frame work is that any thing i need to do extra work like adding phpmailer or need to change in php.ini file or need to setup any mail function

html code

<form name="sentMessage" id="contactForm" novalidate>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Your Name *" name="name" id="name" required data-validation-required-message="Please enter your name.">
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="email" class="form-control" placeholder="Your Email *" name= "email" id="email" required data-validation-required-message="Please enter your email address.">
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="tel" class="form-control" placeholder="Your Phone *" name="phone" id="phone" required data-validation-required-message="Please enter your phone number.">
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <textarea class="form-control" placeholder="Your Message *" name="message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
            <div id="success"></div>
            <button type="submit" name="submit" class="btn btn-xl">Send Message</button>
        </div>
    </div>
</form>

js Codes

$(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: "././mail/contact_me.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>Your message has been sent. </strong>");
                    $('#success > .alert-success').append('</div>');
                    //clear all fields
                    $('#contactForm').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>Sorry " + firstName + ", it seems that my mail server is not responding.Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').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('');
});

This is my php file

<?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 = 'prasanna.venkatesan83@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - 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: prasanna.venkatesan83@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";

$mail = @mail($to, $email_subject, $email_body, $headers);

if (!$mail->send()) {
    echo "Cannot send e-mail";
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
}
echo "e-mail Has Been Sent To Your Email Address.";
return true;
?>
Siguza
  • 21,155
  • 6
  • 52
  • 89

1 Answers1

1

I believe the issue your facing is due to the built in php mail function not sending mail. I recommend you use an external library such as swiftmail to send the mail for you.

Because we cannot see your smtp setting due to them been in your php.ini file which is not posted here we cannot evaluate them for you.

Your comments appear to point to the face the code is working fine but the mail is not sending as such the issue is with the php mail code.

clonerworks
  • 641
  • 1
  • 6
  • 16
  • What are the changes i need to change in my php.ini file to send mail to gmail account – Prasanna Venkatesan Dec 22 '14 at 07:57
  • As i stated you havent shown the php.ini file so i cannot advise if the settings you have put in there are correct. – clonerworks Dec 22 '14 at 07:58
  • This is my php.ini file mail settings – Prasanna Venkatesan Dec 22 '14 at 08:01
  • [mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury ; SMTP = localhost ; smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhost – Prasanna Venkatesan Dec 22 '14 at 08:02
  • Try here - http://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost – clonerworks Dec 22 '14 at 08:04
  • If you sending your mail externally what domain are you sending it from? postmaster@localhost will not work because localhost is not a valid domain example.com is however. Are you able to check logs to and see if there are any errors? – clonerworks Dec 22 '14 at 10:51
  • `\xampp\php\logs\php_error_log` or `\xampp\apache\logs\error.log`i think, i havent used xampp in years, i use vagrant. BTW here is a link to the xampp support forum - https://community.apachefriends.org/f/viewforum.php?f=16&sid=96dfe1ef23393269f151afa5fcffd6cb – clonerworks Dec 22 '14 at 10:57
  • the error is [22-Dec-2014 11:26:47 Europe/Berlin] PHP Fatal error: Call to a member function send() on boolean in C:\xampp\htdocs\page\contact_me.php on line 25 – Prasanna Venkatesan Dec 22 '14 at 10:59