Attachments not sent using php mail() function

前端 未结 4 1219

I\'m working on file attachment here. The mail function is working fine other than the file field is empty. I have tried using Content-Type: multipart/mixed and som

4条回答
  •  猫巷女王i
    2021-01-28 02:24

    I hope it is just a typo. You are using the var $msg twice. Once for Your html-message and for the body of the message.

    '; $msg .= ' Request Sent From : ' . $reqBy . '
    '; $msg .= ' Name : ' . $name . '
    '; if($_POST["phone"] != "") { $msg .= ' Phone : ' . $phone . '
    '; } if($_POST["company"] != "") { $msg .= ' Company : ' . $company . '
    '; } if($_POST["message"] != "") { $msg .= ' Message : ' . $message . '
    '; } if($_POST["industry"] != "") { $msg .= ' Industry : ' . $industry . '
    '; } if($_POST["job"] != "") { $msg .= ' Job Role : ' . $job . '
    '; } if($_FILES["upload"] != "") { $msg .= ' Upload : ' . $upload . '
    '; } $msg .= '

    '; $msg .= ''; var_dump($msg); $filename = $_FILES["upload"]["name"]; //$content = file_get_contents( $_FILES['upload']['tmp_name'] ); if(!empty($_FILES['upload']['tmp_name']) && file_exists($_FILES['upload']['tmp_name'])) { $content= file_get_contents($_FILES['upload']['tmp_name']); // No add_slashes() } $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $replyto = 'test'; $headers = "From: ".$subject." <".'yourmail@gmail.com'.">\r\n"; $headers .= "Reply-To: ".$replyto."\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

    Rename the var here and change Content-type and transfer-encoding as well:

        $msgBody = "--".$uid."\r\n";
        $msgBody .= "Content-type:text/html; charset=iso-8859-1\r\n";
        $msgBody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
        $msgBody .= $msg."\r\n\r\n";
    
        if(isset($_Files['upload'])) // Only add attachment if uploaded
             $msgBody .= "--".$uid."\r\n";
             $msgBody .= "Content-Type: ".mime_content_type ( $_FILES['upload']['tmp_name'] )."; name=\"".$filename."\"\r\n";
             $msgBody .= "Content-Transfer-Encoding: base64\r\n";
             $msgBody .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
             $msgBody .= $content."\r\n\r\n";
        }
        $msgBody .= "--".$uid."--";
    
        if( mail( $mail_to, $subject, $msgBody, $headers )) {
            echo "Thank You!";
        } else {
            die("Error!");
        }
       }
    
     ?>
    

提交回复
热议问题