【问题标题】:Converting a html generating php script into a automatic email script将生成 html 的 php 脚本转换为自动电子邮件脚本
【发布时间】:2011-01-29 19:29:41
【问题描述】:

我得到了这个从图片上传器创建 HTML 页面的脚本,唯一的问题是它在每次上传时都会覆盖它自己,我想更改它以便我收到一封电子邮件。

想法?

 <?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   ?>  

我刚刚添加了这个:

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   ?>  

【问题讨论】:

  • 您要发送什么电子邮件?文件已写入的消息?图片本身?

标签: php scripting email


【解决方案1】:

我了解,您不想将 HTML 保存到服务器,而是希望将其作为电子邮件发送到某个地方。这就是你要的?如果没有,请编辑/评论您的问题以阐明您的需求。

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

负责将文件写入服务器的文件系统,可能会替换某些内容。如果您不想将 HTML 保存为服务器上的文件,则只需删除该块(删除或注释掉它)。

到那时,您已经在 $result 变量上生成了 HTML(如果您仔细观察,这就是原始代码保存到文件中的内容);所以如果你想通过邮件发送它,你已经有了你的身体。找出“发件人”、“收件人”、“抄送”(如果有)和“密送”(如果有)地址,以及邮件的主题。 “来自”通常作为文字或常量,但也可能是 POSTed 表单的输入字段。 “收件人”地址取决于您要发送邮件的位置。然后使用这样的东西来实际邮寄它:

$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);

查看http://ie2.php.net/manual/en/function.mail.php 上的mail() 文档,了解有关mail() 函数的更多详细信息。 请注意,在这种情况下,您需要定义至少 3 个标头:必须始终指定“发件人”(某些服务器端邮件应用程序可能具有默认的“发件人”地址,但始终建议您脚踏实地)。 “MIME-Version”和“Content-type”标头用于确保邮件以 HTML 而不是文本形式发送。您可能想要添加“Reply-to”、“CC”、“BCC”和其他标题,具体取决于您的需要:在这种情况下,只需将它们附加到 $headers 变量中,用“\r\n”分隔,在调用 mail() 之前。

希望这会有所帮助。

【讨论】:

  • 这是你的时间......你理解我的问题,结果是有效的。电子邮件花了一点时间才送达……但它确实送达了。
  • 我刚刚更改了电子邮件地址,突然间 ?> 不再是红色并且无法正常工作...任何想法如何正确关闭它? Michael 我编辑了问题以显示插件的定位。
  • 您是否只是按照更新后的帖子的建议附加了代码块?首先,这会留下“保存到文件”部分(只要它是您想要的就可以),但更关键的是:在第 134 行,您已经有一个结束“?>”:如果您希望之后的代码是作为 PHP 处理,您必须删除它或添加一个新的 "
  • 我的外发电子邮件都是一个奇怪的字符串,当我将其留空时,它会被填写,并且 $headers 的格式不正确,我正在处理它,但它现在正在上传和发送电子邮件。是的!谢谢大家。
猜你喜欢
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多