Guest User

Untitled

a guest
Jul 26th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. how to download mails attachment to a specific folder using IMAP and php
  2. $hostname = '{xxxx.net:143/novalidate-cert}INBOX';
  3. $username = 'yyy@xxxx.net';
  4. $password = 'zzzz';
  5. /* try to connect */
  6. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());
  7. $emails = imap_search($inbox,'ALL');
  8.  
  9. if($emails) {
  10. $output = '';
  11. rsort($emails);
  12. foreach($emails as $email_number) {
  13. $structure = imap_fetchstructure($inbox, $email_number);
  14. $name = $structure->parts[1]->dparameters[0]->value; // name of the file
  15. $type = $structure->parts[1]->type; //type of the file
  16. }}
  17.  
  18. $savedir = __DIR__ . '/imap-dump/';
  19.  
  20. $inbox = new IMAPMailbox($hostname, $username, $password);
  21. $emails = $inbox->search('ALL');
  22. if ($emails) {
  23. rsort($emails);
  24. foreach ($emails as $email) {
  25. foreach ($email->getAttachments() as $attachment) {
  26. $savepath = $savedir . $attachment->getFilename();
  27. file_put_contents($savepath, $attachment);
  28. }
  29. }
  30. }
Add Comment
Please, Sign In to add comment