Including file in mail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Including file in mail

Post by Zeceer »

I got a document before the actual mail script. This document have two hidden variables.
<input name="username" type="hidden" value="<?=$username?>">
<input name="password" type="hidden" value="<?=$password?>">
These variables are sent with the document to this file:
<?php
$to = "zeceer@hotmail.com";
$subject = "Order";
$headers = "zeceer@hotmail.com";

$body ="

Navn: ".$_POST['navn']."
Post adresse: ".$_POST['postadresse']."
Postnummer: ".$_POST['postnummer']."
Sted: ".$_POST['sted']."
Telefon: ".$_POST['telefon']."
E-mail: ".$_POST['email'];

mail($to,$subject,$body,"From: $headers");

header( "Location: index.php" );
?>
The two variables that are passed is set together so they match a file.
include(users/'.$_POST[username].$_POST[password].'.php');
What i need i something that includes this file in the $body variable of the mail script. So that the body of the mail is that file. Or whats inside the file is printet in the mail. If you know what mean :wink: .

A stupid example just to show what I mean :D :
<?php
$to = "zeceer@hotmail.com";
$subject = "Order";
$headers = "zeceer@hotmail.com";

$body ="

Navn: ".$_POST['navn']."
Post adresse: ".$_POST['postadresse']."
Postnummer: ".$_POST['postnummer']."
Sted: ".$_POST['sted']."
Telefon: ".$_POST['telefon']."
E-mail: ".$_POST['email']

include(users/'.$_POST[username].$_POST[password].'.php');

;

mail($to,$subject,$body,"From: $headers");

header( "Location: index.php" );
?>
The thing is that this file contains information about the user that I need
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Why not use fopen and fread?

Code: Select all

&lt;?php
$fp = fopen("users/{$_POST&#1111;username]}.{$_POST&#1111;password]}.php");
$content = fread($fp,filesize("users/{$_POST&#1111;username]}.{$_POST&#1111;password]}.php"));
fclose($fp);

$body .= $fp;
?&gt;
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Tried your example. Didn't make it work, but it leaded me in the right direction :D .

I made this:
<?php

$to = "zeceer@hotmail.com";
$subject = "Order";
$headers = "zeceer@hotmail.com";

$fp = fopen('brukere/'.$_POST['brukernavn'].$_POST['passord'].'.php', "r");

$body = fpassthru( $fp );

mail($to,$subject,$body,"From: $headers");

?>
It worked 50% :roll: . It prints the file to the browser and everyhing looks fine untill I get the mail. The mail only says "1622" for some reason. What I need is something that put's the text inside of the file in the body of the mail :wink:
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Anyone?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

fpassthru returnes the number of characters read, so thats why your email's body was 1622. fpassthru displays the file to screen as a side effect which is why it looked good on the browser.

Modifiying Takuma's version

Code: Select all

&lt;?php
$fp = fopen("users/{$_POST&#1111;username]}.{$_POST&#1111;password]}.php"); 
$content = fread($fp,filesize("users/{$_POST&#1111;username]}.{$_POST&#1111;password]}.php")); 
fclose($fp); 

$body .= $content; // not $fp! 
?&gt;
This should work. If it doesn't tell us why and we can take it from there.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Why is this error popping up?
Parse error: parse error, unexpected T_VARIABLE in D:\inc shop\tilkassen3.php on line 4
Heres the script:
<?php


$fp = fopen("brukere/".$_POST['brukernavn']$_POST['passord'].".php");
$content = fread($fp,filesize("brukere/".$_POST['brukernavn']$_POST['passord'].".php"));
fclose($fp);

$body = "$content";

print("$body");


?>
[/quote]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need to add a period (.) between your variables in order to concenate them:

Code: Select all

$_POST&#1111;'brukernavn'].$_POST&#1111;'passord']
instead of

Code: Select all

$_POST&#1111;'brukernavn']$_POST&#1111;'passord']
Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

I had it that way before, but then I get this pretty lady 8O :
Warning: Wrong parameter count for fopen() in D:\inc shop\tilkassen3.php on line 4

Warning: fread(): supplied argument is not a valid File-Handle resource in D:\inc shop\tilkassen3.php on line 5

Warning: fclose(): supplied argument is not a valid File-Handle resource in D:\inc shop\tilkassen3.php on line 6
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

fopen() requires two parameters to be passed to it, the filename and the mode which should be used to open the file. More info about the choice of mode is available here:
http://www.php.net/manual/en/function.fopen.php

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Stupid ME! Done that mistake before :D . Thanx!
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

THIS HAS NOTHING TO DO WITH THE POST ABOVE

Post by Zeceer »

When I send this to a hotmail account it works great. It displays the text as HTML. But by sending it to some other account it only display the HTML codes as basic text. What I want is to display the mail as HTML. In other mail programs than Hotmail. Is there i code I must include so the mail client displays it as HTML??
<br>
Navn: gg
<br>
Post adresse: dg
<br>
Postnummer: dgq
<br>
Sted: fgdg
<br>
Telefon: gdfgws
<br>
E-mail: grg
<html>
<body>
<table width="650" border="0" cellspacing="0" align="center"
bgcolor="#ffffff"><tr><td width="200" valign="left"><font color="#000000" size="1"
face="Verdana, Arial, Helvetica, sans-serif">Whey Tech 3000g</font></td><td
width="200" align="right"><font color="#000000" size="1" face="Verdana, Arial,
Helvetica, sans-serif"><strong>1</strong></font></td><td width="200"
align="right"><font color="#000000" size="1" face="Verdana, Arial, Helvetica,
sans-serif"><strong>729,-</strong></font></td></tr></table><br><?php $pris = $pris +
"729"; ?>
<table width="650" border="0" cellspacing="0" align="center"
bgcolor="#ffffff"><tr><td width="200" valign="left"><font color="#000000" size="1"
face="Verdana, Arial, Helvetica, sans-serif">Whey Tech 1500g</font></td><td
width="200" align="right"><font color="#000000" size="1" face="Verdana, Arial,
Helvetica, sans-serif"><strong>1</strong></font></td><td width="200"
align="right"><font color="#000000" size="1" face="Verdana, Arial, Helvetica,
sans-serif"><strong>429,-</strong></font></td></tr></table><br><?php $pris = $pris +
"429"; ?>
<table width="650" border="0" cellspacing="0" align="center"
bgcolor="#ffffff"><tr><td width="200" valign="left"><font color="#000000" size="1"
face="Verdana, Arial, Helvetica, sans-serif">Whey Tech 750g</font></td><td
width="200" align="right"><font color="#000000" size="1" face="Verdana, Arial,
Helvetica, sans-serif"><strong>1</strong></font></td><td width="200"
align="right"><font color="#000000" size="1" face="Verdana, Arial, Helvetica,
sans-serif"><strong>279,-</strong></font></td></tr></table><br><?php $pris = $pris +
"279"; ?>

</body>
</html>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you put in all the headers (content type and MIME) needed for HTML mail as given on this page http://www.php.net/manual/en/function.mail.php?

(Check out Example 4: Sending Complex Email)

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Thanx, that worked fine. The only problem is that i can't set who the mail is from. I've tried some solutions but didn't get it.

Heres my mail() line.
mail($to,$subject,$body,"Content-type: text/html; charset=iso-8859-1\r\n");
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can add a from line in your headers (check the docs on the mail function for what it should look like) but that may not work depending on whether you are using windows or *nix but the manual page will explain more.

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

I tried that, but didn't get it. Gonna read the whole manual and then try.
Post Reply