【发布时间】:2022-01-20 04:04:50
【问题描述】:
早安,
我的目标是当客户上传他的文件时,它将被发送到我们的 Gmail,并附上文件。文件不应发送到我们的服务器,以防止可能的病毒。
我还参考了this website 以了解如何将文件发送到电子邮件。网站在用户上传后从服务器获取文件,但我们不能这样做。我们需要将文件直接发送到电子邮件。
我修改了代码,但是当我上传文件并单击“上传”时没有任何反应。我还在学习 html 和 php,希望有人能指导我了解我的代码有什么问题。请耐心等待我。
这部分代码从数据库中获取用户名和手机(我需要将其转发到电子邮件)
if(isset($valid_user_id) && !empty($valid_user_id))
{
$sql = "SELECT sno,emailid,fname,lname,mobile,address,city,state,country,status FROM member WHERE emailid='$valid_user_id'";
.... ... 这是要求用户上传文件的表单
<form method="post" name="uploadproof" id="uploadproof" enctype="multipart/form-data">
<input type="hidden" id="wrap" name="wrap" value="upload" />
<input type="hidden" id="userid" name="userid" value="<?php echo $valid_user_id; ?>" />
<input type="file" id="images" name="images[]" multiple="multiple" accept=".png,.jpg,.jpeg"/>
<input type="submit" id="upload" name="upload" class="send" value="Upload" style="float: none;padding:10px;" />
<span id="load"></span>
<br />
</form>
这部分代码是将附件发送到Gmail的php
$postData = $uploadedFile = $statusMsg = '';
$statusMsg = 'HELLO';
$msgClass = 'errordiv';
if(isset($_POST['upload'])){
// Get the submitted form data
$postData = $_POST;
$email = $row[emailid];
$name = $row[fname];
$mobile = $row[mobile];
$country = $row[country];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Upload attachment file
if(!empty($_FILES["images"]["name"])){
//Get the uploaded file information
$fileName = basename($_FILES["images"]["name"]);
echo $fileName;
//get the file extension of the file
$fileType = substr($fileName, strrpos($fileName, '.') + 1);
//get file size
$fileSize = $_FILES["images"]["size"]/1024; //size in KBs
// Allow certain file formats
$allowTypes = array('jpg', 'png', 'jpeg');
if(in_array($fileType, $allowTypes)){
$uploadStatus = 1;
}else{
$uploadStatus = 0;
$statusMsg = 'Sorry, only JPG, JPEG, & PNG files are allowed to upload.';
}
}
if($uploadStatus == 1){
// Recipient
$toEmail = 'Operation@gmail.com';
// Sender
$from = 'help@ecz.com';
$fromName = 'ECZ Members KYC';
// Subject
$emailSubject = 'KYC Request Submitted by '.$name;
// Message
$htmlContent = '<h2>Contact Request Submitted</h2>
<p><b>Name:</b> '.$name.'</p>
<p><b>Email:</b> '.$email.'</p>
<p><b>Mobile:</b> '.$mobile.'</p>
<p><b>Country:</b> '.$country.'</p>
<p><b>Subject:</b> '.$subject.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
// Header for sender info
$headers = "From: $fromName"." <".$from.">";
if(!empty($fileName) && file_exists($fileName)){
// 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/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
if(is_file($fileName)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($fileName,"rb");
$data = @fread($fp,filesize($fileName));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($fileName)."\"\n" .
"Content-Description: ".basename($fileName)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($fileName)."\"; size=".filesize($fileName).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $email;
// Send email
$mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
// Delete attachment file from the server
// @unlink($uploadedFile);
}else{
// Set content-type header for sending HTML email
$headers .= "\r\n". "MIME-Version: 1.0";
$headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
// Send email
$mail = mail($toEmail, $emailSubject, $htmlContent, $headers);
}
// If mail sent
if($mail){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
$postData = '';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
}
}
}
亲切的问候
【问题讨论】:
-
您不能“直接”发送文件,除非先将其发送到您的服务器。当用户提交表单时,该文件将被放置在您服务器上的一个临时目录中,然后您的脚本需要决定如何处理它。
-
Multipart email 是一个复杂的话题,你真的应该使用像 PHPMailer 这样成熟的邮件库,而不是尝试自己实现。
-
@CBroe 你的意思是像这个网站上的梨图书馆吗? html.form.guide/email-form/php-email-form-attachment
-
当然,如果你喜欢,你可以使用 PEAR 包。不过,我说的是github.com/PHPMailer/PHPMailer,这或多或少是 PHP 的首选解决方案,在已经带有自己的邮件程序类的框架之外。
-
为什么不在您的服务器上安装病毒扫描程序...
标签: php html file email upload