olie04 Posted December 14, 2010 Share Posted December 14, 2010 Before, if I wanted to send a welcome letter when a user registers i'd do this. $body = "<inserted html code>"; mail('[email protected]', 'Subject', $body, $headers); Now that I am advancing in PHP, I was trying a different method, because when I did the above method, I would have to take all the HTML out of the variable to change something, like an image link, etc... I have tried doing another method, where I can actually leave all the HTML code in a separate file, so that I can easily edit it, and tried 3 things... This one works, but it doesn't pass variables: $body = file_get_contents("emailTemplate.inc.php"); mail('[email protected]', 'Subject', $body, $headers); When I do this one, the page actually just shows on the current page (naturally): $body = include("emailTemplate.inc.php"); mail('[email protected]', 'Subject', $body, $headers); Of course, this has the same effect as the include function: $body = require("emailTemplate.inc.php"); mail('[email protected]', 'Subject', $body, $headers); So my question is, how do I send a formatted HTML email, and pass variables to it, so I can personalize it by the persons name, etc, without adding the HTML to a variable, and so that I can easily update the page whenever I want, and without taking it out of the variable, and sticking back in the edited code? Any help would be appreciated! I would also like to say that I am using this code for the $headers variable too: $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: [email protected] <[email protected]>' . "\r\n"; Thank you in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/221569-php-mail-include-template-and-pass-variables-help/ Share on other sites More sharing options...
kfarrar Posted December 14, 2010 Share Posted December 14, 2010 This might be what you had in mind $sql = "SELECT addy FROM notification_recipient"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res) ) { $area .= $row['addy']. ", "; } // read the list of emails from the file. $email_list = explode(',', $area); // count how many emails there are. $total_emails = count($email_list); // go through the list and trim off the newline character. for ($counter=0; $counter<$total_emails; $counter++) { $email_list[$counter] = trim($email_list[$counter]); } $send_to = $area; $up_subject = ''. $equip .' is back '. $status . ''; $down_subject = ''. $equip .' is '. $status . ''; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: xxx <[email protected]>' . "\r\n"; $down_message = '<html> <head> <title></title> </head> <body> <table> <tr> <td> <br>REASON: ' . $reason . ' <br>CAUSE: ' . $cause . ' <br>REPAIR TIME: ' . $repair_time . ' <br>REMEDY: ' . $remedy . ' <br>MECHANIC: ' . $name . '<br> </td> </tr> </table> </body> </html>'; if ($status == "down") { echo $down_message_success; mail($send_to, $down_subject, $down_message, $headers); } Quote Link to comment https://forums.phpfreaks.com/topic/221569-php-mail-include-template-and-pass-variables-help/#findComment-1146959 Share on other sites More sharing options...
olie04 Posted December 14, 2010 Author Share Posted December 14, 2010 This might be what you had in mind $sql = "SELECT addy FROM notification_recipient"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res) ) { $area .= $row['addy']. ", "; } // read the list of emails from the file. $email_list = explode(',', $area); // count how many emails there are. $total_emails = count($email_list); // go through the list and trim off the newline character. for ($counter=0; $counter<$total_emails; $counter++) { $email_list[$counter] = trim($email_list[$counter]); } $send_to = $area; $up_subject = ''. $equip .' is back '. $status . ''; $down_subject = ''. $equip .' is '. $status . ''; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: xxx <[email protected]>' . "\r\n"; $down_message = '<html> <head> <title></title> </head> <body> <table> <tr> <td> <br>REASON: ' . $reason . ' <br>CAUSE: ' . $cause . ' <br>REPAIR TIME: ' . $repair_time . ' <br>REMEDY: ' . $remedy . ' <br>MECHANIC: ' . $name . '<br> </td> </tr> </table> </body> </html>'; if ($status == "down") { echo $down_message_success; mail($send_to, $down_subject, $down_message, $headers); } Yeah, but see what I mean with the HTML being in the variable? I was looking for a solution to where I can just call an external .php document as an include, or a require, and use THAT as the message body, instead of copying all the html, and sticking it inside a variable. I want to do this just in case I want to edit the HTML code, I have to take it out of the variable, put it in a new HTML document, and then stick it back into the variable again, to my understanding, it doesn't look like it can be done, and if I have to just stick the HTML inside a variable, I guess that's the only solution. Quote Link to comment https://forums.phpfreaks.com/topic/221569-php-mail-include-template-and-pass-variables-help/#findComment-1147021 Share on other sites More sharing options...
VIDesignz Posted February 9, 2012 Share Posted February 9, 2012 Say this is is your email_template.php... "Welcome $name , Thank you for registering! You registered on $date" Here would be your php mail file... $name = 'Users Name'; $date = 'Todays Date'; //Get your email template $message = file_get_contents('/email_template.php'); //Set the variables you want to find $find = array('$name', '$date'); //Declare the variable values you want to replace the first variables with $replace = array($name, $date); //Call the str_replace function $message = str_replace($find, $replace, $message); Quote Link to comment https://forums.phpfreaks.com/topic/221569-php-mail-include-template-and-pass-variables-help/#findComment-1316301 Share on other sites More sharing options...
olie04 Posted February 10, 2012 Author Share Posted February 10, 2012 Say this is is your email_template.php... "Welcome $name , Thank you for registering! You registered on $date" Here would be your php mail file... $name = 'Users Name'; $date = 'Todays Date'; //Get your email template $message = file_get_contents('/email_template.php'); //Set the variables you want to find $find = array('$name', '$date'); //Declare the variable values you want to replace the first variables with $replace = array($name, $date); //Call the str_replace function $message = str_replace($find, $replace, $message); Thanks for your reply. I know this topic is old, but I definitely like your solution. I will keep this snippet for future projects. Seems like a great way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/221569-php-mail-include-template-and-pass-variables-help/#findComment-1316451 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.