Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_Forms_Email_Attachment_Email Attachments - Fatal编程技术网

在php中发送文件附件时,附件未打开

在php中发送文件附件时,附件未打开,php,forms,email,attachment,email-attachments,Php,Forms,Email,Attachment,Email Attachments,我使用此代码从表单向电子邮件发送附件,但我面临两个问题: 1-有时它工作,有时停止工作 2-当我在电子邮件中收到attachmet时,我可以看到大小为的附加文件,但它没有打开,对于word文档文件,它是打开的,但它是空的,对于图片、excel文件和pdf,也一样,任何人都可以帮助吗 <?php // Email address to which you want to send email $to = 'xxx@xxx.com'; $subject = $_POST['fname']; /

我使用此代码从表单向电子邮件发送附件,但我面临两个问题: 1-有时它工作,有时停止工作 2-当我在电子邮件中收到attachmet时,我可以看到大小为的附加文件,但它没有打开,对于word文档文件,它是打开的,但它是空的,对于图片、excel文件和pdf,也一样,任何人都可以帮助吗

<?php
// Email address to which you want to send email
$to = 'xxx@xxx.com';
$subject = $_POST['fname'];
//$message = nl2br($_POST["fname"]);
/*********Creating Uniqid Session*******/
$txtSid = md5(uniqid(time()));
$headers = "";
$headers .= "From: ".$_POST['fname']."<".$_POST['femail'].">\r\nReply-To:   
".$_POST['femail']."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\r\n\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$txtSid."\r\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\n";
//$headers .= $message."\r\n\n";
/***********Email Attachment************/
if($_FILES['attachment']['name'] != "")
{
    $txtFilesName = $_FILES['attachment']['name'];
    $txtContent = chunk_split(base64_encode(file_get_contents($_FILES['attachment']  
['tmp_name']))); 
    $headers .= "--".$txtSid."\r\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n"; 
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\r\n\n";
    $headers .= $txtContent."\r\n\n";
}

// @ is for skiping Errors //
$flgSend=@mail($to,$subject,null,$headers);  
if($flgSend)
{
    echo "Email Sent SuccessFully, WelServ will contact you as soon as possible.<br/>you 
will be redirected in 5 seconds<br/>";
    echo "<script language='javascript'>window.setTimeout(function() 
{location.href='hire.php'}, 5000)</script>";

}
else
{
    $errorread=error_get_last(); 
    echo '$errorread';
    echo "Error in Sending Email, please try again,  you will be redirected in 5 
seconds!<br/>";
    echo "<script language='javascript'>window.setTimeout(function() 
{location.href='hire.php'}, 5000)</script>";
}

?>

第x次:不要自己使用PHP的mail做这样的事情,而是使用类似PHPMailer或Swift-mailer的mailer类。zend mail呢?我对这些mailer不熟悉