views:

119

answers:

1

I got this script for creating an HTML page from an image uploader, The only problem is that it overwrite's itself on every upload, I would like to change it so that I get sent an e-mail instead.

Ideas?

 <?php

$destination_dir = "uploaded/";
$targetPath = dirname($_SERVER['SCRIPT_URI']) . "/";

$html_start = "
<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">

<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<title>Upload results</title>
</head>
<body>
";

$html_end = "
</body>
</html>
";

// Check if there are AdditionalStringVariable
$result = "AdditionalStringVariable: " . $_POST["AdditionalStringVariable"];
$result .= "<br>";


// Process value of QIU_thumbnails_Imagedata field, this is JPEG-files array of generated thumbnails
if($_FILES[QIU_thumbnails_Imagedata])
{
foreach ($_FILES[QIU_thumbnails_Imagedata][name] as $key => $value) 
{
    $uploadfile = $destination_dir . basename($_FILES[QIU_thumbnails_Imagedata][name][$key]);


    if (move_uploaded_file($_FILES['QIU_thumbnails_Imagedata']['tmp_name'][$key], $uploadfile)) 
    {

        $big_image_name = $_FILES[Imagedata][name][$key];

        $result .= "<a href='" .$big_image_name. "'>" . "<img border = '0' src='".$value . "'/></a><br><br>";
    }
}
}
//
$result .= "<br>";


// Process value of Imagedata field, this is JPEG-files array

foreach ($_FILES[Imagedata][name] as $key => $value) 
{
$uploadfile = $destination_dir . basename($_FILES[Imagedata][name][$key]);

if (move_uploaded_file($_FILES['Imagedata']['tmp_name'][$key], $uploadfile)) 
{
    $result .= "File uploaded: <a href='".  $value . "'>" . $value . "</a><br>";
}
}


//
$result .= "<br>";




//
// Process  GlobalControlData field, this is the array of serialized data for Global controls 
// the value for each control is: id|value
if($_POST[GlobalControlData])
    {
    foreach ($_POST[GlobalControlData] as $key => $value) 
{
    $globalControlExploded =  explode("|", $value);
    $result .= "\n" . "GlobalControlData:\n\t" . $globalControlExploded[0] ."\t:\t" . $globalControlExploded[1] . "<br>";
}
}

//
// Process LocalControlData  field, this is the array of serialized data for Local controls 
// value for each image is: image||id1|value1^id2|value2^id3|value3, where image - is picture name, id - is unique control ID , and a value - control value
if($_POST[LocalControlData])
{
foreach ($_POST[LocalControlData] as $key => $value) 
{
    $exploded = explode("||", $value);
    $parentFile = $exploded[0];

    $result .= "<br>" . $exploded[0] . "<br>";

    $explodedToControls = explode("^", $exploded[1]);

    foreach ($explodedToControls as $cnt => $val) 
    {
        $eachControl = explode("|", $val);
        $result .= "\tcontrol:\t" . $eachControl[0] . ", value:\t" . $eachControl[1] . "<br>";

    }
    //
}
}
//

$result = $html_start . $result . $html_end;

//
if(@$fp = fopen($destination_dir.'index.html', 'w')) {
      fwrite($fp, $result);
      fclose($fp);
}

132    echo $targetPath . $destination_dir;  
133  
134   ?>  

I just added this:

135 
136    $to = '[email protected]';
137    $subject = 'Baublet Order Received';
138    $headers = 'From: [email protected] '. "\r\n" .
139           'MIME-Version: 1.0' . "\r\n" .
140    'Content-type: text/html; charset=utf-8' . "\r\n";
141    mail($to, $subject, $result, $headers");
142
143   ?>  
+1  A: 

I understand that, instead of saving the HTML to the server, you want to send it as an e-mail somewhere. It this what you're asking for? If not, please edit/comment your question to clarify what you need.

The block

if(@$fp = fopen($destination_dir.'index.html', 'w')) {
      fwrite($fp, $result);
      fclose($fp);
}

takes care of writting the file in the server's filesystem, potentially replacing something. If you don't want to save the HTML as a file on the server, you just need to get rid of that block (delete it or comment it out).

By that point you already have the generated HTML on the $result variable (if you take a closer look, that's what the original code is saving to the file); so if you want to send it by mail, you already have your body. Figure out the "from", "to", "CC" (if any), and "BCC" (if any) addresses, as well as the subject for your mail. The "from" one often goes as a literal or constant, but may also be an input field from the POSTed form. The "to" address depends on where do you mean to send the mail. Then use something like this for actually mailing it:

$to = "here goes the destination address";
$subject = "here you put the subject line for the e-mail";
$headers = "From: " . $whatever_your_sender_address_is . "\r\n" .
           "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
mail($to, $subject, $result, $headers);

Take a look at mail()'s documentation on http://ie2.php.net/manual/en/function.mail.php for further details about the mail() function. Note that in this case you'll need to define at least 3 headers: "From" must always be specified (some server-side mail apps may have a default "from" address, but it's always advisable to step on solid ground). The "MIME-Version" and "Content-type" headers are to ensure that the mail is sent as HTML, rather than as text. You might want to add "Reply-to", "CC", "BCC", and other headers, depending on your needs: in such case, just append them to the $headers variable, separated with "\r\n", before the call to mail().

Hope this helps.

herenvardo
Thats for your time...you understood my question and the result is working. It took a little time for the email to arrive...but it did.
Michael Robinson
I just changed the email address and all of a sudden the ?> is no long red and it's not working...any ideas how to close this out correctly?MichaelI edited the question to show positioning on the add-on.
Michael Robinson
Did you just append the code block as your updated post suggests? First, this leaves the "save to file" part in there (that's fine as long as it's what you want), but more critically: on line 134 you already have a closing "?>": if you want code after that to be processed as PHP, you have to remove that or add a new "<?php" opener. In that case, all you need is to remove line 134 entirely, and things should work.Also, you might want to check for missmatched quotes, brackets, and the like: these issues can be quite a pain and often go unnoticed.
herenvardo
both my outgoing email was a weird string, when I left it blank it filled in and the $headers weren't formed correctly, I'm working on that but it's uploading and emailing now. Yeah! Thanks everyone.
Michael Robinson