imap open en mysql
ik ben een volledige nieuwkomer op php gebied en ik ben bezig met een script om mail van een imap server te downloaden en naar een mysql database te brengen.
Ondanks men gebrek aan kennis heb ik een script kunnen samen rapen, ik heb het nog niet getest omdat ik vermoed dat er dingen niet goed zijn, heeft er iemand me advies of opmerkingen die fout zijn in dit script?
men script moet volgende doen : imap stream openen, alle nieuwe mail ophalen, afzender, datum, onderwerp, cc en bcc en het body in de database pluggen.
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
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
<?php
// begin of the script
// *******************
// Php script to
// fetch imap in sql
// *******************
$mailserver = "incoming.mailserver.com";
$mailuser = "username";
$mailpass = "password";
$db_name = "mysql_db_name";
$db_host = "mysql_hostname";
$db_usr = "mysql_username";
$db_pass = "mysql_password";
$timestamp = "Y-m-d H:i:s";
// *******************
// connect msql db
// *******************
mysql_select_database($db_name,mysql_connect($db_host,$db_usr,$db_pass));
// STILL NEED AN IF FAIL MESSAGE
// *******************
// open imap stream
// *******************
if($stream = @imap_open("{$mailserver:143/imap}INBOX", $mailuser, $mailpass)
or die("Connection to server has failed while trying to open the imap stream"))
{
// check
stream for new messages
$streamnumber = imap_num_msg($stream);
if($streamnumber >0){
// get all new messages
for($mnum = 1;$mnum <= $streamnumber; $mnum++){
// fetch body
$bodymail = imap_fetchbody($stream,$mnum,1);
// fetch headers
$header = imap_headerinfo($stream,$mnum);
}
}
// *******************
// insert into database
// *******************
$sql = "INSERT INTO `".$prefix."mail` (`timestamp`,`sender`,`subject`,`bodymail`,`cc`,`bcc`,`to`) VALUES ('".$timestamp, strtotime($header->date))."','".$header->fromaddress."','".$header->subject."','".onpost($bodymail)."','".$header-> ccaddress."','".$header->bccaddress."','".$header-> toaddress."')";
// perform insert
mysql_query($sql);
// delete complete mail
imap_delete($stream,$streamnumber);
// *******************
// closing connection
// *******************
// clean mailbox
imap_expunge($stream);
// show imap errors
$errors = imap_errors($stream);
// close imap stream
imap_close($stream);
// end of the script
?>
// begin of the script
// *******************
// Php script to
// fetch imap in sql
// *******************
$mailserver = "incoming.mailserver.com";
$mailuser = "username";
$mailpass = "password";
$db_name = "mysql_db_name";
$db_host = "mysql_hostname";
$db_usr = "mysql_username";
$db_pass = "mysql_password";
$timestamp = "Y-m-d H:i:s";
// *******************
// connect msql db
// *******************
mysql_select_database($db_name,mysql_connect($db_host,$db_usr,$db_pass));
// STILL NEED AN IF FAIL MESSAGE
// *******************
// open imap stream
// *******************
if($stream = @imap_open("{$mailserver:143/imap}INBOX", $mailuser, $mailpass)
or die("Connection to server has failed while trying to open the imap stream"))
{
// check
stream for new messages
$streamnumber = imap_num_msg($stream);
if($streamnumber >0){
// get all new messages
for($mnum = 1;$mnum <= $streamnumber; $mnum++){
// fetch body
$bodymail = imap_fetchbody($stream,$mnum,1);
// fetch headers
$header = imap_headerinfo($stream,$mnum);
}
}
// *******************
// insert into database
// *******************
$sql = "INSERT INTO `".$prefix."mail` (`timestamp`,`sender`,`subject`,`bodymail`,`cc`,`bcc`,`to`) VALUES ('".$timestamp, strtotime($header->date))."','".$header->fromaddress."','".$header->subject."','".onpost($bodymail)."','".$header-> ccaddress."','".$header->bccaddress."','".$header-> toaddress."')";
// perform insert
mysql_query($sql);
// delete complete mail
imap_delete($stream,$streamnumber);
// *******************
// closing connection
// *******************
// clean mailbox
imap_expunge($stream);
// show imap errors
$errors = imap_errors($stream);
// close imap stream
imap_close($stream);
// end of the script
?>
Er zijn nog geen reacties op dit bericht.