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.

<!DOCTYPE html>
    <title>php code to send email from contact form -</title>

		<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">

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

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

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

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

	<script src=""></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)) {
                   url: 'liveMailSnd.php',
                   method: 'POST',
                   dataType: 'json',
                   data: {
                       name: name.val(),
                       email: email.val(),
                       title: title.val(),
                       message: message.val()
                   }, success: function (response) {
                        $('.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;


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

Step 2: Sending the Email with PHP

In emails, settings headers are really important.

//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->Host = "";
    $smail->SMTPAuth = true;
    $smail->Username = "[email protected]";
    $smail->Password = 'yourpassword';
    $smail->Port = 465;
    $smail->SMTPSecure = "ssl";

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

        $status = "success";
        $response = "Good Luck, Great Your Email is sent!";
        $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
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.