我(最终)完成了一个 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>
但是,这种形式仅在我省略 "'.$_SESSION['users'].'" 的值时才有效(例如,value="TEST")。
这是电子邮件 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]);
}
}
}
?>