I am using the following code that programmatically creates a file and attaches it to a file field when a contact form is submitted.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['submit']['#submit'][] = 'custom_submit_handler';
}
function custom_submit_handler(&$form, &$form_state) {
$file_contents = '777';
$filename = 'my_file.txt';
$file = File::create([
'uri' => 'public://' . $filename,
'filename' => $filename,
]);
$file->save();
file_put_contents($file->getFileUri(), $file_contents);
$entity = $form_state->getFormObject()->getEntity();
$entity->get('my_field_file')->appendItem($file);
$entity->save();
}
The code works as expected, but there is no link to the new file in the sent email.
What should I do to change the email message?