Last active
April 2, 2025 08:55
-
-
Save vinhjaxt/b9731c627af3304f169b87b3659881d3 to your computer and use it in GitHub Desktop.
php curl read, send email via pop3, imap, smtp
This file contains hidden or 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 | |
// list email | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'imaps://mail.xxx:993/INBOX', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_CUSTOMREQUEST => 'examine inbox', | |
CURLOPT_USERPWD => 'xxx@xxx:xxx', | |
// CURLOPT_PASSWORD => '', | |
// CURLOPT_USERNAME => '', | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'imaps://mail.xxx:993/INBOX', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_CUSTOMREQUEST => 'fetch 1:* (UID FLAGS)', | |
CURLOPT_USERPWD => 'xxx@xxx:xxx', | |
// CURLOPT_PASSWORD => '', | |
// CURLOPT_USERNAME => '', | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// read email | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'imaps://mail.xxx:993/INBOX;UID=1139', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_USERPWD => 'xxx@xxx:xxx', | |
// CURLOPT_PASSWORD => '', | |
// CURLOPT_USERNAME => '', | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// imap folder | |
// list email in folder | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'imaps://mail.xxx.xxx:993/INBOX/'.rawurlencode(mb_convert_encoding($folder, "UTF7-IMAP", "UTF-8")), | |
'no_verify' => true, | |
'opts' => [ | |
// CURLOPT_CUSTOMREQUEST => 'examine "inbox/'.mb_convert_encoding($folder, "UTF7-IMAP", "UTF-8").'"', | |
CURLOPT_CUSTOMREQUEST => 'fetch 1:* (UID FLAGS)', | |
CURLOPT_PASSWORD => PASSWD, | |
CURLOPT_USERNAME => USER, | |
CURLOPT_VERBOSE => true, | |
CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// read email in folder | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'imaps://mail.xxx.xxx:993/INBOX/'.rawurlencode(mb_convert_encoding($folder, "UTF7-IMAP", "UTF-8")).';UID=59414', | |
'no_verify' => true, | |
'opts' => [ | |
// CURLOPT_CUSTOMREQUEST => 'examine inbox', | |
CURLOPT_PASSWORD => PASSWD, | |
CURLOPT_USERNAME => USER, | |
CURLOPT_VERBOSE => true, | |
CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// list email | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'pop3s://mail.xxx:995/', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_USERPWD => 'xxx@xxx:xxx', | |
// CURLOPT_PASSWORD => '', | |
// CURLOPT_USERNAME => '', | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'pop3s://mail.xxx:995/', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_USERNAME => 'xxx', | |
CURLOPT_PASSWORD => 'xxx', | |
CURLOPT_CUSTOMREQUEST => 'UIDL' | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// read email | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'pop3s://mail.xxx:995/582', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_USERPWD => 'xxx@xxx:xxx', | |
// CURLOPT_PASSWORD => '', | |
// CURLOPT_USERNAME => '', | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// send email | |
$sent_content = false; | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'smtp://mail.xxx.xxx:587/mail.xxx.xxx', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_USERNAME => 'xxx', | |
CURLOPT_PASSWORD => 'xxx', | |
CURLOPT_MAIL_FROM => '[email protected]', | |
CURLOPT_MAIL_RCPT => ['[email protected]'], | |
CURLOPT_USE_SSL => CURLUSESSL_ALL, | |
// CURLOPT_INFILE => STDIN, | |
CURLOPT_UPLOAD => true, | |
CURLOPT_READFUNCTION => function ($ch, $fh, $length = false) use (&$sent_content) { | |
if ($sent_content) return ''; | |
$sent_content = true; | |
return "From: Vinh Duong <[email protected]>\r\nTo: [email protected]\r\nSubject: an example.com example email\r\nDate: Mon, 7 Nov 2016 08:45:16\r\n\r\nDear Joe,\r\nWelcome to this example email. What a lovely day."; | |
}, | |
// CURLOPT_VERBOSE => true, | |
// CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); | |
// Append to sent folder imap (not actually sending an email) | |
$sent_content = false; | |
$content = "From: Vinh Duong <xxx@xxx>\r\nTo: xxx@xxx\r\nSubject: XXXan example.com example email\r\nDate: Mon, 7 Nov 2016 08:45:16\r\n\r\nDear Joe,\r\nWelcome to this example email. What a lovely day."; | |
var_dump($resps = curl_multi([ | |
[ | |
'url' => 'imaps://mail.xxx:993/Sent', | |
'no_verify' => true, | |
'opts' => [ | |
CURLOPT_USERNAME => 'xxx@xxx', | |
CURLOPT_PASSWORD => 'xxx', | |
CURLOPT_MAIL_FROM => 'xxx@xxx', | |
CURLOPT_MAIL_RCPT => ['xxx@xxx'], | |
// CURLOPT_INFILE => STDIN, | |
CURLOPT_UPLOAD => true, | |
CURLOPT_INFILESIZE => strlen($content), | |
CURLOPT_READFUNCTION => function ($ch, $fh, $length = false) use (&$sent_content, $content) { | |
if ($sent_content) return ''; | |
$sent_content = true; | |
return $content; | |
}, | |
CURLOPT_VERBOSE => true, | |
CURLOPT_STDERR => fopen('php://stderr', 'a+'), | |
] | |
], | |
], [ | |
CURLMOPT_MAX_TOTAL_CONNECTIONS => 3, | |
CURLMOPT_MAX_HOST_CONNECTIONS => 3, | |
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX, | |
])); |
curl "smtp://mail.domain.tld:587" -v --mail-from "[email protected]" --mail-rcpt "[email protected]" --ssl -u "[email protected]:xxx" -k --anyauth
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.