Search code examples
phpoutlookoffice365phpmailerimap

PHPMailer with Outlook365 - imap_append 7KB size limit


I have PHPMailer setup to use our Outlook365 server to send emails with attachments. It works great until the script, using imap_append, attempts to add the message to our "sent" folder. What I have discovered is that if the attachment is more than 7KB in size, it fails. Less than 7KB, it works. The recipient receives the attachment either way.

Any idea where this limitation is coming from and how I can increase it?

Here's the code block for saving the messages to the sent folder.

function save_mail($mail) {
global $config;
$MailHost = "{".$config->MailHost."/imap/ssl/novalidate-cert}Sent Items";
$path = $MailHost; 
if ($conn = imap_open($path, $config->MailUsername, $config->MailPassword)) {
    if (imap_append($conn, $path, $mail->getSentMIMEMessage())) {
        $result = true;
    } else {
        error_log("ERROR: Unable to save mail to 'Sent Items' folder.\n  - " . imap_last_error());
        $result = false;
    }
    imap_close($conn);
    return $result;
} else {
    error_log("ERROR: Unable to connect to IMAP server.");
    return false;
}

}


Solution

  • From my testing today, if sending to an Exchange based IMAP server with an attachment or embedded image greater than 7kb, you will need to change the line returns from unix format to windows format. I simply did a string replace on '\n' and replaced with '\r\n' to get this done.

    $content =  str_replace("\n", "\r\n", $mail->getSentMIMEMessage());