Reputation: 13120
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 = '[email protected]';
$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?
Upvotes: 1
Views: 13430
Reputation: 76679
Once written that for downloading Excel files;
Guess it can be changed to do almost anything email attachment-wise.
Basically it demonstrates the two steps that you are missing:
$message = array();
$message["attachment"]["type"][0] = "text";
$message["attachment"]["type"][1] = "multipart";
$message["attachment"]["type"][2] = "message";
$message["attachment"]["type"][3] = "application";
$message["attachment"]["type"][4] = "audio";
$message["attachment"]["type"][5] = "image";
$message["attachment"]["type"][6] = "video";
$message["attachment"]["type"][7] = "other";
function get_decode_value($message, $encoding) {
switch($encoding) {
case 0:case 1:$message = imap_8bit($message);break;
case 2:$message = imap_binary($message);break;
case 3:case 5:$message=imap_base64($message);break;
case 4:$message = imap_qprint($message);break;
}
return $message;
}
for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) {
$structure = imap_fetchstructure($mbox, $jk , FT_UID);
/* retrieve message timestamp */
$header = imap_headerinfo($mbox, $jk);
$timestamp = $header->udate;
$parts = $structure->parts;
$fpos=2;
for($i = 1; $i < count($parts); $i++) {
$message['pid'][$i] = ($i);
$part = $parts[$i];
if($part->disposition == "ATTACHMENT") {
$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
$message["subtype"][$i] = strtolower($part->subtype);
$ext = $part->subtype;
$params = $part->dparameters;
$filename = $part->dparameters[0]->value;
$body="";$data="";
if($filename=='SomeFile.XLS'){
$body = imap_fetchbody($mbox, $jk, $fpos);
$dst = '/SomeLocalPath/SomeOtherFile.xls';
/* don't overwrite */
if(!file_exists($dst)){
$fp = fopen($dst, 'w');
$data = get_decode_value($body, $part->type);
fputs($fp, $data);
fclose($fp);
}
}
$fpos++;
}
}
}
I'd rather wonder why the attachment can't be accessed via any Google API ...
Why there's no bi-directional GMail API, like the uni-directional Mandrill API?
Upvotes: 7