Problem/Motivation

This problem tends to come up mostly with a comma-delimited list of multiple email addresses that includes with spaces. This generates a fatal error: "Invalid address" and results in only the first recipient in the list receiving the email.

Proposed resolution

The issue appears to be in the _get_components() method, which does not trim the $input until after it tries to validate the email. The solution will be to trim the email addres before validating it.

Remaining tasks

Patch #33 applies patch #6, which had been reviewed by many people, and adds test coverage.

Original report by donutdan4114

When the to address is a comma-delimited list of emails with spaces, input validation will fail with an error stating Invalid address: .
The issue appears to be in the _get_components() method, which does not trim the $input until after it tries to validate the email.

CommentFileSizeAuthor
#52 2753115-52-smtp-multiple-to-addresses-error--interdiff.patch4.19 KBjedihe
#52 2753115-52-smtp-multiple-to-addresses-error--full.patch3.57 KBjedihe
#52 2753115-52-smtp-multiple-to-addresses-error--tests-only.patch2.84 KBjedihe
#51 2753115-51-smtp-multiple-to-addresses-error--interdiff.patch4.19 KBjedihe
#51 2753115-51-smtp-multiple-to-addresses-error--full.patch3.57 KBjedihe
#51 2753115-51-smtp-multiple-to-addresses-error--tests-only.patch2.83 KBjedihe
#50 2753115-50-smtp-multiple-to-addresses-error--interdiff.patch1.69 KBjedihe
#50 2753115-50-smtp-multiple-to-addresses-error--full.patch2.96 KBjedihe
#50 2753115-50-smtp-multiple-to-addresses-error--tests-only.patch2.23 KBjedihe
#49 2753115-49-smtp-multiple-to-addresses-error--interdiff.patch1.26 KBjedihe
#49 2753115-49-smtp-multiple-to-addresses-error--full.patch2.53 KBjedihe
#49 2753115-49-smtp-multiple-to-addresses-error--tests-only.patch1.8 KBjedihe
#48 2753115-48-smtp-multiple-to-addresses-error--interdiff.patch1.81 KBjedihe
#48 2753115-48-smtp-multiple-to-addresses-error--full.patch2.84 KBjedihe
#48 2753115-48-smtp-multiple-to-addresses-error--tests-only.patch2.11 KBjedihe
#41 2753115-41-smtp-multiple-to-addresses-error.patch2.65 KBpianomansam
#33 2753115-33-smtp-multiple-to-addresses-error.patch2.75 KBpianomansam
#31 2753115-31-smtp-multiple-to-addresses-error--tests-only.patch2.13 KBpianomansam
#30 2753115-30-smtp-multiple-to-addresses-error--tests-only.patch2.14 KBpianomansam
#28 2753115-27-smtp-multiple-to-addresses-error--tests-only.patch1.89 KBpianomansam
#26 2753115-26-smtp-multiple-to-addresses-error--tests-only.patch1.77 KBpianomansam
#26 2753115-26-smtp-multiple-to-addresses-error--tests-only.patch1.77 KBpianomansam
#6 2753115-6-smtp-multiple-to-addresses-error.patch749 bytespianomansam
#2 2753115-2-smtp-multiple-to-addresses-error.patch493 bytesdonutdan4114
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

donutdan4114 created an issue. See original summary.

donutdan4114’s picture

andileco’s picture

The patch worked for me--thanks! For some reason git apply -v 2753115-2-smtp-multiple-to-addresses-error.patch didn't work, so I applied it manually.

jacobischwartz’s picture

This bug also tripped me up. I had comma separated recipients, with spaces between. Only the first recipient was getting the emails. By just removing the spaces between recipients, all recipients were included.

This seems like kind of a major bug for a module used by so many people... I would have thought that the author would push a maintenance update pretty quickly :/

pianomansam’s picture

Priority: Normal » Critical

This is a critical issue. Having recipients separated by commas is well with the email spec. This needs to be addressed promptly. I've downgraded to 7.x-1.3 in the meantime.

pianomansam’s picture

Title: Sending to multiple email addresses with comma-delimited list gives fatal error: "Invalid address" » Sending to multiple email addresses with comma-delimited list with spaces gives fatal error: "Invalid address"
Version: 7.x-1.4 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
749 bytes

This issue is still present today in 7.x-1.x-dev so I've updated this issue and created a slightly better patch.

royerd’s picture

Used this patch and it works . . .thanks Sam.

related: https://www.drupal.org/node/2812651

pianomansam’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

royerd: Glad to hear it worked! With your approval and the two others from #2812651: Invalid email address: Error due to not checking trimmed email address., I'm going to mark this as RTBC. I'm also going to update the original report.

hass’s picture

This does not fix all bugs with multiple addresses.

pianomansam’s picture

@hass, it looks like this other issue is for the 7.x-2.x branch. How would you like to handle this? Keep a separate issue for each branch?

hass’s picture

I do not know. I guess it also exists in 1.x.

I just moved it here as the case opener has not provided the version he is using.

ilfelice’s picture

Howdy,

I recently updated this module to 7.x-1.6, and sending to multiple comma delimited recipients stopped working (only the first in the list would receive the email).

In my case, there were no spaces between the comma delimited emails (just commas).

Anyway, applied #6 (to 7.x-1.6), and now sending to multiple comma delimited recipients is working fine again.

Jorge

dandaman’s picture

Applied this patch to 7.x-1.6 and it worked great. Some Webform 4.x recipient lists, by default, have e-mails delimited with commas and spaces ', ' and thus were not sending right when switching to SMTP. This patch fixes it. Will need to be using this with SMTP 7.x-1.6 until is is committed.

dandaman’s picture

Actually, looking at the code, adding the trim() at the start of the _get_components() function is what fixes this... It might be counterproductive to remove the trim() later on as well, but I suppose once it's been done, it doesn't need to be done again, so maybe removing it does make sense.

OK, never mind. I was just thinking out loud and I guess i found my point is meaningless.

apotek’s picture

Coming to this thread via https://www.drupal.org/node/2856373, which was closed as a duplicate. The patch here, and the patch in the other issue are nearly identical. We have six RTBCs from the other thread, and a few here as well. Both patches pass automated testing.

What's the hold up with committing one or the other of these? The issues have been open for months with functioning and tested patches and resolve an existing bug in functionality.

Chalk’s picture

The #6 patch solved the bug.

ronaldmulero’s picture

#6 worked for me.
• SMTP 7.x-1.x
• Mass Contact 7.x-1.1 (sending BCC with multiple addresses)
• Drupal core 7.56

apotek’s picture

What's the hold up with committing one or the other of these? The issues have been open for months with functioning and tested patches and resolve an existing bug in functionality.

I don't know. Maintainer has not identified any blocking issue on either of these threads. Tests pass. RTBC. It seems pretty straightforward, but perhaps there is a reason that is not spelled out here.

Greg Varga’s picture

The #6 patch resolves the bug; Tested and working. Please commit. Thank you.

wundo’s picture

Status: Reviewed & tested by the community » Needs work

apotek,
As hass mentioned in #11, this doesn't solve all errors related with multiple e-mail addresses.

If this patched added more tests to make sure this feature works perfectly I'd feel safer to commit it than how it as is now.

pianomansam’s picture

wundo, comment #11 refers to #2847251: Multiple emails not validated properly which was closed as a duplicate of this issue. Furthermore, #2847251: Multiple emails not validated properly doesn't report any additional errors related to multiple email addresses. While this patch doesn't include tests, it has 8 Drupal.org users reporting that it works. What makes this particular issue so frustrating is that it is a critical issue on a supposedly actively maintained module and it's been unresolved for two years. And yet when a simple, two-line patch that has been reviewed by the community is submitted, it is rejected a year later.

hass’s picture

You may reviewed, but not tested with multiple email addresses... code wise this cannot work.

pianomansam’s picture

@hass yes the patch does not include tests. However, I have this patch on at least a dozen production sites and it solves the issue. I believe that's what the other 7 people on this ticket have done as well to label it as "reviewed". So it has in fact been tested in production.

My point about this module being actively maintained or not still remains. Where is the maintainer? Why has the maintainer not been involved more in this critical issue, perhaps writing the tests required for this patch to be committed?

wundo’s picture

Priority: Critical » Normal

> Where is the maintainer?

Hi, @pianomansam I'm a co-maintainer, I'm here.

> Why has the maintainer not been involved more in this critical issue, perhaps writing the tests required for this patch to be committed?

Because we maintainers do this on our own time, and we are free to decide what we think is more important or a better use of our time.

It's really great that you use this module that you've got for free in at least a dozen clients.
I feel proud that I and the other maintainers and all the other people who contributed to this module were able to provide you with this great piece of software that solves yours and your clients business need.

If you feel this issue is a high priority (I don't, as it doesn't impact the majority of the users), please contribute in a positive way, no one here is obligated to write a patch or test just because you want it to.

brandonpost’s picture

I spent about an hour yesterday trying to figure out why emails from Webform 4.x weren't going out and finally tracked it down to the fact that there were spaces in the comma-delimited list of email addresses. Then this morning I thought, "I wonder if anyone else has run into this issue?", so I searched and found this issue thread at what appears to be an unfortunately tense moment.

@wundo, I want to say a big thanks to you and the other maintainers of this module. You're right that module maintainers provide a huge service to the Drupal community free of charge on your own time, and very rarely if ever get the thanks and recognition you deserve. I sincerely appreciate this module and all the benefits it brings to my sites and clients, and I appreciate your work that makes it possible.

At the same time, I also appreciate @pianomansam for providing a patch that solves an issue that caused me a good bit of frustration yesterday.

I think we all want the same thing - to make this module the best it can be. @wundo said in #20 that he would be more comfortable committing the patch if it had further testing. I will try to write a unit test that tests for sending emails to a comma-delimited list with spaces. But I can confirm that I have tested the patch on my own sites, and it works perfectly. Please let me know if there is anything else I can do to help move the process forward.

pianomansam’s picture

@wundo thank you for the recent prompt replies to this conversation. It's a great change from the two years of silence.
I understand the open-source model as I too am a contrib maintainer. I know the reality of balancing work, life, and open-source software contribution on personal time.
Rather than continue to argue over this topic, I've gone ahead and written the tests for it.
For what it's worth, I do consider this a critical issue as it prevents this module from following the RFC 2822 spec.

I'm attaching a patch that adds tests for email address parsing. These tests check the following recipient strings to see if they pass validation.

  1. to_test@example.com
  2. to_test@example.com,to_test2@example.com
  3. to_test@example.com, to_test2@example.com

This patch will result in a failing test as the third string will fail, which is the cause of this bug report to begin with.

Once the tests have completed for this patch, I will submit another patch that will include the changes from #6.

Status: Needs review » Needs work

The last submitted patch, 26: 2753115-26-smtp-multiple-to-addresses-error--tests-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

pianomansam’s picture

Sigh... looks like there was a PHP class autoloader issue. Here's an update, and I removed the failing mock data, so hopefully this test will pass.

Status: Needs review » Needs work

The last submitted patch, 28: 2753115-27-smtp-multiple-to-addresses-error--tests-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

pianomansam’s picture

Autoloading continues to be an issue, so let's try to add a public method exposing the protected _get_components() method.

pianomansam’s picture

Now that we have a test for parsing addresses, let's add the failing test to confirm this issue. The mock data will be this, which is RFC 2822 compliant:

to_test@example.com, to_test2@example.com

Status: Needs review » Needs work

The last submitted patch, 31: 2753115-31-smtp-multiple-to-addresses-error--tests-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

pianomansam’s picture

Status: Needs work » Needs review
FileSize
2.75 KB

Reapply the patch from #6 and see if it makes the new tests pass.

pianomansam’s picture

brandonpost’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @pianomansam for writing these tests.

Since the new tests confirm that the issue exists and that the supplied patch resolves the issue, and since a number of people have already reviewed and tested the patch, I'm updating the status to RTBC.

wundo’s picture

Status: Reviewed & tested by the community » Needs review

I was getting ready to commit this when I saw that you created a new method only to expose the _get_components so you could test it.

IMHO a better way of handling it was creating an anonymous class that extends the main one and exposes the method you want, something like:

(...)
$mailer = new class() extends SmtpMailSystem {

  /**
   * Exposes the protected _get_components method, returning an array of name and email address from a string.
   *
   * @param $input
   *  A string that contains different possible combinations of names and
   *  email address.
   * @return
   *  An array containing a name and an email address.
   */
  public function getComponents($input) {
    return $this->_get_components($input);
  }
}

(...)

This way you don't have to change the interface of the main class.

wundo’s picture

You might take a look on this patch (for D8) as well: https://www.drupal.org/project/smtp/issues/2972280

  • wundo committed 0d82231 on 8.x-1.x
    Issue #2753115 by wundo: Makes the EmailValidator dependency explicit
    
  • wundo committed a00c296 on 8.x-1.x
    Issue #2753115 by wundo: Add tests to SMTPMailSystem::getComponents
    
  • wundo committed c1397bb on 8.x-1.x
    Issue #2753115 by wundo, pianomansam, donutdan4114, hass, apotek,...
wundo’s picture

Status: Needs review » Needs work

I've ported this patch to D8 and committed it.

I've also written PHPUnit tests for this issue (check the 8.x-1.x branch), if anyone could step in in follow that example and back port the test to D7 I'd really appreciate, otherwise I will tackle that myself the next time I'm available.

  • wundo committed 10a6483 on 8.x-1.x
    Issue #2753115 by wundo: Fixes compatibility with older PHP Versions
    
pianomansam’s picture

@wundo, good call on the anonymous class. That didn't even cross my mind, but it's a much better solution. Here's an updated patch that uses an anonymous class to expose _get_components() instead of adding it to the main SmtpMailSystem class.

Status: Needs review » Needs work

The last submitted patch, 41: 2753115-41-smtp-multiple-to-addresses-error.patch, failed testing. View results

pianomansam’s picture

@wundo The anonymous class in the test failed because it's using PHP 5.6. It looks like you discovered that as well for the 8.x-1.x version. Unfortunately, I can't use the approach you ended up using for 8.x as autoloading classes is difficult in D7. And actually, if you at the patch I created for #26, your 8.x approach is exactly what I first attempted.

wundo’s picture

@pianomansam, actually your original tests were missing some scenarios that I've implemented as well (and that was impacting coverage), you can declare more than one class in the same file, like I did for D8: https://cgit.drupalcode.org/smtp/commit/?id=10a6483

pianomansam’s picture

@wundo, I may leave this in your court, then, since you have a better handle of what you did in D8.

jason_purdy’s picture

Thank you for the patch, pianomansam! Works for me! I hope it gets merged into the 7.x branch at some point so it will accept recognized address formats from drupal_mail(), but until then, at least this patch works. :)

jimmynash’s picture

Thanks @pianomansam for the patch. Applied #41 to current 7.x-1.7.

Multiple email addresses were being mapped in webform select component values. The first would work but then trigger the invalid email for the rest.

Confirm working for us.

jedihe’s picture

I'm working on improving #41 in order to:

  1. Use an intermediary class to expose _get_components() as a public method.
  2. Check for some other scenarios, as shown in the D8 commits linked by @wundo

I'm attaching a set of files for testing: tests-only (must fail), full (must pass), interdiff against #41.

It'd be very useful if somebody can check if my approach is sound. I'll now continue working on the 2nd bullet point.

jedihe’s picture

jedihe’s picture

jedihe’s picture

Testbot results look good now, bumping to "Needs review".

Attaching patches with coding standards fixes, only for testAddressParsing(). No functional change vs. #51.

Please note the actual fix is exactly the same as in #6, which has already been tested and reported to work a few times in this thread. So, IMHO this should get in after just getting the tests reviewed and approved.

pianomansam’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me and testbot thinks so too!

  • wundo committed 63ed4de on 7.x-1.x authored by jedihe
    Issue #2753115 by jedihe, pianomansam, donutdan4114, wundo, hass,...
wundo’s picture

Status: Reviewed & tested by the community » Fixed

Thank you guys for the wonderful collaboration on this issue!

I really appreciate all your effort, really thank you.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

solideogloria’s picture