Attachments not sent using php mail() function

前端 未结 4 1213

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条回答
  • 2021-01-28 02:16

    There are several issues here. The primary issue is that this code is a security nightmare and should never be deployed anywhere.

    The second issue is checking if your arguments are populated before trying to use them to instantiate other variables. When you assign $upload and then make a check for $_POST data and only then see if the $_POST argument was even set in the first place, it suggests you need to re-read the isset() documentation and even the fundamentals of conditional statements.

    0 讨论(0)
  • 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.

    <?php
    
    ....
    
        // Message body
    
        $msg = '<html><body><p>';
    
        $msg .= '<b> Request Sent From : </b>' . $reqBy . '<br/>';
    
        $msg .= '<b> Name : </b>' . $name . '<br/>';
    
        if($_POST["phone"] != "") {
           $msg .= '<b> Phone : </b>' . $phone . '<br/>';
        }
    
        if($_POST["company"] != "") {
           $msg .= '<b> Company : </b>' . $company . '<br/>';
        }
    
        if($_POST["message"] != "") {
            $msg .= '<b> Message : </b>' . $message . '<br/>';
        }
    
        if($_POST["industry"] != "") {
            $msg .= '<b> Industry : </b>' . $industry . '<br/>';
        }
    
        if($_POST["job"] != "") {
            $msg .= '<b> Job Role : </b>' . $job . '<br/>';
        }
    
        if($_FILES["upload"] != "") {
            $msg .= '<b> Upload : </b>' . $upload . '<br/>';
        }
    
        $msg .= '</p>';
        $msg .= '</body></html>';
        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!");
        }
       }
    
     ?>
    
    0 讨论(0)
  • 2021-01-28 02:25

    To better handle emails with PHP you can use this library:

    https://github.com/PHPMailer/PHPMailer

    There is also the availability to make the minimal installation:

    At the very least you will need src/PHPMailer.php. If you're using SMTP, you'll need src/SMTP.php, and if you're using POP-before SMTP, you'll need src/POP3.php.

    0 讨论(0)
  • 2021-01-28 02:30

    update your form opening tag with

    <form class="test" action="contactMail.php" method="POST" enctype="multipart/form-data">

    add below code before starting for if condition

    if ($_FILES['upload']['error'] == 0) {
       $filename = $_FILES["upload"]["name"];
       $content = file_get_contents( $_FILES['upload']['tmp_name'] );
       $content = chunk_split(base64_encode($content));
       $uid = md5(uniqid(time()));
    }
    
    0 讨论(0)
提交回复
热议问题