Jump to content

use php to check email inbox for certain subject, then save attatchment?


android6011

Recommended Posts

so you want to download the attachement after matching subjects right?

 

try this:

 

<?php
$mbox = imap_open($mail_server, $mail_box, $password);
$count = imap_num_msg($mbox);
       
for($index = 0; $index < $count; $index++) {
   $obj = imap_headerinfo($mbox, $index);
   $string_to_find = 'look for text here';
   
   // change the if for the search algo you want
   if($string_to_find == imap_utf8($obj->subject)) {
      // put your code here to dl the attachments
   }
}
imap_close($mbox);
?>

 

when dl the attachments... you need to be aware of the encodings and the different ways attaching files. it seems that different mail clients uses different ways of attaching of files.

 

just check the rest of the imap functions: http://php.net/imap

Link to comment
Share on other sites

ill just give you the idea how it is:

 

first, you need to check the structure of the mail. from here you will know which part of the message is the attachment.

 

second, get the attachement (you may do this repeatedly since there could be multiple attachments).

 

third, determine the type of attachment you are dealing with.

 

fourth, use the proper encoding before saving.

 

fifth, save the file based on the name of the file.

Link to comment
Share on other sites

you need imap_fetchstructure() to check to check if it is an attachment (and its encoding).

 

if it is, you need imap_fetchbody() to get the attachment and to store it into a variable..

 

then write it in your server.

 

there you go... i hope this is clearer now... i was thinking that you should do this yourself but i guess am bit generous this time :D.

 

happy programming!!! God bless!

 

cheers,

 

Jay

Link to comment
Share on other sites

Thanks. I've tried a million things and I REALLY appreciate your help

 

eheh :D.

 

here is a more in-depth look:

 

once you get imao_fetchstructure(), it returns a lot of info. what you need are:

 

say you have, $info = imap_fetchstructure($mbox, $item);

 

foreach($info->parts as $part) {
   
   // this checks if its an attachment
   if ($part->disposition == "attachment" || $part->disposition == "inline") {
       
       // get the filename
       $filename = $part->dparameters[0]->value;

       // retrieve the data here
       // using imap_fetchbody()
   }
}

 

so how was it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.