-1

Background of the project :

I am working on a project which goal is to keep track of all emails received in a mailbox. The app connects to the mailbox and store every emails as files.

The user must have the possibility to use his mailbox regardless of the app behavior.

I am using the pop protocol at the moment i am writing this.

How it should work :

Let's say there are 10 unread mails in the mailbox, not stored by the app yet.

Case 1 : The user connects first : he reads, deletes others, tags them as spam or whatever : the app must still store each of the 10emails as files.

Case 2 : The app connects first, store the 10 emails and do something to never store them again, but the when the user will connect to his mailbox, he should still see the 10 emails as unread, and do whatever he wants with them.

How it works at the moment.. :

The function does retrieve every unread emails, and stores them as files (also downloads any attachments).

The first case works very well, i'm using outlook with my mailbox, and whatever i am doing with my emails, the app will still see the emails as unread, find them, and finally do the job.

The second case, however, seems to not work as intended : after storing the emails, i am using the imap_delete to mark these emails for deletion, and then, when everything is finished, i use imap_close with the CL_EXPUNGE flag to delete them, so that the app will not store them again next time...

The problem is that, this also deletes the emails from the server...meaning that in some cases, although the emails will have been stored, the user will never see them in his mailbox...

Question Time

Is there a way, in PHP, to delete emails only for my app, so it does not store the same emails several times, instead of deleting them from the server ? I know outlook makes it possible, because if not, it would not work in the first case either...

Is there any solution to this ? Is there a flag or a function that would do it correctly in php with the pop protocol ? Or is it possible using imap protocol and imap functions in php ?

Community
  • 1
  • 1
malystrix
  • 11
  • 2
  • As far as I know the imap functions always work on the connected server and not on your script. If you want to keep your server untouched, you need to save the message on your end including the message ID. Next time you exclude all messages with the ID you have in your database. – Talisin Jul 31 '14 at 08:55
  • So, that would mean i have to check all the message's id each time i am trying to store the emails, doesn't it ? Wouldn't it slow the process a lot, after 10 or 100k emails ? Thanks for the advice anyway :) – malystrix Jul 31 '14 at 09:28
  • 1
    Of course it will "slow down" the process a bit but as I assume that you use free mailers you will not have 100.000 emails stored. 100k emails each ~10kb means 1gig of data storage. Doubt that this will be allowed on any free mailer. I had no speed issues whatsoever. Also, the message IDs are incremental so you can start fetching the last one of you DB and select only the new one – Talisin Jul 31 '14 at 09:34
  • I'm actually working on this for a client, so there will be a huge amount of datas (he's receiving around 100 emails per day...), and the law requires him to keep his emails for years... So that would make several hundred of thousands of mails... I absolutely did not think about keeping the last ID, so i would only have to test few emails, that sounds like the beginning of a solution to me, thanks a lot ! – malystrix Jul 31 '14 at 09:44
  • Oh, and if you want your APP to delete emails, you should delete the file you've downloaded (your local copy), and not use a server function – mTorres Jul 31 '14 at 09:46
  • well, i'm not sure if i understood correctly this time. I want to keep the files for as long as the law requires it (let's say its 5years, i'm gonna keep that pdf version of the email for 5 years), but I'd like to not read that email again from the server. That might sound a bit stupid but, if there's a way to only delete the local copy of the email...i really don't know how to do it lol Could you tell me more about how it can be done ? – malystrix Jul 31 '14 at 09:54

1 Answers1

0

Your problem is the IMAP protocol. While using IMAP you're always working with the mails stored at the server, so if you mark them as read, you're marking them read at the server so any further connections (using IMAP) won't see the unread messages, also if you delete them you're deleting it from server so no further visibility (either with POP - if never connected before, of course - or IMAP).

You should switch your connection protocol in order to use POP3, doing it so you download a copy of the mail to your machine (actually to the machine executing the PHP script) and work with a local copy.

See this question for more details.

Community
  • 1
  • 1
mTorres
  • 3,590
  • 2
  • 25
  • 36
  • Hi there, thanks for your answer. If I understand correctly, all I should do is specifying I want to open a POP access with a correct string, as stated here : http://stackoverflow.com/questions/3165014/fetching-mail-from-a-pop3-server-using-php#answer-3165058 I am actually already doing that, here's what i'm using to connect : $hostname = '{myhost.example.com:110/pop3}INBOX'; $username = 'my_username@my_hostname.com'; $password = '#thatpasswd'; So, as far as I understand, I am already using the pop protocol, don't I ? – malystrix Jul 31 '14 at 09:41
  • Yup you're right, it's odd in PHP to connect using POP you have to use the imap library... we're back at start again then... – mTorres Jul 31 '14 at 09:44
  • i do agree with you, especially for newbies (like me), even more since the php.net documentation somewhat lacks a lot of information about imap/pop functions and protocols. – malystrix Jul 31 '14 at 09:46