Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* connect to gmail */
- $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
- $username = '##YOUR EMAIL ID';
- $password = '##YOUR PASSWORD';
- /* try to connect */
- $inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
- $emails = imap_search($inbox, 'SUBJECT "##EMAIL SUBJECT"');
- foreach ($emails as $email_msg) {
- $structure = imap_fetchstructure($inbox, $email_msg);
- $msg_info = imap_headerinfo($inbox, $email_msg);
- if (isset($structure->parts) && empty($structure->parts) == FALSE) {
- foreach ($structure->parts as $key => $part_info) {
- if (isset($part_info->disposition) && $part_info->disposition == 'ATTACHMENT' && count($part_info->dparameters) > 0) {
- foreach ($part_info->dparameters as $file) {
- if ($file->attribute == 'FILENAME') {
- $file_info = pathinfo($file->value);
- $fp = fopen('files/' . $msg_info->subject . '.' . $file_info['extension'], 'w+') or exit('Failed to open file');
- $msg_body = imap_fetchbody($inbox, $email_msg, $key + 1);
- $content = base64_decode($msg_body);
- fwrite($fp, $content);
- }
- }
- }
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment