Parsing the Messages
In the first article in this series, we connected to our mail server using PHP's imap functions. We were able to search our mailbox and extract the messages we wanted. Now we need to get that data into the database.
Parsing the Messages
We left our script with a loop to display the messages that were found with the subject line containing the word "Submission". We need to replace that echo command with a block of code that parses the message and breaks it up into the parts we need for the site_content table.
If you followed the link in the previous article, you saw that the second message had a specific format. The output was displayed all on one line because the browser does not recognize text line breaks. But we can break up that message into its component parts using the line breaks in the original message as delimiters.
In a loop, we'll process each message:
$arr = explode("\n", $message); // break it up by lines
$pagetitle = array_shift($arr); // shift off the first line
$createdby = array_shift($arr); // shift off the second line
$introtext = array_shift($arr); // shift off the third line
$content = implode("\n", $arr); // reassemble what is left
And here is what you get.
As you can see, a bit of experimentation was in order to decide how best to have the email formatted. It seems easy enough to have the page title on the first line, the author's name on the second line, a brief summary on the third line, and the rest of the message will be the content. As long as the submitter makes sure to use the "enter" key to put the three first items on their own line, the rest will be taken care of. The main body part can even be pasted from a word processor. It can have all of the HTML or snippet/chunk/TV tags that the content field in the Manager can have.
Into the Database
Now that we have the items we need, it's a simple step to cleaning it up and creating the query for inserting the post into the database.
// won't be needed when this is made into a module
include "manager/includes/config.inc.php";
$conn = mysql_connect($database_server, $database_user, $database_password) or die(mysql_error());
$db = mysql_select_db(trim($dbase, '`')) or die(mysql_error());
// clean it up
$pagetitle = mysql_real_escape_string(trim($pagetitle));
$introtext = mysql_real_escape_string(trim($introtext));
$content = mysql_real_escape_string(trim($content));
$createdby = mysql_real_escape_string(trim($createdby));
// get submitter's ID
$sql = "SELECT internalKey FROM " . $table_prefix . "user_attributes WHERE fullname = '$createdby'";
$res = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($res);
if($num == 1) {
$createdby = mysql_result($res, 0);
$sql = "INSERT INTO " . $table_prefix . "site_content
(pagetitle, introtext, content, parent, template, createdon, createdby)
VALUES
('$pagetitle', '$introtext', '$content', $parent, $template, time(), '$createdby')";
mysql_query($sql) or die(mysql_error());
}
Here's the whole file. You can log in to the Manager using the guest login (guest - guestuser) and see the two qualifying posts in the Demos folder; IDs 24 and 51.





