PHP - mail with attachments - message not showing

488 Views Asked by At

I am trying to complete sending mail with attachments

Problem: Email is sending without message body and attachments

Goal: send utf-8 html email with attachments

I created so far ...

Usage:

email_library.php

<?php

    private function create_body()
    {
        $this->body[] = "--{$this->uid}";
        $this->body[] = "Content-type: text/html; charset=utf-8";
        $this->body[] = "Content-Transfer-Encoding: 7bit";
        $this->body[] = $this->message;

        if (!empty($this->attachments)) {

            foreach ($this->attachments as $k => $a) {
                $this->body[] = "--{$this->uid}";
                $this->body[] = "Content-Type: application/octet-stream; name=\"{$a['name']}\"";
                $this->body[] = "Content-Description: {$a['name']}";
                $this->body[] = "Content-Transfer-Encoding: base64";
                $this->body[] = "Content-Disposition: attachment; filename=\"{$a['name']}\"";
                $this->body[] = "{$a['data']}";
            }

        }
        $this->body[] = "--{$this->uid}--";

    }

EDIT:

$this->body[] = "--{$this->uid}";

removing "--"

The encapsulation boundary following the last body part is a distinguished delimiter that indicates that no further body parts will follow.

2

There are 2 best solutions below

0
On BEST ANSWER

When working with a multipart message, you need to separate each part (message body + attachments) with two line breaks before the --==MIME_BOUNDARY! Otherwise they do not separate properly in your message.

Simply add \n to your existing code:

$this->body[] = $this->message."\n\n";
...
$this->body[] = "{$a['data']}\n\n";

Disclaimer: I have not tested any of this, hopefully it works.

For more information, check out RFC 1521

0
On

Watch out for https://bugs.php.net/bug.php?id=68776 - multiple linebreaks are not allowed anymore, for me DOOManiac answer is not working (anymore).