PHP通过附件发送电子邮件(存储用户名)

时间:2012-08-15 05:22:25

标签: php email session-variables email-attachments

我(最后)完成了一个php脚本,用户可以上传文件并发送给我,但我似乎无法使用他/她已存储的电子邮件帐户进行FROM字段。电子邮件永远不会发送给我......

这就是我所拥有的。我在登录/注册时将用户的电子邮件存储为会话。

session_save_path('/home/users/web/...'); 
session_start();
$_SESSION['users'] = $email;

然后,我有一个用户页面,它会调用会话并显示信息为"以{$ _SESSION [' users']}"

登录

然后我为用户创建了一个表单,用于上传文件并向我发送电子邮件:

<form method="POST" action="testmail.php" enctype="multipart/form-data" class="form-vertical">
<input name="email" type="hidden" value="'.$_SESSION['users'].'" />

<span class="label label-info">Document Type</span><br>
<input type="text" name="project" class="input-medium"style="width: 350px; height: 30px;" /><br><br>

<span class="label label-info">Upload the Document</span><br><br>
<input type="file" name="attachment[]"><br><br>

<span class="label label-info">Brief Description of Document and Concern</span><br>
<textarea name="description" style="width: 550px; height: 200px;">...</textarea>

<input type="submit" class="btn btn-large btn-success">
</form>

但是,只有当我省略&#34;&#39;。$ _ SESSION [&#39;用户&#39;]的价值时,此表单才有效。&#39;&#34; (值=&#34; TEST&#34;,例如)。

这是电子邮件php脚本:

 <?php
    if( $_POST || $_FILES )
    {
            // email fields: to, from, subject, and so on
            // Here 
            $from = $_POST['email'];
            $to = "support@lawcontractor.com";
            $subject = $_POST['project'];
            $message = "This is the message body and to it I will append the attachments.";
            $headers = "From: $from";

            // boundary
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

            // headers for attachment
            $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

            // multipart boundary
            $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
            fixFilesArray($_FILES['attachment']);
            foreach ($_FILES['attachment'] as $position => $file) 
            {
                    // should output array with indices name, type, tmp_name, error, size
                    $message .= "--{$mime_boundary}\n";
                    $fp     = @fopen($file['tmp_name'],"rb");
                    $data   = @fread($fp,filesize($file['tmp_name']));
                    @fclose($fp);
                $data = chunk_split(base64_encode($data));
                $message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
            $message .= "--{$mime_boundary}--";
            $returnpath = "-f" . $from;
            $ok = @mail($to, $subject, $message, $headers, $returnpath);
            if($ok){ return 1; } else { return 0; }
    }
    //This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important

    function fixFilesArray(&$files)
    {
            $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);

            foreach ($files as $key => $part) {
                    // only deal with valid keys and multiple files
                    $key = (string) $key;
                    if (isset($names[$key]) && is_array($part)) {
                            foreach ($part as $position => $value) {
                                    $files[$position][$key] = $value;
                            }
                            // remove old key reference
                            unset($files[$key]);
                    }
            }
    }
    ?>

3 个答案:

答案 0 :(得分:0)

尝试更改以下行。

<input name="email" type="hidden" value="<?php echo $_SESSION['users'];?>" />

您必须设置电子邮件的值。

答案 1 :(得分:0)

试试这个:

<input name="email" type="hidden" value="<?php echo $_SESSION['users'] ?>" />

在您的代码中,您在获取<?php ?>的值时缺少$_SESSION['users']标记。 HTML不理解这一点,因此您需要将其放在<?php ?>标记和echo值内。

答案 2 :(得分:0)

我认为你应该做<input name="email" type="hidden" value="<?php echo $_SESSION['users']; ?>" />