简体   繁体   中英

Include order confirmation email into PHP mail function

I need to produce an order confirmation email on PHP. I have a php file that contains the confirmation email (since it has some variables that should be printed when loaded in the main php processing the order. It looks like this:

**orderConf.php**
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
</body>
Dear <?php echo $firstName." ".$lastName; ?> .....
.....
</body></html>

Then in the main php that processes the order, I have the mail function in which I place this variable: orderProcessing.php

$message = include ("orderConf.php");

Would this be the right way to do it? Or should I compose my confirmation email in a different way?

Thanks

This is one of few cases where HEREDOC is all right

<?php
$message - <<<HERE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
</body>
Dear $firstName $lastName
.....
</body></html>
HERE;

then just

 include ("orderConf.php");

and have your $message variable.

another option would be using output buffering .

This way you would just output the contents of the orderConf.php. The message should be returned by this file.

<?php
return <<<MSG <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
</body>
Dear <?php echo $firstName." ".$lastName; ?> .....
.....
</body></html>
MSG;

Or you could use the ob_ functions.

<?php
ob_start();
include('orderConif.php');
$message = ob_get_contents();
ob_end_clean();

You can't include a file into a variable like that. You would have to use file_get_contents(). However IMO that isn't the best way to do it. Instead what you should do is load your message into a variable and then use the same variable for sending an e-mail. Example below:

$body = '<div>Dear' . $firstName . ' ' . $lastName . '... rest of your message</div>';

Make sure to use inline styling in $body. Tables would probably be a good idea as well, as they work better in e-mails.

Then all you do is use:

$to = recepients address;
$subject = subject;
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><body>' . $body . '</body></html>', $headers);

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