PHP Gmail Imap Connection with Database Insert

gmail php mysql imap
Using PHP, We will connect to a Gmail mailbox and pull down the last message headers and to and from email addresses.
With this data, we will insert it into a MySQL Database and then delete the message from the IMAP Server.

LET ME KNOW IF THIS HELPED YOU IN THE COMMENTS! Enjoy!

Here is the code:

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
<?php
	$dbhost = 'localhost';
	$dbuser = 'DATABASE_USER';
	$dbpass = 'DATABASE_PASSWORD';
	$dbname = 'DATABASE_NAME';
 
	$imapserverstring = 'imap.gmail.com:993/imap/ssl/novalidate-cert';
	$imapuser = [email protected]';
	$imappass = 'GOOGLE_PASSWORD';
 
 
	$mbox = imap_open("{".$imapserverstring."}INBOX", $imapuser,$imappass)	or die("can't connect: " . imap_last_error());
 
	$check = imap_mailboxmsginfo($mbox);
 
	if ($check) {
		echo "Date: "     . $check->Date    . "<br />\n" ;
		echo "Driver: "   . $check->Driver  . "<br />\n" ;
		echo "Mailbox: "  . $check->Mailbox . "<br />\n" ;
		echo "Messages: " . $check->Nmsgs   . "<br />\n" ;
		echo "Recent: "   . $check->Recent  . "<br />\n" ;
		echo "Unread: "   . $check->Unread  . "<br />\n" ;
		echo "Deleted: "  . $check->Deleted . "<br />\n" ;
		echo "Size: "     . $check->Size    . "<br />\n" ;
	} else {
		echo "imap_check() failed: " . imap_last_error() . "<br />\n";
	}
 
	echo "<h1>Headers in INBOX</h1>\n";
	$headers = imap_headers($mbox);
 
	if ($headers == false) {
		echo "Call failed (or no messages)<br />\n";
	} else {
		foreach ($headers as $val) {
			echo $val . "<br />\n";
		}
	}
	$FirstMessageArray = implode('', array(imap_fetchbody($mbox, 1, "")));
	echo $FirstMessageArray;
	$subject = "";
	$message = "";
	$to = substr  ( $FirstMessageArray ,  0,  10 );
	$from = "";
 
 
 
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
	mysql_select_db($dbname);
	$query  = "INSERT INTO `DatabaseTable` (`id`, `to`, `from`, `subject`, `message`) VALUES (NULL, '{$to}', '{$from}', '{$subject}', '{$message}');";
	$result = mysql_query($query);
	mysql_close($conn);
 
	imap_delete($mbox, 1);
	imap_close($mbox);
?>
Facebooktwittergoogle_pluspinterest

5 thoughts on “PHP Gmail Imap Connection with Database Insert”

  1. trying to get this to work with our local imap server.. I can connect through imap but the database insert is not happening not sure why… also – noticed that it looks like you are only inserting message “1” is that correct?

    1. Hmm, well I think this line here works when checking gmail:
      $FirstMessageArray = implode(”, array(imap_fetchbody($mbox, 1, “”)));

      But, you could try doing something like:
      print_r(array(imap_fetchbody($mbox, 1, “”)));
      or print_r(imap_fetchbody($mbox, 1, “”));

      and see what the imap is pulling back.

  2. at line 50:
    $query = “INSERT INTO `DatabaseTable` (`id`, `to`, `from`, `subject`) VALUES (NULL, ‘{$to}’, ‘{$from}’, ‘{$subject}’, ‘{$message}’);”;

    you missed , `message` must be

    $query = “INSERT INTO `DatabaseTable` (`id`, `to`, `from`, `subject`,`message`) VALUES (NULL, ‘{$to}’, ‘{$from}’, ‘{$subject}’, ‘{$message}’);”;

Comments are closed.