Click to See Complete Forum and Search --> : Add a variable to email message


bpdp
05-06-2007, 07:31 PM
Hello,
I'm very new to PHP so bare with me. I have a flash application that allows a user to upload an image. on the flash side there is also a form that lets the user enter their name, email address and a message. this info gets emailed to me. Everything works but I would like to have the url for the image that they uploaded sent in the body of the email along with their message. I thought this would be easy. But I've had no luck. I feel like I've tried everything I know (which isn't all that much). Just wondering someone out there could help me out.

<?php
if ($_FILES['Filedata']['name']) {
$ran = rand();
$ran2 = $ran."_";
$uploadDir = "user_img/" . $ran2;
$uploadFile = $uploadDir . basename($_FILES['Filedata']['name']);
// replace any spaces in file name
$uploadFile = str_replace (' ', '', $uploadFile);
move_uploaded_file($_FILES['Filedata']['tmp_name'],$uploadFile);
echo " ";
}

// email
$sendTo = "email at someemail.com";
$subject = "My Email Subject";

$headers = "From: " . $_POST["name"];
$headers .= "<" . $_POST["email"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= "Return-Path: " . $_POST["email"];
$message = "Message: \r\n" . $_POST["message"];

mail($sendTo, $subject, $message, $headers);

?>

When I try something like:
$message = "Message: \r\n" . $_POST["message"] . $uploadFile;
I get an email with the url, but none of the other info (no subject, sendTo, headers, or the message)... the url is the only thing that is sent. I've tried a bunch of other things too - and I either get everything but the url, or nothing but the url... Please help me out - thanks!

hastx
05-06-2007, 08:16 PM
When I try something like:
$message = "Message: \r\n" . $_POST["message"] . $uploadFile;
I get an email with the url, but none of the other info (no subject, sendTo, headers, or the message)...


Try:

$message = "Message: \r\n" . $_POST["message"] . "\n\n";
$message .= "File URL: http://www.site.com/dir/$uploadFile";

bpdp
05-06-2007, 08:42 PM
I think I've tried that before because I remember getting this error. here is what is in the email body after that change:

Message:
"What ever I typed in the flash form"
File URL: http://www.myDomain.com/

I should have noted that I can concat a string to the message It just doesn't like the variable.

also I have tried to send the variable $uploadFile as the subject of the email which didn't work either. Arrgghh...

But thank you very much for the quick reply... any other ideas?

bpdp
05-06-2007, 09:19 PM
Okay I tried to send other variables:
$message .= "File URL: http://www.site.com/$sendTo";
&
$message .= "File URL: http://www.site.com/$subject";
work like a charm... but if i use any of the variables from the if statement like:
$message .= "File URL: http://www.site.com/$ran";
I get File URL: http://www.site.com/1400558858_ in the body, with none of the headers or anything else the user typed. am I missing something basic here?

hastx
05-06-2007, 10:08 PM
The problem is definately in the if...is that just for simple validation?

hastx
05-06-2007, 10:20 PM
You could do this:

<?php

$ran = rand();
$ran2 = $ran."_";
$uploadDir = "user_img/" . $ran2;
$uploadFile = $uploadDir . basename($_FILES['Filedata']['name']);
// replace any spaces in file name
$uploadFile = str_replace (' ', '', $uploadFile);
if (move_uploaded_file($_FILES['Filedata']['tmp_name'],$uploadFile)){
echo "file uploaded, going to email...";
}

// email
$sendTo = "dude@ranch.net";
$subject = "My Email Subject";

$headers = "From: " . $_POST["name"];
$headers .= "<" . $_POST["email"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= "Return-Path: " . $_POST["email"];
$message = "Message: \r\n" . $_POST["message"] . "\n\n";
$message .= "File URL: http://www.site.com/dir/$uploadFile";

mail($sendTo, $subject, $message, $headers);

?>

bpdp
05-06-2007, 11:04 PM
that worked "somewhat" this is what i got in the email body:
=========
Message:
My message
File URL: http://www.site.com/user_img/1402471845_
=========

the only problem is that the file name is not displayed and 1402471845_ is not the random number assigned to the file that was uploaded to the server. it should be:
File URL: http://www.site.com/user_img/1709671435_Russia.gif

as for why the if statement is needed... I have no idea, this is the first thing in PHP i've written. I used a couple different tutorials until I got something that worked for me. I just tried without the if statement and I didn't get any email, I also tried to move the email block of code right before:
if (move_uploaded_file($_FILES['Filedata']['tmp_name'],$uploadFile)){
echo "file uploaded, going to email...";
}
And I got the same error, where nothing but the URL shows up.

hastx
05-07-2007, 06:46 AM
I have tested the code I posted above on my own server, an it works properly...uploads the file, and emails the full URL with the message.

can you post your submission form? And have you verified that the script has appropriate permission to write to the "user_img" dir? The image is actually getting uploaded to the dir?

bpdp
05-07-2007, 08:50 AM
Wow hastx, you're really going above and beyond here and I thank you. I added a chmod 0777 for the directory and for the file in the php just to be sure. They are getting uploaded as I can navigate to them in by browser.

It's not pretty but my files are attached, I'll post the actual flash file in another post as it was a little too big. all you need to add is your your php file which should be named upload.php

Thanks again.

bpdp
05-07-2007, 08:52 AM
I'll post the actual flash file in another post as it was a little too big.

Thanks again.

Here's the flash file incase you need it.