Sending HTML Email Using PHP: Including an HTML File

I am trying to send an email using PHP. This is an email with hundreds of lines of HTML code (in the email.html file). I do not want to write all the HTML in PHP. How can I do it? Is my method below correct? Any better alternative?

My code (PHP):

$email_text = file_get_contents('email.html'); $headers = "From: def@gmail.com "; mail(' abc@gmail.com ', '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 letter in which the HTML content will be displayed. HTML content is taken from a file called email.html.

Note 1: I simplified the code above for clarity only. The source code has headers that will display the HTML correctly. Also, the source HTML file contains hundreds of lines and CSS styles. I just mentioned just a few lines to simplify.

Note 2: There are many duplicates of sending emails using PHP, but I could not find how to include a large HTML file in just one line of PHP code.

+7
source share
1 answer

Update updated question:

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


You need to specify the content type and version of MIME in the headers. It is also UTF-8 encoded.

 $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

+4
source

All Articles