How do you send email from IMAP account with PHP?

Posted by arthurakay on Stack Overflow See other posts from Stack Overflow or by arthurakay
Published on 2010-12-22T19:29:48Z Indexed on 2010/12/22 19:54 UTC
Read the original article Hit count: 160

Filed under:
|

I'm having an issue sending email via PHP/IMAP - and I don't know if it's because:

  • I don't correctly understand IMAP, or
  • there's an issue with my server

My application opens an IMAP connection to an email account to read messages in the inbox. It does this successfully. The problem I have is that I want to send messages from this account and have them display in the outbox/sent folder.

As far as I can tell, the PHP imap_mail() function doesn't in any way hook into the IMAP stream I currently have open.

My code executes without throwing an error. However, the email never arrives to the recipient and never displays in my sent folder.

private function createHeaders() {
    return "MIME-Version: 1.0" . "\r\n" .
        "Content-type: text/html; charset=iso-8859-1" . "\r\n" .
        "From: " . $this->accountEmail . "\r\n";
}

private function notifyAdminForCompleteSet($urlToCompleteSet) {
    $message = "
        <p>
            In order to process the latest records, you must visit
            <a href='$urlToCompleteSet'>the website</a> and manually export the set.
        </p>
    ";

    try {
        imap_mail(
            $this->adminEmail,
            "Alert: Manual Export of Records Required",
            wordwrap($message, 70),
            $this->createHeaders()
        );
        echo("   ---> Admin notified via email!\n");
    }
    catch (Exception $e) {
        throw new Exception("Error in notifyAdminForCompleteSet()");
    }
}

I'm guessing I need to copy the message into the IMAP account manually... or is there a different solution to this problem?

Also, does it matter if the domain in the "from" address is different than that of the server on whicn this script is running? I can't explain why the message is never sent.

© Stack Overflow or respective owner

Related posts about php

Related posts about imap