Send mail With HTML Template For PHP


Sending email from the script is a very useful and most used feature for the web application. The email functionality is used for many purposes – sending a welcome email on account registration, sending newsletter, contact or feedback form, etc. By sending an email from the script, the mail is sent dynamically to the recipient without manual interaction.


 <?php
$to = 'user@example.com'; 
$from = 'sender@example.com'; 
$fromName = 'SenderName'; 
 
$subject = "Send HTML Email in PHP by Dixit Patel"; 
 
$htmlContent = "<html>
   <head>
      <meta charset='utf-8'>
      <meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=0'>
   </head>
   <body>
      <div class='es-wrapper-color' style='padding-top: 5%;'>
         <table class='es-wrapper' width='100%' cellspacing='0' cellpadding='0'>
            <tbody>
               <tr>
                  <td class='esd-email-paddings' valign='top'>
                     <table cellpadding='0' cellspacing='0' class='es-header esd-header-popover' align='center'>
                        <tbody>
                           <tr>
                              <td class='esd-stripe' align='center' esd-custom-block-id='81780'>
                                 <table class='es-header-body' width='600' cellspacing='0' cellpadding='0' bgcolor='#ffffff' align='center'>
                                    <tbody>
                                       <tr>
                                          <td class='esd-structure es-p10t es-p10b es-p20r es-p20l' align='left' bgcolor='#ffffff' style='border: 1px solid #cccccc;'>
                                             <table cellpadding='0' width='100%' cellspacing='5' class='es-right' align=''>
                                                <tbody>
                                                   <tr>
                                                      <td width='100%' align='center' class='esd-block-text es-m-txt-c'>
                                                         <a href='' class='logo'>
                                                         <img src='logo.png' alt='Logo' width='60%'>
                                                         </a>
                                                      </td>
                                                   </tr>
                                                </tbody>
                                             </table>
                                          </td>
                                       </tr>
                                    </tbody>
                                 </table>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                     <table cellpadding='0' cellspacing='0' class='es-content' align='center' style='word-break: break-word; border: 1px solid #cccccc;'>
                        <tbody>
                           <tr>
                              <td class='esd-stripe' align='center' esd-custom-block-id='83913'>
                                 <table class='es-content-body' style='background-color: #ffffff;' width='600' cellspacing='0' cellpadding='0' bgcolor='#ffffff' align='center'>
                                    <tbody>
                                       <tr>
                                          <td class='esd-structure es-p40t es-p40b es-p20r es-p20l' align='left' bgcolor='#fafafa' style='background-color: #fafafa;'>
                                             <table cellpadding='0' cellspacing='0' width='100%'>
                                                <tbody>
                                                   <tr>
                                                      <td width='560' class='esd-container-frame' align='center' valign='top'>
                                                         <table cellpadding='0' cellspacing='0' width='100%'>
                                                            <tbody >
                                                               <tr>
                                                                  <td style='padding: 10px;' align='center' class='esd-block-text es-p20b'>
                                                                     <strong>
                                                                        <h2>Email verification</h2>
                                                                     </strong>
                                                                  </td>
                                                               </tr>
                                                               <tr>
                                                                  <td style='padding: 10px; font-size: 16px;' align='left' class='esd-block-text es-p20b'>
                                                                     <strong>Dear $name,</strong>
                                                                  </td>
                                                               </tr>
                                                               <tr>
                                                                  <td align='center' class='esd-block-text es-p20b'>
                                                                     <p style='font-size: 15px;'>Please click The following link For verifying and activation of your account</p>
                                                                     <a href='email_verification.php?code=$activationcode' class='btn btn-primary' target='_blank' style='    background-color: #00008b;border: 1px solid #00008b;cursor: pointer;color: #fff;display: inline-block;font-weight: 400;text-align: center;vertical-align: middle;padding: .375rem .75rem;font-size: 1rem;line-height: 1.5;border-radius: .25rem; text-decoration: none;'>Click Here</a>
                                                                  </td>
                                                               </tr>
                                                               <tr>
                                                                  <td style='padding: 10px; padding-top: 40px;' align='left' class='esd-block-text'>
                                                                     <p>
                                                                     <h3 style='color: #5c5956;'>
                                                                        Dixit Patel <br>
                                                                        <text style='color: #1c4792;'>Ahmedabad, Gujrat, India</text>
                                                                     </h3>
                                                                     </p>
                                                                  </td>
                                                               </tr>
                                                            </tbody>
                                                         </table>
                                                      </td>
                                                   </tr>
                                                </tbody>
                                             </table>
                                          </td>
                                       </tr>
                                    </tbody>
                                 </table>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                     <table cellpadding='0' cellspacing='0' class='es-footer' align='center'>
                        <tbody>
                           <tr>
                              <td class='esd-stripe' align='center' esd-custom-block-id='81788'>
                                 <table class='es-footer-body' width='600' cellspacing='0' cellpadding='0' bgcolor='#ffffff' align='center'>
                                    <tbody>
                                       <tr>
                                          <td class='esd-structure es-p20t es-p20b es-p20r es-p20l' >
                                             <table cellpadding='0' cellspacing='0' class='es-right' style='border: 1px solid #cccccc;'>
                                                <tbody>
                                                   <tr>
                                                      <td width='600' align='left' class='esd-container-frame'>
                                                         <table cellpadding='0' cellspacing='0' width='100%'>
                                                            <tbody>
                                                               <tr>
                                                                  <td align='right' class='esd-block-social es-m-txt-c es-p5t es-p5b' style='font-size:0'>
                                                                     <div class='social-wrapper' style='text-align: center;padding: 10px;'>
                                                                        <ul style='    padding-left: 0px;'>
                                                                           <li style='display: inline;margin: 0 5px;'>
                                                                              <a href='' target='_blank'>
                                                                              <img src='website.png' alt='Website Logo' class='web-icon' style='margin-top: .625em;width: 36px;height: 36px;border-radius: 25px;'></a>
                                                                           </li>
                                                                           <li style='display: inline;margin: 0 5px;'>
                                                                              <a href='' target='_blank'>
                                                                              <img src='twitter.png' alt='Twitter Logo' style='margin-top: .625em;width: 36px;height: 36px;border-radius: 25px;' class='twitter-icon'></a>
                                                                           </li>
                                                                           <li style='display: inline;margin: 0 5px;'>
                                                                              <a href='' target='_blank'>
                                                                              <img src='facebook.png' alt='Facebook Logo' style='margin-top: .625em;width: 36px;height: 36px;border-radius: 25px;' class='facebook-icon'></a>
                                                                           </li>
                                                                           <li style='display: inline;margin: 0 5px;'>
                                                                              <a href='' target='_blank'>
                                                                              <img src='youtube.png' alt='Youtube Logo' style='margin-top: .625em;width: 36px;height: 36px;border-radius: 25px;' class='youtube-icon'></a>
                                                                           </li>
                                                                           <li style='display: inline;margin: 0 5px;'>
                                                                              <a href='' target='_blank'>
                                                                              <img src='podcast.png' alt='Podcast Logo' style='margin-top: .625em;width: 36px;height: 36px;border-radius: 25px;' class='googleplus-icon'></a>
                                                                           </li>
                                                                           <li style='display: inline;margin: 0 5px;'>
                                                                              <a href='' target='_blank'>
                                                                              <img src='instagram.png' style='margin-top: .625em;width: 36px;height: 36px;border-radius: 25px;' class='instagram-icon'></a>
                                                                           </li>
                                                                        </ul>
                                                                     </div>
                                                                  </td>
                                                               </tr>
                                                               <tr>
                                                                  <td align='center' class='esd-block-text'>
                                                                     <p>Copyright © $current_year Dixit Patel</p>
                                                                  </td>
                                                               </tr>
                                                               <tr>
                                                                  <td align='center' class='esd-block-text'>
                                                                     <p style='font-size: 11px; color: #939598;'>If you don't want to receive this type of email in the future, please  <a style='font-size: 11px; color: #939598;' target='_blank' href='email_unsubscribe.php?code=$activationcode'> <u>unsubscribe</u></a>.</p>
                                                                  </td>
                                                               </tr>
                                                            </tbody>
                                                         </table>
                                                      </td>
                                                   </tr>
                                                </tbody>
                                             </table>
                                          </td>
                                       </tr>
                                    </tbody>
                                 </table>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                  </td>
               </tr>
            </tbody>
         </table>
      </div>
   </body>
</html>"; 
 
// Set content-type header for sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
 
// Additional headers 
$headers .= 'From: '.$fromName.'<'.$from.'>' . "\r\n"; 
$headers .= 'Cc: welcome@example.com' . "\r\n"; 
$headers .= 'Bcc: welcome2@example.com' . "\r\n"; 
 
// Send email 
if(mail($to, $subject, $htmlContent, $headers)){ 
    echo 'Email has sent successfully.'; 
}else{ 
   echo 'Email sending failed.'; 
}
?>

Post a Comment

0 Comments