diff --git a/composer.json b/composer.json index 6af16647..63d0ffd6 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,8 @@ "league/oauth2-google": "Needed for Google XOAUTH2 authentication", "psr/log": "For optional PSR-3 debug logging", "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)", - "thenetworg/oauth2-azure": "Needed for Microsoft XOAUTH2 authentication" + "thenetworg/oauth2-azure": "Needed for Microsoft XOAUTH2 authentication", + "directorytree/imapengine": "For uploading sent messages via IMAP, see gmail example" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/examples/gmail.phps b/examples/gmail.phps index fe6e09a9..25e18a48 100644 --- a/examples/gmail.phps +++ b/examples/gmail.phps @@ -83,26 +83,26 @@ if (!$mail->send()) { echo 'Message sent!'; //Section 2: IMAP //Uncomment these to save your message in the 'Sent Mail' folder. - #if (save_mail($mail)) { + #if (save_mail($mail->getSentMIMEMessage())) { # echo "Message saved!"; #} } //Section 2: IMAP -//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php -//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php -//You can use imap_getmailboxes($imapStream, '/imap/ssl', '*' ) to get a list of available folders or labels, this can -//be useful if you are trying to get this working on a non-Gmail IMAP server. -function save_mail($mail) -{ - //You can change 'Sent Mail' to any other folder or tag - $path = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail'; +//This example uses the directorytree/imapengine IMAP library: https://imapengine.com +//Earlier versions of this code used the deprecated PHP imap_* functions. +function save_mail($message) { + $mailbox = new \DirectoryTree\ImapEngine\Mailbox([ + 'host' => 'imap.gmail.com', + 'port' => 993, + 'encryption' => 'ssl', + 'username' => 'user@example.com', + 'password' => 'password', + ]); - //Tell your server to open an IMAP connection using the same username and password as you used for SMTP - $imapStream = imap_open($path, $mail->Username, $mail->Password); + // Find the "sent" messages folder – yours may have a different name. + $folder = $mailbox->folders()->find('Sent Mail'); - $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage()); - imap_close($imapStream); - - return $result; + $folder->messages()->append($message); + return true; }