How To Send HTML Table In Email Body In PHP
Last Updated : Mar 11, 2024
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"> <head> <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="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <center> <h1 style = "color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h3> how to send html table in email body in php </h3> </center> <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"/> </form> </body> </html>
- First, we write <! DOCTYPE html> to mention the version of HTML file
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Attach an external CSS file using <link> tag for styling the form.
- Thirdly, the <body> tag is used to define the webpage body.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Create an <form> of the action of the php file ‘send-mail.php’ with the method POST
- Create <label> and <input> for Email subject and email body
- Create a button for submit.
PHP Code:
<?php 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 ='smtp.gmail.com' ; $mail -> SMTPAuth = true ; $mail -> SMTPKeepAlive = true ; $mail -> Port = 25 ; $mail -> Username = 'yourusernamehere@example.com' ; $mail -> Password = 'yourpasswordhere' ; $mail -> setFrom('test@example.com', 'test Manager') ; $mail -> addReplyTo('test@example.com', '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 ( $mysql, "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(); } ?>
- Write <?php to write php function and close tag with ?>
- Use require to import phpmailer classes
- Require to include autoload.php folder.
- Create variables for $subject and $post with the super global variable $_POST
- Now using $mail object with the new PHPMailer.
- $body with file_get_contents() with the html file name
- $mail object with isSMTP() file.
- $mail object to add Host as host email, SMTPAuth as true, SMTPkeepAlive to true and Port 25
- Also Set your username and password
- Use function subject and body to the variable $subject and $body
- Now connect it to mysql database using mysqli_connect().
- 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
- Now creating another try and catch statement. Using $mail function with send() function
- Reset() the connection with getSMTPInstance()
- 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.