Jump to content

PHP mail, include template, and pass variables.... help


olie04

Recommended Posts

Before, if I wanted to send a welcome letter when a user registers i'd do this.

 

$body = "<inserted html code>";
mail('email@email.com', '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@email.com', '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@email.com', 'Subject', $body, $headers);

 

Of course, this has the same effect as the include function:

 

$body = require("emailTemplate.inc.php");
mail('email@email.com', '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: from@email.com <email@email.com>' . "\r\n";

 

Thank you in advanced!

Link to comment
Share on other sites

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 <xxxxxxxx@xxxxxxx.com>' . "\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);
}

Link to comment
Share on other sites

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 <xxxxxxxx@xxxxxxx.com>' . "\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.

Link to comment
Share on other sites

  • 1 year later...

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.