3

I am very new to drupal and need to upload a file and sent it as attachment in Drupal 8. I have created the form element like

$form['candidate_attachment'] = [
  '#type' => 'managed_file',
  '#title' => t('Attachments'),
];

When I submit the form with a PDF file and in the submitform() the data displays for the $form_state->getValue('candidate_attachment');

is Array ( [0] => 1358 )

Code to send email is

// ...
$validators = ['file_validate_extensions' => ['pdf']];
if ($file = file_save_upload('candidate_attachment', $validators, FALSE, 0)) {
  $filepath = file_get_contents($file->getFileUri());
}

// ...
$params['context']['message'] = $test;
$params['context']['attachments'][] = $filepath;
$to = $em["mail"];  
      
$result = $mailManager->mail('system', 'mail', $to, $langcode, $params); 

But the email is sending without an attachment. What am I doing wrong? How can I correctly attach the file to the mail?

0

0

Browse other questions tagged or ask your own question.