php code to send email from contact form

php code to send email from contact form – The simplest way to send an email with PHP is to send a text email. PHP script to connect to a SMTP server and send email.

php code to send email from contact form

Simple HTML PHP Source Codes For Create a PHP Send Email Contact Form Example. Send email with PHP from html form on submit with the same script

today i am going to learn how to connect the Contact form with PHP mail for your website.

Step : 1 Simple HTML Contact Form to send Email in PHP

Create a simple contact form with PHP and send email.
index.php

<!DOCTYPE html>
<html>
<head>
    <title>php code to send email from contact form - www.pakainfo.com</title>
</head>
<body>

	<center>
		<h4 class="sent-basicmail"></h4>

		<form id="myForm">
			<h2>Send an Email</h2>

			<label>Full Name</label>
			<input id="name" type="text" placeholder="Please Enter Your Name">
			<br><br>

			<label>Your Email</label>
			<input id="email" type="text" placeholder="Please Enter Your Email">
			<br><br>

			<label>Head Line / Title</label>
			<input id="title" type="text" placeholder=" Please Enter Your Title or HeadLine">
			<br><br>

			<p>Message / Comments</p>
			<textarea id="message" rows="6" placeholder="Type Message"><textarea>
			<br><br>

			<button type="button" onclick="liveMailSnd()" value="Send An Email">Submit</button>
		</form>
	</center>

	<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
	<script type="text/javascript">
        function liveMailSnd() {
            var name = $("#name");
            var email = $("#email");
            var title = $("#title");
            var message = $("#message");

            if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(title) && isNotEmpty(message)) {
                $.ajax({
                   url: 'liveMailSnd.php',
                   method: 'POST',
                   dataType: 'json',
                   data: {
                       name: name.val(),
                       email: email.val(),
                       title: title.val(),
                       message: message.val()
                   }, success: function (response) {
                        $('#myForm')[0].reset();
                        $('.sent-basicmail').text("Good Luck, Your Message Sent Successfully.");
                   }
                });
            }
        }

        function isNotEmpty(caller) {
            if (caller.val() == "") {
                caller.css('border', '1px solid red');
                return false;
            } else
                caller.css('border', '');

            return true;
        }
    </script>

</body>
</html>
      

Don’t Miss : Simple file attachment in PHP mail function

Step 2: Sending the Email with PHP

In emails, settings headers are really important.
liveMailSnd.php

<?php
//php code to send email from contact form
use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['name']) && isset($_POST['email'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $title = $_POST['title'];
    $message = $_POST['message'];

    require_once "PHPMailer/PHPMailer.php";
    require_once "PHPMailer/SMTP.php";
    require_once "PHPMailer/Exception.php";

    $smail = new PHPMailer();

    //here simple way to smtp settings
    $smail->isSMTP();
    $smail->Host = "smtp.gmail.com";
    $smail->SMTPAuth = true;
    $smail->Username = "[email protected]";
    $smail->Password = 'yourpassword';
    $smail->Port = 465;
    $smail->SMTPSecure = "ssl";

    //here some email settings
    $smail->isHTML(true);
    $smail->setFrom($email, $name);
    $smail->addAddress("[email protected]");
    $smail->Subject = ("$email ($title)");
    $smail->Body = $message;

    if($smail->send()){
        $status = "success";
        $response = "Good Luck, Great Your Email is sent!";
    }
    else
    {
        $status = "failed";
        $response = "Sorry, Something is wrong: <br>" . $mail->ErrorInfo;
    }

    exit(json_encode(array("status" => $status, "response" => $response)));
}

?>
      

I hope you get an idea about php code to send email from contact form.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.