Bear with me on this, don't have much experience with recommending fixes or patches...

I set up a Feed importer to add emails to a site in hopes of associating that email with a contact by using their email address as a reference. In my 'email' content type I set up an email field that allows unlimited values. Using Rules I was going to create a entity reference on create.

Problem is that using the standard field mappings with Mailhandler, only one email address was being added to each node. I tried using Feeds Tamper->Explode using a comma as the delimiter on the full 'To address line' source. But, if the email address had a comma in name portion ("smith, j" ) then the Tamper would obviously split the source up at the wrong points.

So... going through the code I saw this on lines 8-25 of mailhandler/plugins/mailhandler/commands/MailhandlerCommandsHeaders.class.php

        // Some headers are arrays of objects
        if (in_array($key, array('to', 'from', 'reply_to', 'sender', 'cc', 'bcc', 'return_path'))) {
          $message[$key . '-name'] = isset($value[0]->personal) ? iconv_mime_decode($value[0]->personal, 0, "UTF-8") : '';
          $message[$key . '-address'] = (isset($value[0]->mailbox) && isset($value[0]->host)) ? $value[0]->mailbox . '@' . $value[0]->host : '';
        }
        else {
          $message[$key] = iconv_mime_decode($value, 0, "UTF-8");
        }

It looks like the code is only taking the first element of object and setting the item to that value. So I tried this:

        // Some headers are arrays of objects
        if (in_array($key, array('to', 'from', 'reply_to', 'sender', 'cc', 'bcc', 'return_path'))) {
          
          $message[$key . '-name'] = array();
          $message[$key . '-address'] = array();
          
          foreach($value as $valkey => $val){
            $message[$key . '-name'][$valkey] = isset($val->personal) ? iconv_mime_decode($val->personal, 0, "UTF-8") : '';
            $message[$key . '-address'][$valkey] = (isset($val->mailbox) && isset($val->host)) ? $val->mailbox . '@' . $val->host : '';
          }
        }
        else {
          $message[$key] = iconv_mime_decode($value, 0, "UTF-8");
        }

And viola, each individual address gets added as a separate value in my email field. I'm sure it's probably not a complete solution, but perhaps will help somebody along the way.

Thanks.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ordermind’s picture

Thanks, this is how it should work!

Todd Young’s picture

This is superb and I was suffering from the same glitch... Can we get this rolled into a dev release?

mrfelton’s picture

Status: Active » Needs review
FileSize
2.28 KB

Attached is a patch to this effect. It also enhances the processing a little to provided dedicated mailbox and host properties.

Status: Needs review » Needs work

The last submitted patch, 1815688-mailhandler-better-addressee-support.patch, failed testing.

mrfelton’s picture

Status: Needs work » Needs review
FileSize
2.28 KB

Fix for syntax error i last patch.

Dane Powell’s picture

Title: Parsing multiple email addresses » Parsing of multiple email addresses
Version: 7.x-2.x-dev » 6.x-2.x-dev
Status: Needs review » Patch (to be ported)

Thanks for the patch, I applied it locally and will push to d.o once the repos are back online.

Does anyone know if Feeds 6.x parses in the same way? i.e. can this patch just be cherry-picked for 6.x? I guess we should write a simpletest for multiple email address mapping.

EDIT: commit:
http://drupalcode.org/project/mailhandler.git/commit/14716ec

nickonom’s picture

Is this functionality present in 7.x branch? Should I try to make it work with custom multiple e-mail field or by using Multiple E-mail Addresses module? Someone mentioned on that module about Mailhandler integration long time ago and I don't know if there were any actual attempts: https://www.drupal.org/node/201808#comment-10754276

nickonom’s picture

My bad, guys, as I've just noticed there is in fact the Mailhandler Multiple Email support module. Would be nice to mention on the module's main page it works with Multiple Email module. Going to give it a try right away.