Skip to content

Commit

Permalink
IMAP section in the Gmail example (PHPMailer#1117)
Browse files Browse the repository at this point in the history
Add IMAP example into gmail example
  • Loading branch information
NiklasBr authored and Synchro committed Aug 2, 2017
1 parent 22d04c6 commit ccdeae1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/gmail.phps
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/

//SMTP needs accurate times, and the PHP time zone MUST be set
Expand Down Expand Up @@ -72,4 +73,27 @@ if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# 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";

//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);

$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
imap_close($imapStream);

return $result;
}

0 comments on commit ccdeae1

Please sign in to comment.