All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Send HTML Table In Email Body In PHP

Last Updated : Mar 11, 2024

How To Send HTML Table In Email Body In PHP

In this article we will show you the solution of how to send html table in email body in PHP, we are going to use the PHP mailer to send an HTML table to the email body in php. PHPmailer is used to email creation.

We will create a form in HTML to take input of email subject and body from the user.

To download the PHPMailer, open the command prompt and run the following command

composer require phpmailer/phpmailer

We will use a table from MySQL database as an HTML table and then use it to send mail.

Step By Step Guide On How To Send Html Table In Email Body In PHP :-

HTML code

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to send html table in email body in php </title>
    <link rel="stylesheet" href="">
    <h1 style = "color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h3> how to send html table in email body in php </h3>
    <form action="send-mail.php" method="post">
        <label for="subject"> Email Subject </label>
        <input type="text" id="subject" name="subject">
        <label for="body"> Email body </label>
        <textarea type="text" id="body" name="body" cols="60" rows="10"> </textarea>
        <input type="submit" name="submit" id="submit" value="submit"/>
  1. First, we write <! DOCTYPE html> to mention the version of HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag for styling the form.
  5. Thirdly, the <body> tag is used to define the webpage body.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Create an <form> of the action of the php file ‘send-mail.php’ with the method POST
  8. Create <label> and <input> for Email subject and email body
  9. Create a button for submit.

PHP Code:

require 'PHPMailer/PHPMailer.php' ;
require 'PHPMailer/Exception.php' ;
require 'PHPMailer/SMTP.php' ;
$subject = $_POST['subject'];
$body = $_POST['body'] ;
$mail = new PHPMailer\PHPMailer\PHPMailer() ;
$body = file_get_contents('index.html') ;
$mail -> isSMTP() ;
$mail -> Host ='' ;
$mail -> SMTPAuth = true ;
$mail -> SMTPKeepAlive = true ;
$mail -> Port = 25 ;
$mail -> Username = '' ;
$mail -> Password = 'yourpasswordhere' ;
$mail -> setFrom('', 'test Manager') ;
$mail -> addReplyTo('', 'test Manager') ;
$mail -> Subject = $subject ;
$mail -> Body = $body ;
$mail -> Subject = 'send html table in email body in php' ;
$mysql = mysqli_connect('localhost', 'root', '');
mysqli_select_db($mysql, 'test') ;
$result = mysqli_query($mysql, 'SELECT fname, email FROM list WHERE email = FALSE') ;
foreach($result as row) {
    try {
        $mail -> addAddess($row['email'], $row['fname']) ;
    } catch(Exception $e) {
        echo 'invalid address' ;
        continue ;
    try {
        $mail -> send() ;
        echo 'Message sent';
        mysqli_query (
            "UPDATE list SET email = true WHERE email= '".mysqli_real_escape_string($mysql, $row['email'])."'"
        ) ;
    } catch (Exception $e) {
        echo 'Mailer Error' ;
        $mail -> getSMTPInstance() ->reset() ;
    $mail -> clearAddresses() ;
    $mail -> clearAttachments();
  1. Write <?php to write php function and close tag with ?>
  2. Use require to import phpmailer classes
  3. Require to include autoload.php folder.
  4. Create variables for $subject and $post with the super global variable $_POST
  5. Now using $mail object with the new PHPMailer.
  6. $body with file_get_contents() with the html file name
  7. $mail object with isSMTP() file.
  8. $mail object to add Host as host email, SMTPAuth as true, SMTPkeepAlive to true and Port 25
  9. Also Set your username and password
  10. Use function subject and body to the variable $subject and $body
  11. Now connect it to mysql database using mysqli_connect().
  12. Create a foreach loop with try and catch statement. Into the try statement set row as ‘email and ‘fullname’. Catch statement used echo to invalid address. Then use continue statement
  13. Now creating another try and catch statement. Using $mail function with send() function
  14. Reset() the connection with getSMTPInstance()
  15. Now clear address and attachments.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to send html table in email body in php.

I hope this tutorial on how to send html table in email body in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪