Last active
August 4, 2020 04:56
-
-
Save lesstif/0bea04325d5f4a1c362059277d848f4c to your computer and use it in GitHub Desktop.
sending email with PHP Swiftmailer and put into the SentBox using imap protocol.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$message = (new Swift_Message('Wonderful Subject')) | |
->setFrom(['noreply@example.org' => 'John Doe']) | |
->setReplyTo('reply@example.org', 'Reply To') | |
->setTo(['receiver@domain.org', 'other@domain.org' => 'A name']) | |
->setBody('mail body', 'text/html') | |
; | |
// Swiftmail 는 sent box 에 남기지 않으므로 아래 꽁수로 imap_처리 | |
// https://stackoverflow.com/questions/19497557/add-mail-to-send-folder-with-swiftmailer | |
$stream = null; | |
$try_count = 0; | |
retry: | |
try { | |
$stream = \imap_open("{imap.naver.com/imap/ssl/novalidate-cert}", | |
'user', | |
'password', | |
null, | |
1, | |
['DISABLE_AUTHENTICATOR' => 'GSSAPI']); | |
} catch (\Exception $exception){ | |
/** | |
* 가끔 아래와 같은 예외와 함게 imap_open 에러가 발생함. 이런 경우 sleep 후에 재시도 | |
* | |
* Whoops\Exception\ErrorException : Unknown: [AUTH] Sorry, IMAP UserAuth Server Is Checking..Please, Try later. (errflg=1) | |
* | |
*/ | |
$this->info("imap exception. retrying... $try_count "); | |
sleep(3); | |
if ($try_count++ < 3) { | |
goto retry; | |
} | |
} | |
$msg = $message->toString(); | |
// connect to IMAP SSL (port 993) without Kerberos and no certificate validation | |
\imap_append($stream, "{imap.naver.com/imap/ssl/novalidate-cert}Sent Items", $msg . "\r\n", "\\Seen"); | |
// Saves message to Sent folder and marks it as read | |
\imap_close($stream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment