Converting a html generating php script into a automatic email script
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($_F开发者_运维技巧ILES[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 = 'michael.robinson@mac.com';
137 $subject = 'Baublet Order Received';
138 $headers = 'From: orders@baublet.com '. "\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 ?>
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.
精彩评论