I have 4 domains using domain access module like a.com, b.com, c.com, d.com. currently when user registers he receives a mail from [email protected] which is default in site configuration. Now what I want is to Change system mail on run time with respect to 4 diffrent domains so that when user registers from b.com he will receive mail from [email protected] and so on.As this info is saved in system configuration so how to change the mail id of the site when sending mail on run time.

1 Answer 1


You can alter the mail using hook_mail_alter(&$message)

Alter an email message created with MailManagerInterface->mail().

with in message array you have 'from' field as well.


function mymodule_mail_alter(&$message) {
  if ($message['id'] == 'modulename_messagekey') {
    // Login to get domain specific email id.
    $email_id == _some_logic_to_get_email();
    $message['from'] = $email_id;

Refernce: https://api.drupal.org/api/drupal/core%21core.api.php/function/hook_mail_alter/8.2.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.