Email Uploads

End Goal: Import content from several e-mail addresses utilizing cron.

Scope: To help other people understand how to create the connection between the feeds importer and Mailhandler Mailboxes.

There are two modules involved, for information on how to install them, please refer to their readme files.

  • Feeds
  • Mailhandler

There are four basic areas to understand and set up correctly

  • Setting up the e-mail addresses
  • Setting up a basic Feed
  • Automating the import by creating the connection between the e-mail addresses and the feed
  • Creating the connection between the feed and the content type of choice.

Setting up the e-mail addresses

The Mailhandler is set up to handle three e-mail protocols. This tutorial deals with the IMAP protocol.

  1. Before starting, send a test e-mail to the e-mail account.
  2. Location for setting up the e-mail addresses in the Mailhandler Module
    1. Drupal 6: Site Building-> Mailhandler Mailboxes
    2. Drupal 7: Structure->Mailhandler Mailboxes
  3. Create New Mailbox
    1. Protocol: IMAP
    2. Port: 143
    3. Extra commands: Review options, test as needed
  4. The module will test the account and list the number of e-mails in the account
  5. Continue until the e-mail connection works.

Setting up a basic Feed

Before we do the heavy lifting, setup and test the default mailhandler to make sure the connection between the mailhandler and the e-mail account works. We will be adjusting settings after we have confirmed that the basic settings are correct. 

  1. Location for setting up feeds in the Feeds module
    • Drupal 6: Site Building->Feeds-> List  ($baseurl/admin/build/feeds/list)
    • Drupal 7: Structure->Feeds->List 

       ($baseurl/admin/structure/feeds/list)

  1. Consider cloneing the Mailhandler and then check the following settings on the clone.
    • Basic Settings – Attached to
      • Leave this attached to [none] for now
    • Fetcher: Mailhandler fetcher
    • Message Filter: Nodes Only
    • Parser: Mailhandler IMAP stream parser
    • Mailhandler: IMAP stream parser settings keep default settings
    • Node processor: keep default of story
    • Create and Update nodes:
      • Set Subject to title
      • Body (text) to Body
  1. Send an e-mail to the e-mail account
  2. Go to $baseurl/import
  3. Choose the Mailhandler
  4. Choose the e-mail address to import from
  5. Import
  6. The software will display a notification if a node as been imported.  If this does not work, please recheck the settings until it does work.

Automating the Feed import

There are a few concepts to be aware of, the Feeds module will handle the connection from the e-mail account to a particular content type and handle mapping of information from the node to the content type of choice.  It will need two content types (discussed below) to do this.  The first content type creates the connection between the Mailhander Mailbox and the E-mail, the second is the content type of choice for data in the e-mail address.

Creating Connection Content Type

Creating a connection content type automates the feed import process.  This may take time to understand, so be patient. A second content type will be needed to store the information from the e-mail.

  1. Create a new content type – suggestion use Mailhandler Source
    • Submission Settings – Title Field: Title, delete Body Field Label
    • Workflow Settings – Published
    • Save
  1. This is the challenging part – how to create the connection. Normally we would add a field for Mailbox when we created the content type. Not true in this case.
  • In this case go to the Feeds Importer List. 
  • Choose the mailhandler, then Basic Settings.  Attach to: set this to attach to the Mailhandler Source. This adds a field listing the e-mail addresses available.
  1. Keep the rest the same – we know it works. We did much more than add a field to a content type, we created the connection from the Feeds Importer to a content type.
  2. Now, create content– Mailhandler Source. There is a new field – Mailbox. Choose the e-mail address.
  3. Save
  4. Importing has been automated.  Going to the $baseurl/import will not work anymore. (If you try, you will get a new content form). Now we have to run the cron to import.
  5. Test. 
  6. Keep in mind that we will need a Mailhandler Source node for each e-mail address.

Creating the connection between the feed and the content type of choice

One way to utilize the Mailhandler and Feeds modules is to have each e-mail address map to a specific content type, ie blogs, page, story, etc.  Note: For each e-mail address storing its information in a different content type a new importer and connection content type will need to be made.

  1. To accept file attachments go to the Mailhandler IMAP stream parser and make sure the File Processor command plugin is chosen.  Add all three commands.
  2. If you have CKEditor installed, make sure to remove the fields from the view.  Check the CKEditor: the ID for excluding or including this element just below the text field.
  3. Node Processor Setting: Choose the content type and import format
  4. Save
  5. Create and Update Nodes Mapping
  6. Map fields as needed.
  7. Save
  8. Test

Another tutorial to explore: http://www.fieldguidetoprogrammers.com/drupal/using-drupals-mailhandler-... I find myself going back to this tutorial.

Remember to use this patch!

 .../mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php        |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php b/modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php
index 605f4b5..208d336 100644
--- a/modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php
+++ b/modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php
@@ -233,7 +233,7 @@ class MailhandlerPhpImapRetrieve extends MailhandlerRetrieve {
           $prefix = $part_number . '.';
         }
         $sub_parts = $this->get_parts($stream, $msg_number, $max_depth, $depth + 1,
-          $sub_structure, $prefix . ($index + 1));
+          $sub_structure, ($index + 1));
         $parts = array_merge($parts, $sub_parts);
       }
       return $parts;