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 = '[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 = @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;
?>
14
  • Are you getting an issue or asking for review of your code?
    – clonelab
    Dec 22, 2014 at 7:24
  • You can't boostrap is not server side. You create the form with bootstrap but it alone can not send mail
    – exussum
    Dec 22, 2014 at 7:26
  • i am asking for the problem only mail is been not sending by using this code so only i posted these codes. @clonerworks Dec 22, 2014 at 7:28
  • Then how can i send email from bootstrap frame work @exussum Dec 22, 2014 at 7:30
  • On the server is this running for you have a local mail server or using an STMP server at all?
    – clonelab
    Dec 22, 2014 at 7:30

1 Answer 1

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.

8
  • What are the changes i need to change in my php.ini file to send mail to gmail account Dec 22, 2014 at 7: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.
    – clonelab
    Dec 22, 2014 at 7:58
  • This is my php.ini file mail settings Dec 22, 2014 at 8: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. ; php.net/sendmail-from ;sendmail_from = postmaster@localhost Dec 22, 2014 at 8:02
  • Try here - stackoverflow.com/questions/15965376/…
    – clonelab
    Dec 22, 2014 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.