Below I am looping through emails, using PHPs imap libraries. It works, but I'm stuck on saving attachments from the message.
<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'someemail@somedomain.com';
$password = 'somepassword';
/* try to connect */
$imap_connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($imap_connection,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
$structure = imap_fetchstructure($imap_connection,$email_number);
if(isset($structure->parts) && count($structure->parts)){
for($i = 0; $i < count($structure->parts); $i++){
if(!empty($structure->parts[$i]->bytes)){
if(!empty($ifdparameters)){
echo "File: ----".$structure->parts[$i]->dparameters[0]->value;
// returns: testMeOut.jpg
}
}
} //eof $i loop
} //eof $structure->parts
} // eof foreach $emails
} // eof if($emails)
?>
I am trying to understand how to save an attachment to a directory on my server. I know: $structure->parts[$i]->dparameters[0]->value; is returning the name of the attachment.
I'm sure there is something I need to do with imap_fetchbody(), but I'm absolutely confused reading the tutorials online and php's site.
Can anyone see my missing last few steps on how to save an attachment from the $message?