Tutorial: Using one email account to import mails into Drupal.

- Tags:

We use the mail import feature of Drupal in our apps to accept content like calls and invoices coming from multiple sources. This tutorial shows how to work with just one email account for multiple sources/clients to import mails into drupal. It works with aliasing/nicknames. This way you can use one email account for multiple email addresses to import mails into different drupal sites. This tutorial was also partly published at http://drupal.org/node/207366#comment-4878904. The first part is taken from the install.txt from the Mailhandler module (thanks to Dane Powell).

Make Drupal ready for mail import

Install and activate the following modules:

  • Mailhandler
  • MimeRouter
  • MimeDetect
  • Chaos tools
  • Feeds
  • Job Scheduler

Mailhandler 2.x requires configuration of two primary components: a mailbox, which stores access information for an IMAP/POP mailbox such as the username and password, and a feeds importer, which checks and retrieves messages from a mailbox.

  • First, add at least one mailbox configuration at admin/build/mailhandler. Email address: accountname@gmail.com Folder: INBOX (the last part of this blog will deal with subfolders)
  • Pop3 or IMAP: IMAP
  • Mailbox Domain: gmail-imap.l.google.com (this is the imap server that I used. imap.google.com did NOT work)
  • Mailbox Port: 993
  • Mailbox Username: accountname@gmail.com
  • Extra commands: /ssl/novalidate-cert
  • Check delete messages after they are processed.

Then, ensure that Feeds Admin UI is enabled and add a new feeds importer at admin/build/feeds (click "New Importer"):

  • Enter a name and description for the new importer and click 'next'.
  • Change the fetcher to "Mailhandler fetcher". For the fetcher settings, choose which types of messages to retrive (nodes, comments, or all). Note: if this importer fetches a type of message that the processor (below) does not support, that message will be marked as read or deleted! Thus, it's important to set this filter appropriately.
  • Change the parser to "Mailhandler IMAP stream parser". For the parser settings, you can choose plugins to handle commands and authentication as well as set default commands. For instance, setting the default commands to "status: 1" will publish all posts (if the processor, below, is configured appropriately)
  • Change the processor to "Node processor". If you would like to import comments by email, you can download Mailcomment and Feeds Comment Processor and select the Feeds Comment Processor instead. For the settings, choose (among other things) whether authorization should be performed- in other words, whether the post author has permissions to actually post content (by default Feeds doesn't check this). For the mapping, you should at least map "subject" to "Title", "origbody" to "Body", and "Authenticated UID" to "User ID". You can also map commands (such as "Published status") and attachments. Note that if you don't map things correctly here, certain features that you configured earlier (such as authorization and commands processing) will not work.
  • Now, if you did not attach your importer to a specific content type, you can go to /import, choose from the drop down the mailbox you've just setup to import from, and select "Import" which should import your messages. If you attached your importer to a content type, you'll need to create a new node of the corresponding type, choosing from the node form which mailbox to tie to that node, save the node, and then you'll be able to import from the node view of that node.

Finally, you may want to filter out certain parts of messages such as signatures and quoted replies. To do this, simply configure whatever Input Format you have chosen in the feeds importer and select one of the Mailhandler message cleaners.

If you get the message: "Mailhandler requires that PHP's IMAP extension is enabled in order to function properly." please install php5-imap:

  • sudo apt-get install php5-imap
  • sudo /etc/init.d/apache2 restart (or in case of nginx: sudo /etc/init.d/nginx restart; sometimes it is already sufficient to restart php5).

If you get the message: "Fileinfo could not load the magic file". Add the following lines to the settings.php

$conf = array( 'mimedetect_magic' => '/usr/share/file/magic', );

 

Subfolders mailbox (Google Accounts)

  • Create an alias for the mailbox you want to use.
  • Go to your mail account on Google Accounts. Click on manage this domain (you need admin rights)
  • Admin window Google Apps open. Go to Organization & Users.
  • Click on user.
  • Go to Nicknames. Click on Add a nickname. Create an alias / nickname, say for example clientx@abc.com.

Create subfolder / Label.

  • Go to gmail. On the left hand site click on '4 more' (in my case, maybe this is different for other users). Click on 'Create a new label' .
  • Create a new label. I named it INBOX/Clientx

Create a filter

  • Go to gmail.
  • Click on 'Create a filter' (next to the search button).
  • In the To: box, enter the alias/nickname (in my case clientx@abc.com). Click on Next.
  • Select 'Apply the label' and choose the label just created (INBOX/Clientx) Now you can send an email to clientx@abc.com. This mail will be routerd to INBOX/Clientx in gmail. You can use the Mailhandler to pick up mails from this folder/label. Please use as a folder name the complete label (that is INBOX/Clientx).