PHP - 带附件的邮件 - 邮件未显示

PHP - mail with attachments - message not showing

我正在尝试完成发送带附件的邮件

问题:正在发送的电子邮件没有邮件正文和附件

目标:发送utf-8html带附件的电子邮件

到目前为止我创建了...

用法:

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}--";

    }

编辑:

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

删除"--"

最后一个正文部分之后的封装边界是一个可区分的分隔符,表示后面没有其他正文部分。

处理多部分邮件时,您需要在 --==MIME_BOUNDARY 之前用 两个 换行符分隔每个部分(邮件正文 + 附件)!否则它们无法在您的消息中正确分隔。

只需将 \n 添加到您现有的代码中:

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

免责声明:我还没有测试过这些,希望它有效。

有关详细信息,请查看 RFC 1521

注意 https://bugs.php.net/bug.php?id=68776 - 不再允许使用多个换行符,对我来说 DOOManiac 答案不再有效。