How to get email and their attachments from PHP

I used to do a lot of this before, but I can’t find the code, here’s a scaled down version I found. It should put you on the correct path. I used to run this type of script from a cronjob. Sorry I can’t find the final version. ;(

// Open pop mailbox
if (!$mbox = imap_open ("{localhost:110/pop3/notls}INBOX", "user", "tester")) {
  die ('Cannot connect/check pop mail! Exiting');
}

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
} else {
  echo "Failed to get mail";
  exit;
}

$MN=$msgCount;
$overview=imap_fetch_overview($mbox,"1:$MN",0);

for ($X = 1; $X <= $MN; $X++) {

  $file = imap_fetchbody($mbox, $X, 1);

  imap_delete($mbox, $X);
}

imap_expunge($mbox);
imap_close($mbox);

Good luck!

Leave a Comment