Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How to send an email with HTML template using PHP and Ajax

How to send an email with HTML template using PHP and Ajax

Posted on July 23, 2018September 26, 2021 By XpertPhp No Comments on How to send an email with HTML template using PHP and Ajax

We will have learned how to sending mail with a template using PHP. whenever creating contact form this tutorial is used.

See the below image is my email template design.

mail_template

First, create an HTML form. take a name, email, subject, and message in this form. after then when clicking on the submit button then Ajax will take data and send the request to another page. I have created jquery validation in this HTML form and when validations are completed then submit a handler through send ajax form data. see follow my example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<html>
<head>
<title>Ajax by sending an email with a template using PHP - XpertPhp</title>
</head>
<body>
<form name="frmContact" id="frmContact">
<table border="1" align="center">
    <tr>
        <td colspan="2" align="center">Contact Form</a></td>
    </tr>
    <tr>
<td>Name</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" id="email"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="subject" id="subject"></td>
</tr>
<tr>
<td>Message</td>
<td><input type="text" name="message" id="message"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnContact"></td>
</tr>      
</table>
</form>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
 
<script>
    $(document).ready(function(){
        $("#frmContact").validate({
        rules:
        {
            name:
            {
              required:true
            },
            email:
            {
        required:true,
email:true
            },
            subject:
    {
required:true
    },
            message:
    {
required:true
    }
        },
 
        messages:
        {
        },
        submitHandler: function(form)
        {
 
            var fomr = $('form')[0];
            var formData = new FormData(fomr);
            $.ajax({
            type: 'POST',
            url: "contact_sendmail.php",
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success: function(data)
            {
                if(data=='Message Send Successfully')
                {
                    alert(data);
 
                }
                else
                {
                    alert(data);
                }
            }
            });
            return false;
        }
     });
 
});
</script>
</body>
</html>

Get the all form data and store in a particular variable. This has used the PHP Mail function in the below example. if you want to send mail in the template then create the template and its store in the variable, and this variable can pass in the mail function. if mail will be sent successfully then return the success message otherwise return fail message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
     if(isset($_POST['btnContact']))
   {
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$msg     =  $_REQUEST['message'];
    $message = '<html><body>';
$message.='<table border="0" width="600px" style="font-size:14px; text-align:center">';
$message.='<tr style="background: #1eaace; color:#FFFFFF; height: 50px;"><td colspan="2">Contact Information</td></tr>';
$message.='<tr style="background: #1eaace; color:#FFFFFF; height: 50px;"><td>Name</td><td>'.strip_tags($name).'</td></</tr>';
$message.='<tr style="background: #1eaace; color:#FFFFFF; height: 50px;"><td>Email</td><td>'.strip_tags($email).'</td></</tr>';
$message.='<tr style="background: #1eaace; color:#FFFFFF; height: 50px;"><td>Subject</td><td>'.strip_tags($subject).'</td></</tr>';
$message.='<tr style="background: #1eaace; color:#FFFFFF; height: 50px;"><td>Message</td><td>'.strip_tags($msg).'</td></</tr>';
$message.='</table>';
        $message.='</body></html>';
$to=$email;
$from='[email protected]';
$subject='Contact Infromation';
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
   $retval = mail ($to,$subject,$message,$headers);
   if($retval==true)
   {
  echo "Message Send Successfully";
   }
else
   {
  echo "failed";
   }
   }
 
?>

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • How to use yajra datatable in laravel 5.8
  • Convert XML into Associative Array in PHP
  • Drag and Drop rows using jquery with ajax example
  • Laravel 7 CRUD Operation With Ajax Example
  • Multiple image upload using jquery Ajax and Php
Ajax, Jquery, Php Tags:contact form, form validation, jQuery Ajax Form Handling, php mail server, php mail smtp, php send email, php send email form, php send email smtp, send mail, send mail using smtp in php

Post navigation

Previous Post: Number_format() function in php
Next Post: How to Send Email with Attachment in PHP

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com