1

I search and try different methods to produce a php mail with an attachment, which is saveable with the Client (Outlook). It doesn't work porper and I don't know why?

If the script send the mail with this code:

    $result  = mail($mail_to, $subject, $headers);

I got an email with an inline base64 coded attachment and if use it in this way:

    $result  = mail($mail_to, $subject, "", $headers); 

or

    $result  = mail($mail_to, $subject, $text, $headers); 

I got the PHP Error: "false" - the mail could not send.

So it isn't and Mailserverproblem, I think and from headersview I think, it should work and I get no mail error on the Linux site.

A part from the mail coming with the attachment inline:

    MIME-Version: 1.0
    From: [email protected]
    Content-Type: multipart/mixed; boundary="F34A3E8D822EE5A70EEDE9C3C143243C"

    --F34A3E8D822EE5A70EEDE9C3C143243C
    Content-Type: multipart/alternative; boundary="A0403290562B2A2F19FB62E48C51BE33"

    --A0403290562B2A2F19FB62E48C51BE33
    Content-Type: text/plain
    Content-Transfer-Encoding: 8bit

    Hello Mr. Xyz,
    Some stupid text as an example.

    --A0403290562B2A2F19FB62E48C51BE33--

    --F34A3E8D822EE5A70EEDE9C3C143243C
    Content-Type: application/CSV; charset="ISO-8859-1"; name="trouble.csv"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="trouble.csv"

    ...
    YWNodW5nIG5pY2h0IGVyZm9yZGVybGljaCAvIG3DtmdsaWNoIjsiIjsiIjsK

    --F34A3E8D822EE5A70EEDE9C3C143243C--

The matching PHP code:

$text = "Hello Mr. Xyz," . $nl . "Some stupid text as an example." . $nl; $content = chunk_split(base64_encode($file)); $headers = "MIME-Version: 1.0" . $nl; $headers .= "From: " . $mail_from . $nl; $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $nl; $headers .= "--" . $boundary1 . $nl; $headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary2 . "\"" . $nl . $nl; $headers .= "--" . $boundary2 . $nl; $headers .= "Content-Type: text/plain" . $nl; $headers .= "Content-Transfer-Encoding: 8bit" . $nl . $nl; $headers .= $text . $nl; $headers .= "--" . $boundary2 . "--" . $nl . $nl; $headers .= "--" . $boundary1 . $nl . $nl; $headers .= "Content-Type: application/excel; charset=\"ISO-8859-1\"; name=\"xyz.csv\"" . $nl; $headers .= "Content-Transfer-Encoding: base64" . $nl; $headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $nl . $nl; $headers .= $content . $nl; $headers .= "--" . $boundary1 . "--" . $nl;

So, where is the bug? How can I debug such a Situation? Thx in advance...

3
  • §nl is probably a syntax error. See also PHP mail form doesn't complete sending e-mail for typical causes of mail() failure. While the MIME multipart assembling looks ok for once, you'd still be better of with PHPMailer/SwiftMailer for such tasks. (Both provide better debugging options for SMTP errors in particular.) Commented Jun 1, 2015 at 15:58
  • The "§nl" would be a syntax error, but it is just a transfer bug from the orignal source to the post here. Commented Jun 1, 2015 at 19:05
  • The Trouble is...: double nl in $eaders... removing them I got an email with an attachment, but it is too short and the message text is empty... Commented Jun 3, 2015 at 7:42

1 Answer 1

0

This one works....

        $text     = "Hello Mr. Xyz," . $crnl . "Some stupid text as an example."                . $crnl;
        $content  = chunk_split(base64_encode($file));

        $headers  = "MIME-Version: 1.0"                                                         . $crnl;
        $headers .= "From: " . $mail_from                                                       . $crnl;

        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\""            . $nl . $crnl;
        $headers .= "Content-Type: text/plain"                                                  . $nl;
        $headers .= "--" . $boundary1                                                           . $crnl;
        $headers .= 'Content-Length: '.strlen($text)                                            . $crnl;
        $headers .= "Content-Transfer-Encoding: 8-bit"                                          . $nl . $crnl;
        $headers .= $text;
        $headers .= "--" . $boundary1                                                           . $crnl;

        $headers .= "Content-Type: application/excel; charset=\"ISO8859-1\"; name=\"xyz.csv\""  . $nl . $crnl;
        $headers .= "--" . $boundary1                                                           . $crnl;
        $headers .= "Content-Disposition: attachment; filename=\"xyz.csv\""                     . $crnl;
        $headers .= 'Content-Length: ' . strlen($content)                                       . $crnl;
        $headers .= "Content-Transfer-Encoding: base64"                                         . $nl . $crnl;
        $headers .= $content;
        $headers .= "--" . $boundary1 . "--"  

...with a small bug. I have an extra attment, but that's for now ok. We other thing is a longer play with "\n", "\r\n" and a mixture of both on the right places (lines).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.