Using Drupal’s mailhandler module

Drupal’s mailhandler module allows you and your visitors to email new content into your site. This is a powerful module because it allows you to create any content whatsoever. In addition, with the application of another module named listhandler, you can turn your forums into a post-by-email community.

The mailhandler module is very powerful and so of course it is also poorly documented. :) This is my humble attempt at distilling what I have learned about the mailhandler module. It is by no means exhaustive.

Setup

To setup the mailhandler module, begin by downloading and installing the module as you would any other Drupal add-on. Once you have the mailhandler module code installed in your Drupal environment (i.e. ftp the mailhandler folder to drupal/modules), you’re ready to start configuring.

Creating an email address

To start with, you need an email account for mailhandler to check. mailhandler is capable of checking POP or IMAP mailboxes so either should be fine. When setting up your email account, there are three things to keep in mind:

1. You will be giving this email address out to your users.

2. You may end up creating more than one mailhandler.

3. Drupal stores the password in cleartext when you set up the account, so you may not want to use the same password or rubric you have for other critical passwords.

The first consideration is obvious: you want to create an email address that is fairly memorable. I’ll address the second consideration later on.

A word on Security

mailhandler has built-in security to protect your site from malicious and spam posts. First, it will only accept emails that come from registered users, using the email address to validate the sender.

If you are concerned about spoofing, you can also require users to supply their Drupal password in the body of their email. [I would also recommend setting up your mail server to enforce SPF records for inbound email, but this is probably a bit much for the casual site admin. Still, if you enforce SPF records for in-bound email you will eliminate most if not all spoofing attempts.]

Configuring the mailhandler

The mailhandler configuration is located under Administer->Content Management->Mailhandler.
Begin by clicking “Add Mailbox”. This will lead you to a long form of configuration options. I’ve replicated the list below:

E-mail address:
The e-mail address to which users should send their submissions.

Second E-mail address:
Optional. The e-mail address to which modules should send generated content.
[This is used by other modules within the Drupal engine to send outbound email. For example, if you have installed the listhandler module, this field would be used to keep your mailing list updated with the latest forum posts.]

Folder:
Optional. The folder where the mail is stored. If you want this mailbox to read from a local folder, give the full path. Leave domain, port, name, and pass empty below. Remember to set the folder to readable and writable by the webserver.
[This is useful if you have mail rules that sort incoming messages into different folders. It really depends on how you want to organize the mailhandler accounts, but I would probably favor setting up different addresses instead of fussing about with this.]

POP3 or IMAP Mailbox:
If you wish to retrieve mail from a POP3 or IMAP mailbox instead of a Folder, select POP3 or IMAP. Also, complete the Mailbox items below.

Mailbox domain:
The domain of the server used to collect mail.

Mailbox port:
The port of the mailbox used to collect mail (usually 110 for POP3, 143 for IMAP).

Mailbox username:
This username is used while logging into this mailbox during mail retrieval.

Mailbox password:
The password corresponding to the username above. Consider using a non-vital password, since this field is stored without encryption in the database.
[Yes, the password is stored in clear text.]

Extra commands:
Optional. In some circumstances you need to issue extra commands to connect to your mail server (e.g. “/notls”, “/novalidate-cert” etc.). See documentation for imap_open. Begin the string with a “/”, separating each subsequent command with another “/”.
[I needed to add the /novalidate-cert option as my mail server has a self-signed certificate. If that doesn't make a whole lot of sense to you, it might when you try to connect for the first time. :) ]

Mime preference:
When a user sends an e-mail containing both HTML and plain text parts, use this part as the node body.
[You should probably set this to HTML. Chances are some of your users will have an HTML mail client.]

Security:
Disable security if your site does not require a password in the Commands section of incoming e-mails. Note: Security=Enabled and Mime preference=HTML is an unsupported combination.

Send error replies:
Send helpful replies to all unsuccessful e-mail submissions. Consider disabling when a listserv posts to this mailbox.
[I think the suggestion of "consider disabling" is more of a requirement. Nothing says newbie like auto-responders to a listerv.]

From header:
Use this e-mail header to determine the author of the resulting node. Admins usually leave this field blank (thus using the From header), but Sender is also useful when working with listservs.
[This is an interesting feature. You can set this up to authenticate posts on any header in the email message.]

Default commands:
A set of commands which are added to each message. One command per line.
[This is very important. Using this field, you can set up automatic posting to forums, blogs, etc. This makes it easy for your users to post new content without going crazy with commands in the body of their email. Below I will show some of the commands you can use in this field or in the body of the email, but spend some time thinking about how you can make things easier for your users by utilizing this field.]

Signature separator:
All text after this string will be discarded. A typical value is “– ” that is two dashes followed by a blank in an otherwise empty line. Leave blank to include signature text in nodes.

Delete messages after they are processed?
Uncheck this box to leave read messages in the mailbox. They will not be processed again unless they become marked as unread.

Cron processing:
Select disable to temporarily stop cron processing for this mailbox.
[I kept this turned off while testing, but I suppose it wouldn't hurt anything to have it on.]

Input format
[This is the format for the inbound entries. I found this to be one of the more frustrating parts about mailhandler. Basically, you should be able to set this on the fly for each new post by including the directive in your message. Unfortunately, I couldn't get it to work. So, from my perspective, this is where you MUST set the format. If I figure out why this doesn't work for me, I will update this accordingly.]

The first run

Once you’ve set up the mailhander, you will end up back at the list of existing mailhandlers. Unless you’ve already been this way before, there should be one handler in the list. You can fire off the mailhandler by clicking on the “Retrieve” link. Try it out!
If everything worked, you should see some nice green text:

Mailhandler retrieve successful: 0 messages for xxxx@somesite.com

If not, check out the Extra Commands section above. Also, make sure you are accessing your mail server on the right port. POP runs on 110 by default, but some hosts change this for a variety of reasons.

Commands

The mailhandler module parses the body of your email message for commands and then for content. As noted in the configuration section above, you can define all the commands for inbound messages in the setup and absolve your users from the need to do anything other than write luscious prose.

Of course, you are a control freak and you want to micromanage each and every post you throw up there. So, you’ll probably put the commands in your email body anyway.

With that in mind, a basic list of controls appears below. I’ve cribbed these from a variety of places (including the source code), so your mileage may vary on usage. When in doubt, try it out. In your email body, list each control on a separate line followed by a colon and the value:

type: (values are: blog [default], story, page, forum, comment)

taxonomy: [SectionName] (categories for post, separate multiples with a comma. Also, this needs to be in brackets)

promote: (0 or 1. Use 1 to put the content on the front page)

status: (0 or 1. Use 1 to publish the content immediately)

comment: (0, 1, or 2. 1 is read-only comments, 2 is open comments, and 0 is no comments)

format: (1,2,3. 1 is Filtered HTML, 2 is PHP, 3 is Full HTML. Like I said above, I did not get this to work but I found a reference to “depends on your input filter settings” so I will try once more)

moderate: (0 or 1. 0 NOT in moderation queue. 1 in moderation queue)

revision: (0 or 1. 0 is no revision, 1 is a revision)

sticky: (0 or 1. 0 is not sticky, 1 is sticky at the top of the page)

title: (DO NOT USE THIS, use the mail subject only, if you use both, the title will be a combination of both subject and title like “Welcome ! Welcome !”)

path: (to set a URL alias)

References:
better documentation for nonprogrammer users
Drupal mailhandler Tutorial
Useful command for mailhandler default commands (make sure to read the comments on this post as the post itself is a bit cryptic)

Mailhandlers Galore!

If you think about it, I’m sure you’ll have ideas about how you might use mailhandlers to create lots of quick methods for posting content to your site. For example, I’ve been thinking about ways to archive automatic daily sales reports and other system generated emails into a huge archive of “what’s going on at work”. A few mailhandlers would make quick work of this…

Another example might be archiving emails from people I know in a personal Drupal site. I set up a few filters on gmail and forwarded messages are soon streaming into my website.

Then of course there is just the posting of content. Articles like this one for example!

21 Comments

  1. [...] Salah satu fitur adalah setiap user yang terdaftar dalam web ini harus mendapatkan notifikasi imel jika terdapat content terbaru. Dan juga sebaliknya, user harus bisa melakukan posting melalui email untuk masuk sebagai content di web. Hal ini penting jika server down atau malah user lebih suka interaksi melalui email. Hal ini bisa dilakukan dengan module workflow dan actions. Atau video tutorialnya yang detail bisa dilihat disini. Untuk bisa mengirim imel module smtp harus diaktifkan terlebih dahulu. Dan untuk menerima posting dari email bisa menggunakan module mailhandler. Tutorial mailhandler bisa dilihat disini. [...]

  2. jamiegrove says:

    Link to an Indonesian Drupal article appears above (not comment spam).

    Makasih, bro!

  3. doug says:

    Thanks! This post saved me lots of time getting mailhandler up and running. One thing to add: I found that the “taxonomy:” command can only put things into existing categories. At the moment, it seems that you can’t use mailhandler to create new terms, even if you’re using free-tagging.

  4. jamiegrove says:

    Glad it helped, Doug! And thanks for posting the note about taxonomy.

    Perhaps the drupal gods will fix this in rev 6 (just installed it locally but haven’t played with it too much yet).

  5. JK_Drupal says:

    Hi!

    I would like to ask if I were to reply via email (comment) to a node, how will mailhandler know that my e-mail is meant as a comment for that node?

    Apart from writing commands, is there any other way? Because it is unlikely my members will want to write commands just to reply via email (comment) on a node.

  6. jamiegrove says:

    Hi, JK. I hear what you’re saying. Unfortunately, mailhandler won’t make it that simple. I did find this in the documentation though:

    Also, in the headers of the email, if you put

    In-Reply-To: nid=5

    then the email becomes the new version of node 5. I’m told that there’s a way to take it down to the level of comments, presumably by using In-Reply-To and the comment id#, but I haven’t checked that.

    My recommendation would be to use the comment_notify module. That would send an email when there are new comments with a link for replies.

  7. klaus says:

    Hi,
    I’m using Mailhandler only for submissions. However I couldn’t adjust the Drupal filters to display emails in an acceptable way. So the site only works in a fashion. Aplying a filter that does no filtering at all displays all emails quite nice, but then the drupal theme gets kind of broken, colours are changing and colums distorted. Filtered HTML works best but many nodes still look bad. Also the default commands
    taxonomy: [Forum]
    do not take the nodes into the forum sections. This may be due to my poor understanding of Taxonomy.

    Well, don’t want to waste your time, if you can give me some hints, I’ll be happy to work myself though this stuff…..

    regards

    Klaus

  8. jamiegrove says:

    Hi, Klaus.

    Are you sending HTML or rich text formatted email? If so, you want to be sure to include a format directive in your message. The mailhandler doco says that Full HTML is best:

    http://drupal.org/node/39172

    Of course, you’re saying that filtered HTML looks better. That makes me wonder what other formatting is built into your message. What email client are you sending from? Have you tried making sure that the message is going as plain-text?

    On the Taxonomy side, the forum should be:

    the “term” in the “forums” vocabulary that was automatically created when a forum named “My Forum” was created (see Admin->Categories).

    http://drupal.org/node/38943

    This worked in my install.

  9. Seige says:

    Hi,

    I am trying the mailHandler module and have configured it with GMAIL perfectly. The problem is that when mailHandler imports the mail, it creates a node with type “”. I have explicitly specified type: page in the commands list.

    Any clue why ??

  10. jamiegrove says:

    Hi, Seige.

    Is the node empty? Or just the type? In other words, does it actually pull in the information?

    Also, do you have /ssl/novalidate-cert in the extra commands section?

  11. Seige says:

    The node is created. I can even see my content. But the type is null. Also, If I try to edit the page, I get this error :

    warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘blog_node_form’ was given in /var/www/drupal/includes/form.inc on line 218.

    Yes, I do have /ssl/novalidate-cert in the extra commands.

  12. Seige says:

    Allright, I figured out something more.I have opened an issue for this on the module page on drupal as well and there someone suggested that enabling of “blog” module which i had not done.

    So i enable the module and try it again. This time my mail gets transformed into a node with type “blog entry” and i can edit it too.
    So atleast whatever content i email, a node is created for that which is editable.

    Now, I dont want to create a “blog entry” node. I want a page node to be created of every page i email. I have type: page in my commands list. Any further suggestions on how to get a node of type “page”.

  13. jamiegrove says:

    @Seige Do you have a space between type: and page? Have you tried it both with and without?

    I was going to recommend this thread on drupal.org but I see you are already there. :)

    http://drupal.org/node/153117

  14. Seige says:

    Yeah, I do have it now. If you dont have it , then the module just appends them to the content.

    Here is exactly what i have in that textarea:

    type: page

    status: 1

    format: 3

  15. jamiegrove says:

    Do you have an extra carriage return between each command? If so, try removing the extra empty line.

    The only other thing I can think of is disabling all non-standard modules to see if you have a conflict.

    If none of that works, I’m out of suggestions.

  16. Seige says:

    Allright, I had this initially
    type: page
    status: 1
    format: 3

    which I changed to

    type: page
    status: 1
    format: 3

    Since, the type,status and format commands are not working at all, there seems to be a problem with the format in which i am giving these commands.

    As far as modules are concerned, I am not using anything extraordinary.However, I do have TinyMCE enabled.

  17. jamiegrove says:

    FYI… I zapped all the dupe comments.

    If there is nothing special in terms of modules, I think your reasoning on the formatting of the commands may be sound. What about turning off TinyMCE?

  18. Seige says:

    I turned off TinyMCE ! The commands type: page and status: 1 are working now.
    Everything works now except the format:3 command to allow complete HTML in the posts.

    I might be wrong in the understanding of it though.

    I am trying to email this :

    this is (tag strong) strong (end tag strong).

    However, instead of bolding the word, it imports it exactly like this.

  19. jamiegrove says:

    Good deal! (make sure to update your forum post on drupal.org as I know folks are probably checking that)

    Format 3 works, but I believe the message itself needs to have a MIME type of HTML and not be plain-text.

  20. TJ says:

    I’m working on a way to send an email to my website with location information (either city/state, an address, or even a geotagged photo), and I’d like to use the mailhandler. For this phase, all I need it to do is read the put the content into the correct fields of a custom content type. Something like:

    City: Los Angeles
    State: CA

    Since the module can hangle Taxonomy as a separate field, I’m guessing this is pretty easy, but I wonder if it will do it automatically per content type or if it will require a hack in order to accept custom fields.

    If anyone has knowledge on this, I’d really appreciate some advice.

    Thanks for your help!
    TJ

    TJ’s last blog post..Vizrt, meet TJ… TJ, meet Vizrt

  21. jamiegrove says:

    @TJ You should be able to do it with CCK. This thread has a bit of code that explains (about half way down). I don’t know if it will work, but you might want to try putting a note on the thread.

    http://drupal.org/node/141211