Sending HTML email using PHP: including the HTML file

12,336

Update for updated question:

If you don't want to include all that HTML in your PHP code, then yes file_get_contents() is a fine alternative.


You need to include the content type, and MIME version in the headers. This is charset UTF-8 also.

$headers = "From: $from <$from_email>\r\n". 
           "MIME-Version: 1.0" . "\r\n" . 
           "Content-type: text/html; charset=UTF-8" . "\r\n";

http://php.net/manual/en/function.mail.php

Share:
12,336
sumit
Author by

sumit

Updated on August 09, 2022

Comments

  • sumit
    sumit over 1 year

    I am trying to send email using PHP. The email is an HTML email with hundreds of line of HTML code (in file email.html). I don't want to write the whole HTML code in the PHP. How can I do it? Is my below method correct? Any better alternative?

    My Code (PHP):

    $email_text = file_get_contents('email.html');
    $headers = "From:[email protected]";
    
    mail('[email protected]', 'Hello', $email_text, $headers);
    

    My Email File (email.html):

    <html>
         <body>
              <a href="http://google.com">Hello</a>
         </body>
    </html>
    

    My problem: I want to send an HTML email which will display HTML content. The HTML content is taken from a file called email.html.

    Note 1: I have simplified my above code just for the clarity. Original code is having headers which will display HTML code correctly. Also the original HTML file is having hundreds of line and CSS styling. I have mentioned only few lines just to simplify.

    Note 2: There are many duplicates of sending email using PHP but I couldn't find how to include a big HTML file in just one line of PHP code.