public function WebformEmailReplyForm::submitForm in Webform Email Reply 8
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ WebformEmailReplyForm.php, line 202 - Contains \Drupal\webform_email_reply\Form\WebformEmailReplyForm.
Class
Namespace
Drupal\webform_email_reply\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$webform_id = $form_state
->getValue([
'details',
'webform_id',
]);
$sid = $form_state
->getValue([
'details',
'sid',
]);
$emails = explode(',', $form_state
->getValue([
'details',
'email',
]));
$body = $form_state
->getValue([
'details',
'message',
]);
$subject = $form_state
->getValue([
'details',
'subject',
]);
$file = $form_state
->getValue([
'details',
'attachement',
]);
$params = [
'body' => $body,
'subject' => $subject,
];
$from_address = $form_state
->getValue([
'details',
'from_address',
]);
$params['from'] = $from_address;
// Data for saving in schema.
$data = $form_state
->getValue([
'details',
]);
// Saving files permanently.
if (isset($file[0])) {
$file = File::load($file[0]);
$file
->setPermanent();
$file
->save();
$data['fid'] = $file
->id();
$params['attachments'][] = [
'filecontent' => file_get_contents($file
->getFileUri()),
'filename' => $file
->getFilename(),
'filemime' => $file
->getMimeType(),
'filepath' => \Drupal::service('file_system')
->realpath($file
->getFileUri()),
'_uri' => file_create_url($file
->getFileUri()),
];
$params['headers'] = [
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
];
}
// Send each emails individually.
foreach ($emails as $email) {
$mail_sent = \Drupal::service('plugin.manager.mail')
->mail('webform_email_reply', 'email', $email, $this->currentUser
->getPreferredLangcode(), $params, NULL, TRUE);
if ($mail_sent) {
\Drupal::messenger()
->addMessage($this
->t('Reply email sent to @email from @from_address.', [
'@email' => $email,
'@from_address' => $from_address,
]));
// Insert the values into the database.
webform_email_reply_insert($data);
}
else {
\Drupal::messenger()
->addError($this
->t('There was an error sending the email to @email, please contact the site admin.', [
'@email' => $email,
]));
}
}
}