Jump to content

[SOLVED] How to include the contents of htm files to an email


ACertainTrigger

Recommended Posts

Hey all, newbie to the forum here  ;D

 

I've got a set of htm files which I use as includes on some of my pages, but I want to be able to use them when sending an email but I can't see a way how.

 

I've been trying to assign them to my $message for sending in the mail() but I can't see a way of doing this. Is there a better way?

 

Any help appreciated :)

 

Thanks

Link to comment
Share on other sites

I don't mind which, as long as they display as the email.

 

I'm writing a test, and the different results are include files, so at the moment I have the results page include the relevant htm file for the results that fit. I want the user to also recieve an email with the results, but as the results are stored as htm includes I'm not sure how to get them in the body of the email.

 

I don't want the whole email to be the htm file, just a part of it, as I want the first part to be "Dear $name" etc. Hence why i was trying to set it all as a variable. Any ideas?

 

Thanks :)

ACT

Link to comment
Share on other sites

I want to send an html email, where a part of the body is the contents of a results.inc.htm

 

I know this doesn't work, but something like this just so you can see the logic

 

$message = "<p>Dear $name</p> <p>Thanks for taking the test, here are your results:</p>"

$message .= include('yourResults.inc.htm');

 

It would be more complex than that because the include would differ depending on the results, but hopefully this makes sense  :)

Link to comment
Share on other sites

Ah! I was going to post this:

$header='MIME-Version: 1.0'."\r\n".'Content-type: text/html; charset=iso-8859-1'."\r\nFrom: Someone <someone@somewhere.net>\r\n";
mail($recipient,$subject,$message,$header)

That's to send your email as an HTML file, basically allowing you to send an entire web page as an email.

 

I think I understand what you are requesting now :)

 

Let's say you have the following data:

Recipient's name is stored as $username

Recipient's email is stored as $useremail

Recipient's address is stored as $useraddr

Recipient's age is stored as $userage

 

Have a play with this and see how you get on.

  $header="From: Someone <someone@somewhere.net>\r\n";
 $subject="Please verify your address";
 $message="Dear $username,\n\nI see your age is $userage and your address is:\n$useraddr\n\nPlease let us know if these details are incorrect.";
mail($useremail,$subject,$message,$header)

Link to comment
Share on other sites

You will need to set the content type of mail in header like in the code below.

 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 

Now just add html content to an string, and this string will be message that will be sent with the mail.

Link to comment
Share on other sites

You will need to set the content type of mail in header like in the code below.

 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 

Now just add html content to an string, and this string will be message that will be sent with the mail.

 

Just like what I "was" going to post.

Link to comment
Share on other sites

You will need to set the content type of mail in header like in the code below.

 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 

Now just add html content to an string, and this string will be message that will be sent with the mail.

 

How do I add the html content (which is inside a testResults.inc.htm) to a string? This is the bit I can't fathom. I've read something about output buffering, is this the right way?

Link to comment
Share on other sites

ACertainTrigger, posting some code will help otherwise how are myself or skali going to know?

Sorry guys, this is where I'm up to so far but it doesn't work.

//email the user
if ($emailSend == "yes") {
$headers = "From: ". $emailFrom . "\r\n" .
		"Reply-To: $email" . "\r\n" .
		'X-Mailer: PHP/' . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$to = $_POST['email'];

$subject = "Your Results";

$message  = "<h1>Your Results</h1>";
$message .= include('profiles/pca' . $pcatype.'.inc.htm');
mail($to,$subject,$message,$headers);
};

I hope this makes a little more sense :)

Link to comment
Share on other sites

$headers='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers.="From: ACertainTrigger <".$emailFrom.">\r\n";

Try changing your headers line to that - personally I'm not sure about how you're including the HTML file into the email as I must admit I've never tried using include() like that!

Link to comment
Share on other sites

Thanks for your help everyone, I've managed to figure it out. I needed to use ob_start() to add the contents of the file to my $message variable. Here's the working code:

<?php
//email the user
if ($emailSend == "yes") {
$headers = "From: ". $emailFrom . "\r\n" .
		"Reply-To: $email" . "\r\n" .
		'X-Mailer: PHP/' . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$to = $_POST['email'];

$subject = "Your Results";

//Construct the contents of the email
ob_start();
?>
<p>Dear<?=$name?>,</p>
<p>Many thanks for taking the test, here are your results.</p>
<h1>Results for <?=$pcatype?></h1>
<?php include('profiles/pca' . $pcatype.'.inc.htm');?>
<p>Many thanks for taking the test</p>

<?php
//Get the contents of the output buffer, and assign it to $message
$message = ob_get_clean();
//Send the message
mail($to,$subject,$message,$headers);
};?>

 

With this, I can make the contents of an include file a part of a variable and in turn use it in an email

Link to comment
Share on other sites

Glad you sorted it, just been playing with some sample code myself and found include() does work when used like that but found what I was trying to include appeared before where I was including it - I never knew about ob_start()

 

I just learnt something new myself ;)

Same here :) Seems to work well!

Thanks again for your help!  ;D

act

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.