-3

I create a custom form. Users can send phone number and email address with this form.

Now I want send email after submit form and users data send to my mail.

How can I do this in Drupal 8 ?

  • 1
    I guess you try rules – sarathkm Jul 29 '17 at 6:05
  • I want do this with my module and don't need to another module like Rules – RobinSon Jul 29 '17 at 6:11
0

Using MailManager::mail you can send mail from your custom form submission function.

Code for MyForm.php

public function submitForm(array &$form, FormStateInterface $form_state) {
    $newMail = \Drupal::service('plugin.manager.mail');
    $params['password'] = $password;
    $params['email'] = $userEmail;
    $newMail->mail('eura_registerform', 'registerMail', $userEmail, 'en', $params, $reply = NULL, $send = TRUE);
    drupal_set_message('Mail has been sent.', 'status');
}

Code for myform.module

  /**
   * Implementing hook_mail()
   */  
  function myform_mail($key, &$message, $params) {

  // E-mail formatting will be exactly as below, so do not use indenting!
  $body =
"Hi,

I'm just showing off my work ...
Works nicely huh?
Should probably add a password here somewhere?

Your username: " . $params['email'] . "
Your password: " . $params['password'] . "

See ya!";

  $message['subject'] = "EuRA account registration";
  $message['body'][] = Drupal\Core\Mail\MailFormatHelper::htmlToText($body);
}

Code Source

  • You didn't seprate test.module and TestForm.php codes !! – RobinSon Jul 29 '17 at 13:52
  • Ok separated the codes as myform.module and MyForm.php. – sarathkm Jul 31 '17 at 6:55
-1

I am thinking your in submit function of your form. Let's try this code to send mail.

public function submitForm(array &$form, FormStateInterface $form_state){
 $phone_number = $form_state->getValue('phone_number');
 $email_address = $form_state->getValue('email_address');
 $send_mail = new \Drupal\Core\Mail\Plugin\Mail\PhpMail();
 $from = 'your@site.com';
 $message['headers'] = array(
 'content-type' => 'text/html',
 'MIME-Version' => '1.0',
 'reply-to' => $from,
 'from' => 'Name site<'.$from.'>'
 );
$message['to'] = $email_address;
$message['subject'] = "Your Subject";
$message['body'] = 'Your Body';
$result = $send_mail->mail($message);
if ($result !== true) {
 drupal_set_message(t('There was a problem please contact site admin.'), 'error');
return new RedirectResponse(\Drupal::url('<front>'))
}else{
drupal_set_message(t('mail Sent'); 
 return new RedirectResponse(\Drupal::url('<front>'))
 }
}

Please install PHPMailer module.

Thanks

  • Read question comment : "I want do this with my module and don't need to another module like Rules" – RobinSon Jul 29 '17 at 13:49
  • Are you serious ? You want to do this without any module. I think you need a custom module to do this. – Pankaj Yogi Jul 29 '17 at 13:52
  • Yes.I create my custom module and I dont need to any ready moduls. – RobinSon Jul 30 '17 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.