I'm simply trying to send a mail with the following code.
function contact_form_mail($key, &$message, $params){
switch ($key) {
case 'contact':
// Set headers etc
// $message['headers']['Reply-To'] = $params['to'];
$message['headers']['Content-Type'] = 'text/html';
$message['headers']['From'] = $params['from'];
$message['to'] = $params['to'];
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
break;
}
}
public function sendForm(){
$user = $this->getCurrentUser();
$recipent = $this->findEmailForLocation($this->getLocation());
if(empty($recipent)){ return; }
$request = \Drupal::request();
$subject = $request->get('subject');
$body = $request->get('body');
if(empty($subject) || empty($body)){
return $this->redirect('contact_form.contact', ['error' => 'Fill in all fields.']);
}
$mailManager = \Drupal::service('plugin.manager.mail');
$mailManager->mail(
'contact_form',
'contact',
['[email protected]'],
$user->getPreferredLangcode(),
['to' => [$recipent], 'from' => $user->getEmail(), 'subject' => $subject, 'body' => $body],
$user->getEmail(),
TRUE
);
return $this->redirect('contact_form.contact', ['success' => 'Data sent']);
}
I'm getting this error.
Warning: Invalid argument supplied for foreach() in Drupal\Core\Mail\MailManager->doMail() (line 286 of core/lib/Drupal/Core/Mail/MailManager.php).
I have no clue which value is wrong and/or what kind of datatype it should be.
$body = $request->get('body');
isn't an array as it's expecting