Issue sending emails from a contact form

Created on 2 April 2024, 3 months ago
Updated 11 April 2024, 3 months ago

Problem/Motivation

Hello, my Drupal 10 website migrated from swift mailer to symfony mail.
It contains a few contact forms, that have been "Enabled & imported" from the Mailer policy page.
I have set up an SMTp transport and sending a test email works.

Steps to reproduce

When I compile a contact form as an anonymous user, the website shows "The website encountered an unexpected error. Try again later.".

In the error log i get the following error:

TypeError: Drupal\symfony_mailer\Address::__construct(): Argument #1 ($email) must be of type string, null given, called in [redacted]/modules/symfony_mailer/src/Address.php on line 86 in Drupal\symfony_mailer\Address->__construct() (line 58 of [redacted]/modules/symfony_mailer/src/Address.php).

and the following backtrace:

#0 [redacted]/modules/symfony_mailer/src/Address.php(86): Drupal\symfony_mailer\Address->__construct()
#1 [redacted]/modules/symfony_mailer/src/Address.php(142): Drupal\symfony_mailer\Address::create()
#2 [redacted]/modules/symfony_mailer/src/BaseEmailTrait.php(65): Drupal\symfony_mailer\Address::convert()
#3 [redacted]/modules/symfony_mailer/src/BaseEmailTrait.php(97): Drupal\symfony_mailer\Email->setAddress()
#4 [redacted]/modules/symfony_mailer/src/Plugin/EmailBuilder/ContactEmailBuilderBase.php(33): Drupal\symfony_mailer\Email->setReplyTo()
#5 [redacted]/modules/symfony_mailer/src/Plugin/EmailBuilder/ContactPageEmailBuilder.php(77): Drupal\symfony_mailer\Plugin\EmailBuilder\ContactEmailBuilderBase->build()
#6 [internal function]: Drupal\symfony_mailer\Plugin\EmailBuilder\ContactPageEmailBuilder->build()
#7 [redacted]/modules/symfony_mailer/src/Email.php(490): call_user_func()
#8 [redacted]/modules/symfony_mailer/src/Mailer.php(179): Drupal\symfony_mailer\Email->process()
#9 [redacted]/modules/symfony_mailer/src/Mailer.php(149): Drupal\symfony_mailer\Mailer->doSend()
#10 [redacted]/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\symfony_mailer\Mailer->Drupal\symfony_mailer\{closure}()
#11 [redacted]/modules/symfony_mailer/src/Mailer.php(158): Drupal\Core\Render\Renderer->executeInRenderContext()
#12 [redacted]/modules/symfony_mailer/src/Email.php(292): Drupal\symfony_mailer\Mailer->send()
#13 [redacted]/modules/symfony_mailer/src/MailManagerReplacement.php(92): Drupal\symfony_mailer\Email->send()
#14 [redacted]/core/modules/contact/src/MailHandler.php(114): Drupal\symfony_mailer\MailManagerReplacement->mail()
#15 [redacted]/core/modules/contact/src/MessageForm.php(223): Drupal\contact\MailHandler->sendMailMessages()
#16 [internal function]: Drupal\contact\MessageForm->save()
#17 [redacted]/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array()
#18 [redacted]/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers()
#19 [redacted]/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm()
#20 [redacted]/core/lib/Drupal/Core/Form/FormBuilder.php(325): Drupal\Core\Form\FormBuilder->processForm()
#21 [redacted]/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(48): Drupal\Core\Form\FormBuilder->buildForm()
#22 [redacted]/modules/contact_storage/src/ContactFormViewBuilder.php(92): Drupal\Core\Entity\EntityFormBuilder->getForm()
#23 [redacted]/modules/contact_storage/src/Controller/ContactStorageController.php(45): Drupal\contact_storage\ContactFormViewBuilder->view()
#24 [internal function]: Drupal\contact_storage\Controller\ContactStorageController->contactSitePage()
#25 [redacted]/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#26 [redacted]/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#27 [redacted]/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#28 [redacted]/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#29 [redacted]/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#30 [redacted]/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#31 [redacted]/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#32 [redacted]/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#33 [redacted]/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#34 [redacted]/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\ContentLength->handle()
#35 [redacted]/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#36 [redacted]/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#37 [redacted]/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#38 [redacted]/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#39 [redacted]/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#40 [redacted]/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#41 [redacted]/index.php(19): Drupal\Core\DrupalKernel->handle()
#42 {main}

I tried to debug a bit the code in modules/symfony_mailer/src/Address.php:86:

    elseif ($address instanceof AccountInterface) {
      return new static($address->getEmail(), $address->getDisplayName(), $address->getPreferredLangcode(), $address);
    }

$address->getEmail() is null, $address->getDisplayName() is "(not verified)", $address->getPreferredLangcode() is "en".

I'm not sure how i can debug this further. Thank you for any guidance

πŸ› Bug report
Status

Active

Version

1.4

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @fbas
  • Contact forms contain by default two fields named "sender name" and "sender email".
    The problem is caused by the fact that i disabled those fields in my form (instead of the "sender name" field, i added two separated fields for "name" and "last name").
    As a result, the contact form module is passing an empty sender of type AccountInterface.
    This used to work with the old swift_mailer module, that used the website default email address as a fallback.

    Right now i patched the problem by forcing symfony mailer to use the website default email address as the sender:

    --- Address.php.bak     2024-04-03 08:42:11.564698369 +0000
    +++ Address.php 2024-04-03 08:42:38.244623682 +0000
    @@ -83,7 +83,9 @@
           }
         }
         elseif ($address instanceof AccountInterface) {
    -      return new static($address->getEmail(), $address->getDisplayName(), $address->getPreferredLangcode(), $address);
    +      $site_config = \Drupal::config('system.site');
    +      $site_mail = $site_config->get('mail') ?: ini_get('sendmail_from');
    +      return new static($site_mail, $site_config->get('name'));
         }
         elseif ($address instanceof SymfonyAddress) {
           return new static($address->getAddress(), $address->getName());
    
    

    Of course this is a bad hack, and i would like to implement a better solution.
    I think that at least symfony_mailer should detect when $address->getEmail() is null and avoid the php error.

  • Other problems have arisen . I gave up and switched to Drupal Symfony Mailer Lite.
    Feel free to ignore this.

Production build 0.69.0 2024