Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how to download mails attachment to a specific folder using IMAP and php
- $hostname = '{xxxx.net:143/novalidate-cert}INBOX';
- $username = 'yyy@xxxx.net';
- $password = 'zzzz';
- /* try to connect */
- $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());
- $emails = imap_search($inbox,'ALL');
- if($emails) {
- $output = '';
- rsort($emails);
- foreach($emails as $email_number) {
- $structure = imap_fetchstructure($inbox, $email_number);
- $name = $structure->parts[1]->dparameters[0]->value; // name of the file
- $type = $structure->parts[1]->type; //type of the file
- }}
- $savedir = __DIR__ . '/imap-dump/';
- $inbox = new IMAPMailbox($hostname, $username, $password);
- $emails = $inbox->search('ALL');
- if ($emails) {
- rsort($emails);
- foreach ($emails as $email) {
- foreach ($email->getAttachments() as $attachment) {
- $savepath = $savedir . $attachment->getFilename();
- file_put_contents($savepath, $attachment);
- }
- }
- }
Add Comment
Please, Sign In to add comment