A php imap script to download gmail attachments

This is a simple php script to automatically download gmail attachments based on incoming author and subject text of email into folder script is run on, but other imap search queries could be used. I use it locally on my linux box in the form of ‘php download-directly-from-gmail.php’ I hope it helps someone.

eg.

orion@orion-Dimension-2350 ~/Downloads/SHUTTERSTOCK IMAGES $ php download-directly-from-gmail.php
173-shutterstock_117192613.jpg .. saving
174-shutterstock_125141474.jpg .. saving
175-shutterstock_126624197.jpg .. saving
170-shutterstock_107627765.jpg .. saving
171-shutterstock_116964424.jpg .. saving
172-shutterstock_117192571.jpg .. saving
166-shutterstock_77529775.jpg .. saving
167-shutterstock_85118617.jpg .. saving
168-shutterstock_93945283.jpg .. saving
169-shutterstock_107147873.jpg .. saving

Script below.. with password and username nulled.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
set_time_limit(0); 
 
 /* connect to gmail with your login account details */
 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'somebody@gmail.com'; # e.g somebody@gmail.com
$password = 'your-gmail-password';
 
 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
 
 
$emails = imap_search($inbox,'FROM "person" SUBJECT "something in subject"'); // finds all incoming mail from "person" containing partial text in subject 'something in subject'
 
if($emails) {
 
    $count = 1;
 
 
    rsort($emails);
 
 
    foreach($emails as $email_number) 
    {
 
        $overview = imap_fetch_overview($inbox,$email_number,0);
 
        $message = imap_fetchbody($inbox,$email_number,2);
 
        $structure = imap_fetchstructure($inbox, $email_number);
 
        $attachments = array();
 
        if(isset($structure->parts) && count($structure->parts)) 
        {
            for($i = 0; $i < count($structure->parts); $i++) 
            {
                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );
 
                if($structure->parts[$i]->ifdparameters) 
                {
                    foreach($structure->parts[$i]->dparameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'filename') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }
 
                if($structure->parts[$i]->ifparameters) 
                {
                    foreach($structure->parts[$i]->parameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'name') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object->value;
                        }
                    }
                }
 
                if($attachments[$i]['is_attachment']) 
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
 
                    if($structure->parts[$i]->encoding == 3) 
                    { 
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    elseif($structure->parts[$i]->encoding == 4) 
                    { 
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }
 
        foreach($attachments as $attachment)
        {
            if($attachment['is_attachment'] == 1)
            {
                $filename = $attachment['name'];
                if(empty($filename)) $filename = $attachment['filename'];
 
                if(empty($filename)) $filename = time() . ".dat";
 
                echo($filename." ");
 
                if(file_exists($email_number . "-" . $filename)) {
                echo(".. file already exists! \n");
                } else {
                echo(".. saving \n");
                $fp = fopen($email_number . "-" . $filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
                }
            }
 
        }
 
    }
 
} 
 
imap_close($inbox);
 
echo "Done";
 
?>