We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I’ve set it up to parse simply on line breaks.
    <?php
    // script to access domain mail and gather content
    // of mail intended for insertion into 
    // the site_content table.
    // Variables:
    // $uname: mailbox username
    // $upass: mailbox password
    // $mailserver: mail server to access
    // $parent: the document ID to set as this document's parent (for Ditto call)
    // $template: the template to use for this document
    // to-do: make this a module and set these in the module configuration
    
    $uname = '[email protected]';
    $upass = 'password';
    $mailserver = 'mail.domain.com';
    
    $parent = 3;
    $template = 3;
    
    // connect to the mail server
    $mbox = imap_open("{localhost:143}INBOX", $uname, $upass);     
    $listing = imap_search($mbox, "SUBJECT Submission");
    if(imap_num_msg($mbox) > 0) {
        foreach($listing as $msg) {
            $message = imap_body($mbox, $msg);
            $arr = explode("\n", $message); // break it up
            $pagetitle = array_shift($arr); // shift off the first three lines
            $createdby = array_shift($arr);
            $introtext = array_shift($arr);
            $content = implode("\n", $arr); // put the rest of it back together
                    
            // 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());
            }
            
        }
    }
    

    It works as a stand-alone script. The next step will be to make a module out of it.
    http://www.sottwell.com/email-posting.html
    http://www.sottwell.com/email-posting-2.html
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Of course, if you want to make an automated process out of it, you would want to add a notification email function to it. There’s a number of snippets/modules that do this already, so it wouldn’t be difficult to grab the code from one of them.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 25663 MODX Staff
        • 12,272 Posts
        Why couldn’t you have it just parse the email as follows:

        From name, not the email address into the "author".

        Subject (Submission: My Cool Mobile Title Here) after stripping and trimming whitespace becomes "My Cool Mobile Title Here" and becomes the headline, which is passed through a template and wrapped in an HX tag.

        The email body itself becomes the content, stripping out any HTML that would cause problems (allow only a very small subset of HTML tags) and wrapping things in paragraphs that are just blocks of text separated by newlines..
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          This would work, if your user was always posting from the same email address... maybe he’s visiting a relative and uses the relative’s computer to post? Anyway, this should be simple enough as it is for anyone to be able to modify it to suit their own ideas. As usual, I expect this to be mostly a proof-of-concept general idea sort of thing that will need some refining for individual preferences and needs.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 27376
            • 576 Posts
            A great idea would be to integrate this functionality into each manager user because that way you have an email address field there already. This would tie one email account to each registered user, which I prefer for security reasons (imagine anyone being able to email your server undecided). The module would then compare the "from" email address to the manager user table and grab the UserID that way. If there’s no match, the email is discarded (or reported in the Event Log).

            A customizable subject prefix key would be good, but in my setup I will be dedicating an entire email account to be used so would have no need for it.

            Now that I’m on the idea of a subject key, Maybe you could setup a number of subject key ’selectors’ (eg. [chunk], [template], [blog], etc.) that would handle each email according to its selector.

            Hmm, this opens so many new doors.... Thanks for writing your tutorial Susan, this is something I’ve been wanting to do for a long time! smiley
              • 7231
              • 4,205 Posts
              Yes, this is a very exciting idea. I can’t wait to see what evolves from here.
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don&#39;t know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                The code I wrote does check for an existing manager user name, and drops the message being parsed if a match is not found, but using the email address would be a better way, and have the advantage of avoiding the need to parse the message for the name.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org