简体   繁体   中英

Formatting HTML using PHP Mail()

I'm am sending an email via the php code below, which sends an email template to the users emails address. However the email sends with all the HTML tags still visible and not in effect.

$fh = fopen('templates/customer.eml','r');

        $emailContents = fread($fh, filesize('templates/customer.eml'));
        $emailContents = str_replace(":name:", $person->first . " " . $person->last, $emailContents);
        $emailContents = str_replace(":meeting_name:", $meeting->name, $emailContents);
        $emailContents = str_replace(":chair:", $chair->first . " " . $chair->last, $emailContents);

        fclose($fh);

        $header = "FROM: vra@vodafone.com\r\n";
        $header = "MIME-Version: 1.0\r\n";
        $header.="Content-type: text/html; charset: utf8\r\n";
        mail($person->email , "Meeting Request", $emailContents, "FROM: user@domain.com");

And below is the email template file that I am using for the content:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <p>
            Dear :name:,<br />
            You have been invited to <em>:meeting_name:</em> chaired by :chair:.
        </p>

        <p>
            Main text of the email will go here<sup>*</sup>.
        </p>
        <p>
            Thankfully
        </p>
        <p>
            User
        </p>
    </body>
</html>

You're not passing the headers into the mail function. Try:

$header = "FROM: vra@vodafone.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .="Content-type: text/html; charset: utf8\r\n";
mail($person->email , "Meeting Request", $emailContents, $header);

Try these headers instead

$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf8' . "\r\n";
$header .= 'From: vra@vodafone.com' . "\r\n";
mail($person->email , "Meeting Request", $emailContents, $header);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM