Imap_mail_compose撰写带附件和邮件的电子邮件

时间:2018-04-12 06:32:55

标签: php imap

我尝试使用正文和多个附件撰写电子邮件,但无法完成。使用imap_mail_compose撰写电子邮件并使用imap_append将草稿添加到草稿中。虽然创建电子邮件用户可以上传多个图像,这就是为什么我需要这些imgaes在草稿中移动。谢谢你的帮助

    $conn = imap_open($rootMailBox, Auth::user()->email_smtp, Auth::user()->pass_smtp);


    $envelope["date"] = date('r');
    $envelope["from"] = $data['from'];
    $envelope["to"] = $data['to'];
    $envelope["subject"] = $data['subject'];
    if (array_key_exists('cc', $data)) {
        $envelope["cc"] = $data['cc'];
    }
    if (array_key_exists('bcc', $data)) {
        $envelope["bcc"] = $data['bcc'];
    }
    if (array_key_exists('attachs', $data)) {
        $multipart["type"] = "MULTIPART";
        $multipart["subtype"] = "mixed";
        $body[] = $multipart;
    }

    if (array_key_exists('attachs', $data)) {
        foreach ($data['attachs'] as $attach) {
            $part = array();
            $filename = $attach;
            if (filesize($filename) > 0) {
                $fp = fopen($filename, "rb");
                $file_size = filesize($attach);
                $part["type"] = 'APPLICATION';
                $part["encoding"] = ENCBASE64;
                $part["subtype"] = "octet-stream";
                $part["description"] = basename($filename);
                $part['disposition.type'] = 'attachment';
                $part['disposition'] = array('filename' => basename($filename));
                $part['type.parameters'] = array('name' => basename($filename));
                $part["description"] = '';
                $part["contents.data"] = base64_encode(fread($fp, $file_size));
                $body[] = $part;
                fclose($fp);
            }
        }
    }
    if ($data['message']) {
        $part = array();
        $part["type"] = "TEXT";
        $part["subtype"] = "html";
        $part["description"] = '';
        $part["contents.data"] = $data['message'];
        $body[] = $part;
    }

    $msg = imap_mail_compose($envelope, $body);
    if (imap_append($conn, $draftsMailBox, $msg) === false) {
        //die("could not append message: " . imap_last_error());
        return FALSE;
    } else {
        return TRUE;
    }

1 个答案:

答案 0 :(得分:0)

更正码---

我必须为附件添加“替代”子类型。它适用于html正文和附件。

    if (array_key_exists('attachs', $data)) {
        $multipart["type"] = TYPEMULTIPART;
        $multipart["subtype"] = "alternative";
        $body[$i++] = $multipart;
    }

    if (array_key_exists('attachs', $data)) {
        foreach ($data['attachs'] as $attach) {
            $part = array();
            $filename = $attach;
            if (filesize($filename) > 0) {
                $fp = fopen($filename, "rb");
                $file_size = filesize($attach);
                $part["type"] = 'APPLICATION';
                $part["encoding"] = ENCBASE64;
                $part["subtype"] = "octet-stream";
                $part["description"] = basename($filename);
                $part['disposition.type'] = 'attachment';
                $part['disposition'] = array('filename' => basename($filename));
                $part['type.parameters'] = array('name' => basename($filename));
                $part["description"] = '';
                $part["contents.data"] = base64_encode(fread($fp, $file_size));
                $body[$i++] = $part;
                fclose($fp);
            }
        }
    }
    if ($data['message']) {
        $part = array();
        $part["type"] = "TEXT";
        $part["subtype"] = "html";
        $part["description"] = '';
        $part["contents.data"] = $data['message'];
        $body[$i++] = $part;
    }

    $msg = imap_mail_compose($envelope, $body);
    if (imap_append($conn, $draftsMailBox, $msg) === false) {
        //die("could not append message: " . imap_last_error());
        return FALSE;
    } else {
        return TRUE;
    }