pop3 email script
Ik heb een email script gevonden waar ik even mee aan het rommelen ben
Ik heb het voor zover voor elkaar dat ik de from, to, subject, en de date uit de emails kan halen maar ik krijg het niet voor elkaar om ook de attachment er uit te halen
heeft iemand wat ervaring met dit soort scripts en een idee hoe ik ook de attachment er uit kan vissen
alvast bedankt
hieronder het script
Code (php)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
*
* POP3 Class
*
* PHP Version 5
*
* @author Martijn Dwars (Xanno)
* @copyright Xanno Webdevelopment
* @file class.pop3.inc.php
* @info Please visit Xanno.nl
* @date 13 / 12 / '06
* @version 2.4.0
*/
error_reporting( E_ALL );
set_time_limit( 30 );
Class POP3
{
public $sUser;
public $sPass;
public $sServer;
public $iMails;
public $iSize;
public $sBuffer;
/**
* @desc Connect to the mailserver
* @scope public
* @param The mailserver
* @param The serverport
*/
public function Connect( $sServer, $iPort = 110 )
{
if ( $this -> sServer = fsockopen( $sServer, $iPort, &$iSockerror, &$sSockerror, 1 ) )
{
return true;
}
else
{
$this -> _Error( $sSockerror );
return false;
}
}
/**
* @desc Authenticate with mailserver
* @scope public
* @param Mailbox Username
* @param Mailbox Password
*/
public function Authenticate( $sUser, $sPass )
{
$this -> _Put( "USER " . $sUser );
$this -> _Get( );
$this -> _Put( "PASS " . $sPass );
$this -> _Get( );
$this -> _Get( );
if ( substr( $this -> sBuffer, 0, 3 ) == "+OK" )
{
return true;
}
else
{
$this -> _Error( $this -> sBuffer );
return false;
}
}
/**
* @desc Get some information
* @scope public
*/
public function Information( )
{
$this -> _Put( "STAT" );
$this -> sBuffer = $this -> _Get( );
$this -> iMails = substr( $this -> sBuffer, 4, 2 );
$this -> iSize = substr( $this -> sBuffer, 6, strlen( $this -> sBuffer ) -6 );
return array( $this -> iMails, $this -> iSize );
}
/**
* @desc Retrieve mailinfo
* @scope public
* @param Mail ID
*/
public function Retrieve( $iID )
{
$this -> _Put( "RETR " . $iID );
$this -> _Get();
$this -> _Get();
while( $this->sBuffer <> ".\r\n" )
{
echo "$this->sBuffer";
$this->_Get();
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'x-priority:' )
{
$Retr['priority'] = substr( $this->sBuffer, 12, 1 );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'from:' )
{
$Retr['from'] = htmlspecialchars( substr( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 8 ) == 'subject:')
{
$Retr['subject'] = htmlspecialchars( substr( $this->sBuffer, 9, strlen( $this->sBuffer) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'date:' )
{
$Retr['date'] = htmlspecialchars( substr ( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 3 ) == 'to:' )
{
$Retr['to'] = htmlspecialchars( substr ( $this->sBuffer, 3, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'attachment;' )
{
$Retr['attachment'] = substr( $this->sBuffer, 512, 1 );
}
}
return array( $Retr['from'], $Retr['subject'], $Retr['date'], $Retr['to'], $Retr['attachment'] );
}
/**
* @desc Disconnect connection
* @scope public
*/
public function Disconnect( )
{
$this -> _Put( "QUIT" );
fclose( $this -> sServer );
}
/**
* @desc Write something to server
* @scope public
* @param Command that has to been send
*/
public function _Put( $Msg )
{
fputs( $this -> sServer, $Msg . "\r\n" );
}
/**
* @desc Get response from server
* @scope public
*/
public function _Get( )
{
$this -> sBuffer = fgets( $this -> sServer, 512 );
return $this -> sBuffer;
}
/**
* @desc Write nice errors
* @scope public
* @param Error message
*/
public function _Error( $Msg )
{
die( '<span id="error">» Er heeft zich een onverwachtte fout opgedaan:<br /><ul>' . $Msg . '</ul></span>' );
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>POP3 Class Demo</title>
<style type="text/css">
html,body,table {
font-family: verdana;
font-size: 12px;
}
hr {
height: 0;
border: 0;
border-top: 1px solid #555555;
}
</style>
</head>
<body>
<?php
/**
* Example using the class
*/
$pop3 = new POP3;
/**
* Connect to pop3 server
*/
if ( $pop3 -> Connect( 'mail.huizenwireless.nl' ) )
{
echo '» Connection Made!<br />';
/**
* Signin on pop3 server
*/
if ( $pop3 -> Authenticate( 'username', 'password ) )
{
echo ' Authentiacted with the mailserver!<br />';
echo '<hr /><br /><br />';
$Statics = $pop3 -> Information( );
echo '<table>';
echo ' <tr>';
echo ' <td>Mails:</td>';
echo ' <td>' . ($Statics[0]-1) . '</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td>Size:</td>';
echo ' <td>' . $Statics[1] . ' kB</td>';
echo ' </tr>';
echo '</table>';
echo '<br /><br />';
echo '<table>';
echo ' <tr>';
echo ' <td style="width:20%;"><b>From</b></td>';
echo ' <td style="width:20%;"><b>To</b></td>';
echo ' <td style="width:20%; vertical-align:top;"><b>Subject</b></td>';
echo ' <td style="width:20%; vertical-align:top;"><b>Date</b></td>';
echo ' <td style="width:20%; vertical-align:top;"><b>Attachment</b></td>';
echo ' </tr>';
for ( $i = 2; $i < $Statics[0]+1; $i++ )
{
$Mail = $pop3 -> Retrieve( $i );
echo '<tr>';
echo ' <td style="vertical-align:top;">' . $Mail[0] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[3] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[1] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[2] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[4] . '</td>';
echo '</tr>';
}
echo '</table>';
}
$pop3->Disconnect( );
}
?>
</body>
</html>
/**
*
* POP3 Class
*
* PHP Version 5
*
* @author Martijn Dwars (Xanno)
* @copyright Xanno Webdevelopment
* @file class.pop3.inc.php
* @info Please visit Xanno.nl
* @date 13 / 12 / '06
* @version 2.4.0
*/
error_reporting( E_ALL );
set_time_limit( 30 );
Class POP3
{
public $sUser;
public $sPass;
public $sServer;
public $iMails;
public $iSize;
public $sBuffer;
/**
* @desc Connect to the mailserver
* @scope public
* @param The mailserver
* @param The serverport
*/
public function Connect( $sServer, $iPort = 110 )
{
if ( $this -> sServer = fsockopen( $sServer, $iPort, &$iSockerror, &$sSockerror, 1 ) )
{
return true;
}
else
{
$this -> _Error( $sSockerror );
return false;
}
}
/**
* @desc Authenticate with mailserver
* @scope public
* @param Mailbox Username
* @param Mailbox Password
*/
public function Authenticate( $sUser, $sPass )
{
$this -> _Put( "USER " . $sUser );
$this -> _Get( );
$this -> _Put( "PASS " . $sPass );
$this -> _Get( );
$this -> _Get( );
if ( substr( $this -> sBuffer, 0, 3 ) == "+OK" )
{
return true;
}
else
{
$this -> _Error( $this -> sBuffer );
return false;
}
}
/**
* @desc Get some information
* @scope public
*/
public function Information( )
{
$this -> _Put( "STAT" );
$this -> sBuffer = $this -> _Get( );
$this -> iMails = substr( $this -> sBuffer, 4, 2 );
$this -> iSize = substr( $this -> sBuffer, 6, strlen( $this -> sBuffer ) -6 );
return array( $this -> iMails, $this -> iSize );
}
/**
* @desc Retrieve mailinfo
* @scope public
* @param Mail ID
*/
public function Retrieve( $iID )
{
$this -> _Put( "RETR " . $iID );
$this -> _Get();
$this -> _Get();
while( $this->sBuffer <> ".\r\n" )
{
echo "$this->sBuffer";
$this->_Get();
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'x-priority:' )
{
$Retr['priority'] = substr( $this->sBuffer, 12, 1 );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'from:' )
{
$Retr['from'] = htmlspecialchars( substr( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 8 ) == 'subject:')
{
$Retr['subject'] = htmlspecialchars( substr( $this->sBuffer, 9, strlen( $this->sBuffer) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'date:' )
{
$Retr['date'] = htmlspecialchars( substr ( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 3 ) == 'to:' )
{
$Retr['to'] = htmlspecialchars( substr ( $this->sBuffer, 3, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'attachment;' )
{
$Retr['attachment'] = substr( $this->sBuffer, 512, 1 );
}
}
return array( $Retr['from'], $Retr['subject'], $Retr['date'], $Retr['to'], $Retr['attachment'] );
}
/**
* @desc Disconnect connection
* @scope public
*/
public function Disconnect( )
{
$this -> _Put( "QUIT" );
fclose( $this -> sServer );
}
/**
* @desc Write something to server
* @scope public
* @param Command that has to been send
*/
public function _Put( $Msg )
{
fputs( $this -> sServer, $Msg . "\r\n" );
}
/**
* @desc Get response from server
* @scope public
*/
public function _Get( )
{
$this -> sBuffer = fgets( $this -> sServer, 512 );
return $this -> sBuffer;
}
/**
* @desc Write nice errors
* @scope public
* @param Error message
*/
public function _Error( $Msg )
{
die( '<span id="error">» Er heeft zich een onverwachtte fout opgedaan:<br /><ul>' . $Msg . '</ul></span>' );
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>POP3 Class Demo</title>
<style type="text/css">
html,body,table {
font-family: verdana;
font-size: 12px;
}
hr {
height: 0;
border: 0;
border-top: 1px solid #555555;
}
</style>
</head>
<body>
<?php
/**
* Example using the class
*/
$pop3 = new POP3;
/**
* Connect to pop3 server
*/
if ( $pop3 -> Connect( 'mail.huizenwireless.nl' ) )
{
echo '» Connection Made!<br />';
/**
* Signin on pop3 server
*/
if ( $pop3 -> Authenticate( 'username', 'password ) )
{
echo ' Authentiacted with the mailserver!<br />';
echo '<hr /><br /><br />';
$Statics = $pop3 -> Information( );
echo '<table>';
echo ' <tr>';
echo ' <td>Mails:</td>';
echo ' <td>' . ($Statics[0]-1) . '</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td>Size:</td>';
echo ' <td>' . $Statics[1] . ' kB</td>';
echo ' </tr>';
echo '</table>';
echo '<br /><br />';
echo '<table>';
echo ' <tr>';
echo ' <td style="width:20%;"><b>From</b></td>';
echo ' <td style="width:20%;"><b>To</b></td>';
echo ' <td style="width:20%; vertical-align:top;"><b>Subject</b></td>';
echo ' <td style="width:20%; vertical-align:top;"><b>Date</b></td>';
echo ' <td style="width:20%; vertical-align:top;"><b>Attachment</b></td>';
echo ' </tr>';
for ( $i = 2; $i < $Statics[0]+1; $i++ )
{
$Mail = $pop3 -> Retrieve( $i );
echo '<tr>';
echo ' <td style="vertical-align:top;">' . $Mail[0] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[3] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[1] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[2] . '</td>';
echo ' <td style="vertical-align:top;">' . $Mail[4] . '</td>';
echo '</tr>';
}
echo '</table>';
}
$pop3->Disconnect( );
}
?>
</body>
</html>
SanThe.
Code (php)
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
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
while( $this->sBuffer <> ".\r\n" )
{
echo "$this->sBuffer";
$this->_Get();
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'x-priority:' )
{
$Retr['priority'] = substr( $this->sBuffer, 12, 1 );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'from:' )
{
$Retr['from'] = htmlspecialchars( substr( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 8 ) == 'subject:')
{
$Retr['subject'] = htmlspecialchars( substr( $this->sBuffer, 9, strlen( $this->sBuffer) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'date:' )
{
$Retr['date'] = htmlspecialchars( substr ( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 3 ) == 'to:' )
{
$Retr['to'] = htmlspecialchars( substr ( $this->sBuffer, 3, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'attachment;' )
{
$Retr['attachment'] = substr( $this->sBuffer, 512, 1 );
}
}
return array( $Retr['from'], $Retr['subject'], $Retr['date'], $Retr['to'], $Retr['attachment'] );
}
{
echo "$this->sBuffer";
$this->_Get();
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'x-priority:' )
{
$Retr['priority'] = substr( $this->sBuffer, 12, 1 );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'from:' )
{
$Retr['from'] = htmlspecialchars( substr( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 8 ) == 'subject:')
{
$Retr['subject'] = htmlspecialchars( substr( $this->sBuffer, 9, strlen( $this->sBuffer) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 5 ) == 'date:' )
{
$Retr['date'] = htmlspecialchars( substr ( $this->sBuffer, 6, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 3 ) == 'to:' )
{
$Retr['to'] = htmlspecialchars( substr ( $this->sBuffer, 3, strlen( $this->sBuffer ) ) );
}
if ( substr( strtolower( $this->sBuffer ), 0, 11 ) == 'attachment;' )
{
$Retr['attachment'] = substr( $this->sBuffer, 512, 1 );
}
}
return array( $Retr['from'], $Retr['subject'], $Retr['date'], $Retr['to'], $Retr['attachment'] );
}
dus deze staat goed hier in het script
http://www.phphulp.nl/php/scripts/4/1356/
Probeer die eens. IMAP kun je ook prima voor POP3 gebruiken, mits je de juiste flags instelt. Dat script heeft ook geen problemen met attachments e.d.
Probeer die eens. IMAP kun je ook prima voor POP3 gebruiken, mits je de juiste flags instelt. Dat script heeft ook geen problemen met attachments e.d.
Een email met bijlages heeft in ieder geval iets als;
Om aan te geven dat er, naast een bericht, ook bijlages zijn. Die boundary is belangrijk, die geeft namelijk de grens aan van een bericht en/of bijlage.
Niet veel later komen we die boundary al tegen;
Code (php)
1
2
3
4
5
2
3
4
5
------=_20090121165504_48105
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
test 3
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
test 3
Niks aan de hand, dit is gewoon de inhoud van het bericht. Gevolgd door;
Code (php)
1
2
3
4
2
3
4
------=_20090121165504_48105
Content-Type: image/png; name="Afbeelding 1.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Afbeelding 1.png"
Content-Type: image/png; name="Afbeelding 1.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Afbeelding 1.png"
En nu word het interessant... Er is zo te zien een png'tje toegevoegd met de naam Afbeelding 1.png. Mooi! Wat we vervolgens zien is enkele paginas met random meuk;
Dit is de afbeelding zelf. Het einde van die afbeelding is daar waar de boundary weer word neergezet. Met de functie base64_decode() kunnen we hier weer een normale afbeelding van maken.
Bedankt hier kan ik wel iets mee denk ik
base64_decode()
van al die meuk er weer een .wav van kan maken
Alvast bedankt
EDIT : Laat maar ik heb hem al
$this -> _Put( "DELE " . $iID );
Gewijzigd op 01/01/1970 01:00:00 door Jeroen van Welzen
met het substr commando stopt hij telkens waneer ik een spatie tegenkom
Is er misschien iemand die nog een voorbeeld script heeft liggen
Alvast bedankt