Issue #3489931: Allow multiple email recipients.
Closes #3489931
Merge request reports
Activity
139 $to = $this->configFactory->get('system.site')->get('mail'); 140 $params['message'] = $message; 141 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 142 $send = TRUE; 143 144 $result = $this->pluginManagerMail->mail($module, $key, $to, $langcode, $params, NULL, $send); 145 return (bool) $result['result']; 146 } 147 148 // Split emails by newline and remove empty lines 149 $email_list = array_filter(explode("\n", $emails)); 150 foreach ($email_list as $to) { 151 $to = trim($to); 152 if (!empty($to)) { 153 $params['message'] = $message; 154 $langcode = $this->languageManager->getCurrentLanguage()->getId(); changed this line in version 2 of the diff
130 * Whether the message was sent or not. 131 * Whether all messages were sent successfully. 131 132 */ 132 public function notifyEmail($message, $email = NULL) { 133 public function notifyEmail($message, $emails = NULL) { 133 134 $module = 'migration_notify'; 134 135 $key = 'migration_notify'; 135 $to = empty($email) ? $this->configFactory->get('system.site')->get('mail') : $email; 136 $params['message'] = $message; 137 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 138 $send = TRUE; 136 $success = TRUE; 137 138 if (empty($emails)) { 139 $to = $this->configFactory->get('system.site')->get('mail'); 140 $params['message'] = $message; changed this line in version 3 of the diff
131 132 */ 132 public function notifyEmail($message, $email = NULL) { 133 public function notifyEmail($message, $emails = NULL) { 133 134 $module = 'migration_notify'; 134 135 $key = 'migration_notify'; 135 $to = empty($email) ? $this->configFactory->get('system.site')->get('mail') : $email; 136 $params['message'] = $message; 137 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 138 $send = TRUE; 136 $success = TRUE; 137 138 if (empty($emails)) { 139 $to = $this->configFactory->get('system.site')->get('mail'); 140 $params['message'] = $message; 141 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 142 $send = TRUE; changed this line in version 3 of the diff
136 $success = TRUE; 137 138 if (empty($emails)) { 139 $to = $this->configFactory->get('system.site')->get('mail'); 140 $params['message'] = $message; 141 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 142 $send = TRUE; 143 144 $result = $this->pluginManagerMail->mail($module, $key, $to, $langcode, $params, NULL, $send); 145 return (bool) $result['result']; 146 } 147 148 // Split emails by newline and remove empty lines 149 $email_list = array_filter(explode("\n", $emails)); 150 foreach ($email_list as $to) { 151 $to = trim($to); changed this line in version 2 of the diff
144 $result = $this->pluginManagerMail->mail($module, $key, $to, $langcode, $params, NULL, $send); 145 return (bool) $result['result']; 146 } 147 148 // Split emails by newline and remove empty lines 149 $email_list = array_filter(explode("\n", $emails)); 150 foreach ($email_list as $to) { 151 $to = trim($to); 152 if (!empty($to)) { 153 $params['message'] = $message; 154 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 155 $send = TRUE; 156 157 $result = $this->pluginManagerMail->mail($module, $key, $to, $langcode, $params, NULL, $send); 158 if (!$result['result']) { 159 $success = FALSE; changed this line in version 2 of the diff
341 353 ->save(); 342 354 } 343 355 356 /** 357 * {@inheritdoc} 358 */ 359 public function validateForm(array &$form, FormStateInterface $form_state) { 360 parent::validateForm($form, $form_state); 361 362 // Only validate if email notifications are enabled changed this line in version 2 of the diff
160 172 '#default_value' => $config->get('email'), 161 173 ]; 162 174 $form['email_to'] = [ 163 '#type' => 'email', 164 '#title' => $this->t('Send email to this address'), 165 '#description' => $this->t('Send the email to the specified address. Leave empty for %email (site administrator)', ['%email' => $email]), 166 '#default_value' => $config->get('email_to'), 175 '#type' => 'textarea', 176 '#title' => $this->t('Send email to these addresses'), 177 '#description' => $this->t('Send the email to the specified addresses. One email per line. Leave empty for %email (site administrator)', ['%email' => $email]), 178 '#default_value' => is_array($config->get('email_to')) ? implode("\n", $config->get('email_to')) : '', We should just read the data on config. We should keep it as a string. In fact, I think we should allow both line separated or comma separated.
changed this line in version 3 of the diff
327 339 $cron = $form_state->getValue('cron'); 328 340 $daily = $form_state->getValue('daily'); 329 341 $email = $form_state->getValue('email'); 330 $email_to = $form_state->getValue('email_to'); 331 342 $slack = $form_state->getValue('slack'); 332 343 $message = $form_state->getValue('stuck_message'); 333 344 345 $valid_emails = $form_state->get('valid_emails') ?: []; changed this line in version 3 of the diff
363 $valid_emails = []; 364 $emails = $form_state->getValue('email_to'); 365 if (!empty($emails)) { 366 foreach (array_filter(explode("\n", $emails)) as $email) { 367 if ($trimmed = trim($email)) { 368 if (!$this->emailValidator->isValid($trimmed)) { 369 $form_state->setErrorByName('email_to', $this->t('The email address %email is not valid.', [ 370 '%email' => $trimmed, 371 ])); 372 } 373 else { 374 $valid_emails[] = $trimmed; 375 } 376 } 377 } 378 $form_state->set('valid_emails', $valid_emails); changed this line in version 3 of the diff
331 343 $slack = $form_state->getValue('slack'); 332 344 $message = $form_state->getValue('stuck_message'); 333 345 346 $cleaned_emails = ''; 347 if (!empty($email_to)) { 348 $email_list = array_filter(array_map('trim', explode("\n", $email_to))); We are using
array_filter(array_map('trim', explode("\n", $emails)))
in two places, let's extract the logic into a function.And then this line can be changed to:
Edited by Fran Garcia-Linareschanged this line in version 4 of the diff
338 358 ->set('email', $email) 339 ->set('email_to', $email_to) 359 ->set('email_to', $cleaned_emails) 340 360 ->set('stuck_message', $message) 341 361 ->save(); 342 362 } 343 363 364 /** 365 * {@inheritdoc} 366 */ 367 public function validateForm(array &$form, FormStateInterface $form_state) { 368 parent::validateForm($form, $form_state); 369 370 $emails = $form_state->getValue('email_to'); 371 if (!empty($emails)) { 372 $email_list = array_filter(array_map('trim', explode("\n", $emails))); - Edited by Fran Garcia-Linares
changed this line in version 4 of the diff
160 172 '#default_value' => $config->get('email'), 161 173 ]; 162 174 $form['email_to'] = [ 163 '#type' => 'email', 164 '#title' => $this->t('Send email to this address'), 165 '#description' => $this->t('Send the email to the specified address. Leave empty for %email (site administrator)', ['%email' => $email]), 175 '#type' => 'textarea', 176 '#title' => $this->t('Send email to these addresses'), 177 '#description' => $this->t('Send the email to the specified addresses. One email per line. Leave empty for %email (site administrator)', ['%email' => $email]), 177 '#description' => $this->t('Send the email to the specified addresses. One email per line. Leave empty for %email (site administrator)', ['%email' => $email]), 177 '#description' => $this->t('Send the email to the specified addresses. Comma separated list of emails or one email per line. Leave empty for %email (site administrator)', ['%email' => $email]), changed this line in version 4 of the diff
128 129 * 129 130 * @return bool 130 * Whether the message was sent or not. 131 * Whether all messages were sent successfully. 131 132 */ 132 public function notifyEmail($message, $email = NULL) { 133 public function notifyEmail($message, $emails = NULL) { 133 134 $module = 'migration_notify'; 134 135 $key = 'migration_notify'; 135 $to = empty($email) ? $this->configFactory->get('system.site')->get('mail') : $email; 136 $emails = empty($emails) ? $this->configFactory->get('system.site')->get('mail') : $emails; 136 137 $params['message'] = $message; 137 138 $langcode = $this->languageManager->getCurrentLanguage()->getId(); 138 139 $send = TRUE; 140 $success = TRUE; 141 foreach (explode("\n", $emails) as $to) { And then somewhere in this service file:
// Add phpdocs public function explodeString($string) { // Allow commas as delimiters as well. $string = str_replace(',', "\n", $string); return array_filter(array_map('trim', explode("\n",$string))); }
changed this line in version 4 of the diff