We need a small PHP command line script that reads a POP3 account and extract attatchments and saves them to a folder. The code should be in a function so that the script is easy to include and call from other scripts.
## Deliverables
We need php command line script containing with a function that:
1) Logs on to a POP3 account and download messages one by one
2) If the message contains an attachment with a specified name, it should be stored to a folder. A number must be added to the file to be sure it is unique and an existing file with the same name is not overwritten.
3) The function should return an array containing the names of all stored files This should be the name the file is saved with, not the original name of the file.
4) All the mails from which a file is saved, should be marked as read and deleted from the POP3 mailbox so that a file is never saved twice.
5) Code shoud be well documented so that is easy for another coder to understand what the code is doing.
Example PHP code:
<?php
$file_names = download_attachments('[login to view URL]', 'name', 'pwd', '[login to view URL]', '/tmp/');
echo "Saved files: \n";
foreach($file_names as $file_name){
echo $file_name . "\n";
}
function download_attachments($pop3_server, $pop3_user, $pop3_password, $attachment_filename_regex, $save_to_folder){
? ? //Log on server
? ? ?
? ? //Iterate through messages:
? ? ?
? ? //If attachment exixsts and attachment filename match $attachment_filename_regex :
? ? ? ? ? ? //If filemname already exists in folder:
? ? ? ? ? ? //Find a running number to add before the file ending like this
? ? ? ? ? ? //[login to view URL], [login to view URL], [login to view URL]
? ? ? ? //Save attatchment as selected filename
? ? ? ? //Mark message as read/to be deleted
? ? ? ? //Store filename in array
? ? ? ?
? ? //Return array of filenames
? ? ?
}
?>