I'm not sure if this is an issue with the mail function, but I have a challenge to send two emails in hook_user_presave.
I'm sending two emails like (admin and customer) :
<?php
function hook_user_presave(UserInterface $user) {
// 1. send email (admins)
$admin_params = [
'subject' => t('New online application form'),
'from' => $site_mail,
'field_first_name' => $user->field_first_name->value,
'field_last_name' => $user->field_last_name->value,
'contractor_name' => $contractor_details->name,
'contractor_street1' => $contractor_details->street1,
'contractor_street2' => $contractor_details->street2,
'contractor_street3' => $contractor_details->street3,
'contractor_street4' => $contractor_details->street4,
'contractor_street5' => $contractor_details->street5,
];
$admin_emails = \Drupal::config('admin_general_settings.settings')->get('admin_emails');
$more_receipient_array = explode("\r\n", $admin_emails);
$mail_service = \Drupal::service('plugin.manager.mail');
$mail_service->mail(
'adpomail',
're_application',
$more_receipient_array,
$user->getPreferredLangcode(),
$admin_params,
$site_mail,
true
);
// 2. send email (user)
$customer_params = [
'subject' => t('Thank you for your application.'),
'from' => $site_mail,
'field_first_name' => $account->field_first_name->value
];
$mail_service = \Drupal::service('plugin.manager.mail');
$mail_service->mail(
'adpomail',
'application',
$user->getEmail(),
$user->getPreferredLangcode(),
$customer_params,
$site_mail,
true
);
?>
but I'm getting an error for admin email:
Undefined array key "field_last_name" , "contractor_name" ect - everything except "field_first_name" in re_application
I have debug the params, all values exists there.
PS. I have removed the second user email and everting works fine and I'm not getting the error about undefined array for admin email, that is why I believe there is a conflict between these two mail functions.
Closed: works as designed
1.2
Documentation