Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将文件从服务器附加到电子邮件中_Php_Email Attachments - Fatal编程技术网

Php 将文件从服务器附加到电子邮件中

Php 将文件从服务器附加到电子邮件中,php,email-attachments,Php,Email Attachments,有谁能告诉我如何从服务器附加文件并通过电子邮件将其作为附件发送?? 我有以下代码: <?php // Read POST request params into global vars $to = $_POST['to']; $from = $_POST['from']; $subject = $_POST['subject']; $message = $_POST['message']; // Obtain file upload vars $fileatt

有谁能告诉我如何从服务器附加文件并通过电子邮件将其作为附件发送?? 我有以下代码:

<?php 
// Read POST request params into global vars 
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject']; 
$message = $_POST['message'];
// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name']; 
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";  
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file);
// Generate a boundary string 
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment  
$headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" . 
        " boundary=\"{$mime_boundary}\""; 
// Add a multipart boundary above the plain message 
 $message = "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" .   
$message . "\n\n"; 
// Base64 encode the file data 
// Add file attachment to the message 
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
 " name=\"{$fileatt_name}\"\n" . 
//"Content-Disposition: attachment;\n" . 
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .              
          $data . "\n\n" .  "--{$mime_boundary}--\n"; 
}
// Send the message 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
echo "<p>Mail sent! Yay PHP!</p>"; 
} else { 
echo "<p>Mail could not be sent. Sorry!</p>"; 
}
?>

但此代码不会从服务器附加文件。
请帮帮我。。提前谢谢

一个问题:您是希望发送带有附件的电子邮件,还是希望通过此问题提高您的PHP技能

如果你只想发邮件,看看哪一个很好,也很容易处理。就我自己而言,我已经用了很多年了,没有任何问题


为了提高你的技能:你说附件是用base64编码的,但我找不到你做类似于$data=base64\u encode($data)的事情的那一行您将附件中的$data以明文形式添加到邮件中。

是的。。I’对不起。。我在这里粘贴代码时漏掉了那一行。。代码是这样的$标题。=“MIME版本:1.0\n”。“内容类型:多部分/混合;\n”。“边界=\”{$mime\u boundary}\”$数据=块分割(base64编码($data));一切正常,我可以发送带有附件的电子邮件。。我面临的问题是当我尝试从服务器附加文件时..添加位于服务器上的文件的代码在哪里?您可以检查是否上载了该文件。因此,无法从服务器添加文件。嗨,lspcity。。!!谢谢你的帮助。。!!嗯,问题解决了。。以前,我只是把文件上传到服务器上。。当我将文件插入数据库并在以附件形式邮寄文件时检索它时,问题就解决了。。下面是代码:$sql12=“select*fromsdrafts,其中path='$path';$result=mysql\u查询($sql12);$curr\u file=mysql\u fetch\u assoc($result)$fileatt_size=$curr_file['file_size']$fileatt_type=$curr_file['file_type']$fileatt_name=$curr_file['file_name']$数据=$curr_file['file_Content'];再一次谢谢你。。!!