PHP mail() attachment problems

后端 未结 4 1179
北海茫月
北海茫月 2020-11-30 01:50

Can someone help me figure out why this keeps returning false?

$to = \"myemail@mydomain.com\";
$from = \"Website \";
$subject = \         


        
相关标签:
4条回答
  • 2020-11-30 02:32

    These are the headers I use in my script

    $base = basename($_FILES['upload']['name']);
    $file = fopen($randname_path,'rb');
    $size = filesize($randname_path);
    $data = fread($file,$size);
    fclose($file);
    $data = chunk_split(base64_encode($data));
    
    //boundary
    $div = "==Multipart_Boundary_x".md5(time())."x";
    //headers
    $head = "From: $from\n".
            "MIME-Version: 1.0\n".
            "Content-Type: multipart/mixed;\n".
            " boundary=\"$div\"";
    //message
    $mess = "--$div\n".
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
            "Content-Transfer-Encoding: 7bit\n\n".
            "$message\n\n".
            "--$div\n".
            "Content-Type: application/octet-stream; name=\"$base\"\n".
            "Content-Description: $base\n".
            "Content-Disposition: attachment;\n".
            " filename=\"$base\"; size=$size;\n".
            "Content-Transfer-Encoding: base64\n\n".
            "$data\n\n".
            "--$div\n";
    $return = "-f$from";
    

    http://asdlog.com/Create_form_to_send_email_with_attachment

    0 讨论(0)
  • 2020-11-30 02:33
    $to = "myemail@mydomain.com";
    $from = "Website <website@mydomain.com>";
    $subject = "Test Attachment Email";
    
    $separator = md5(time());
    
    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;
    
    // attachment name
    $filename = "document.pdf";
    
    //$pdfdoc is PDF generated by FPDF
    $attachment = chunk_split(base64_encode($pdfdoc));
    
    // main header
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
    
    // no more headers after this, we start the body! //
    
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;
    
    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;
    
    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    $body .= "--".$separator."--";
    
    // send message
    if (mail($to, $subject, $body, $headers)) {
        echo "mail send ... OK";
    } else {
        echo "mail send ... ERROR";
    }
    

    What I've done:

    • Separated the body from the headers
    • removed extra EOLs put before separators
    0 讨论(0)
  • 2020-11-30 02:44

    I was also facing issue and found a neat answer. You can use this function for multiple attachment.

    function mail_attachment($files, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $cc, $bcc) 
    {
        $uid = md5(uniqid(time()));
        $header = "From: ".$from_name." <".$from_mail.">\r\n";
        $header .= "Reply-To: ".$replyto."\r\n";
        $header .= "cc : < $cc > \r\n" ;  // comma saparated emails
        $header .= "Bcc :  < $bcc >\r\n"; // comma saparated emails
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
    
        foreach ($files as $filename) 
        { 
            $file = $path.$filename; // path should be document root path.
            $name = basename($file);
            $file_size = filesize($file);
            $handle = fopen($file, "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $content = chunk_split(base64_encode($content));
    
            $header .= "--".$uid."\r\n";
            $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
            $header .= "Content-Transfer-Encoding: base64\r\n";
            $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
            $header .= $content."\r\n\r\n";
        }
        $header .= "--".$uid."--";
        return mail($mailto, $subject, "", $header);
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-30 02:49

    Like i commented in the @GovindTotla's answer, his solution has worked for me. For a matter of completeness, below is the output code of the $header with two attachments. Consider it a reverse engineered answer:

    From: from@domain.com
    Reply-To: reply@domain.com
    Cc: cc@domain.com
    Bcc: bcc@domain.com
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary="2527518bb813d2f2847a2adba8b8b47f"
    
    This is a multi-part message in MIME format.
    --2527518bb813d2f2847a2adba8b8b47f
    Content-type:text/html; charset=iso-8859-1
    Content-Transfer-Encoding: 7bit
    
    Here comes the message.
    
    --2527518bb813d2f2847a2adba8b8b47f
    Content-Type: application/octet-stream; name="test.txt"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="test.txt"
    
    U29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5n
    IGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNv
    bWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBl
    bHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21l
    dGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxz
    ZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLgo=
    
    
    --2527518bb813d2f2847a2adba8b8b47f
    Content-Type: application/octet-stream; name="test2.txt"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="test2.txt"
    
    VGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgc2Vjb25kIGF0dGFjaG1lbnQuIFRoaXMgaXMgdGhl
    IGNvbnRlbnQgb2YgdGhlIHNlY29uZCBhdHRhY2htZW50LiBUaGlzIGlzIHRoZSBjb250ZW50IG9m
    IHRoZSBzZWNvbmQgYXR0YWNobWVudC4gVGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgc2Vjb25k
    IGF0dGFjaG1lbnQuIFRoaXMgaXMgdGhlIGNvbnRlbnQgb2YgdGhlIHNlY29uZCBhdHRhY2htZW50
    LiBUaGlzIGlzIHRoZSBjb250ZW50IG9mIHRoZSBzZWNvbmQgYXR0YWNobWVudC4gVGhpcyBpcyB0
    aGUgY29udGVudCBvZiB0aGUgc2Vjb25kIGF0dGFjaG1lbnQuCg==
    
    
    --2527518bb813d2f2847a2adba8b8b47f--
    
    0 讨论(0)
提交回复
热议问题