0

I've created my first Drupal 8 module which is a form that emails its output to a staff person. Everything works except for the email portion which doesn't throw any errors, but just doesn't do anything. I'm not sure what I'm doing wrong. I have the Drupal 7 version of the code running on the same server so I don't think it's an SMTP issue. Here's my code:

public function submitForm(array &$form, FormStateInterface $form_state) {
        $params = array(
            'title' => $form_state->getValue('title'),
            'author' => $form_state->getValue('author'),
            'materialtype' => $form_state->getValue('materialtype'),
            'isbn' => $form_state->getValue('ISBN'),
            'edition' => $form_state->getValue('edition'),
            'name' => $form_state->getValue('name'),
            'phone' => $form_state->getValue('phone'),
            'barcode' => $form_state->getValue('barcode'),
            'email' => $form_state->getValue('email'),
            'pickuplib' => $form_state->getValue('pickuplib'),
            'notes' => $form_state->getValue('notes')
        );

        $dest = $this->config('itemrequest.settings')->get('itemrequest_email');

        $newmail = \Drupal::service('plugin.manager.mail');
        if ($newmail->mail('itemrequest', 'patronitemrequest', $dest, 'en', $params)) {
            drupal_set_message(t("Your item request has been successfully submitted."));
        } else {
            drupal_set_message(t("Your request failed. Please contact your local library."));
        }
    }

    function itemrequest_mail($key, &$message, $params) {
        $headers = array(
            'MIME-Version' => '1.0',
            'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
            'Content-Transfer-Encoding' => '8Bit',
            'X-Mailer' => 'Drupal'
        );
        foreach ($headers as $key1 => $value) {
            $message['headers'][$key1] = $value;
        }
        switch ($key) {
            case 'patronitemrequest': 
                $message['subject'] = t('Item Request - !value', array('!value' => $params['pickuplib']));
                $message['body'][] = "<font color='black'>";
                $message['body'][] = t('Title: !value <br />', array('!value' => $params['title']));
                $message['body'][] = t('Author: !value <br />', array('!value' => $params['author']));
                $message['body'][] = t('Material Type: !value <br />', array('!value' => $params['materialtype']));
                $message['body'][] = t('ISBN: !value <br />', array('!value' => $params['isbn']));
                $message['body'][] = t('Edition: !value <br />', array('!value' => $params['edition']));
                $message['body'][] = t('Name: !value <br />', array('!value' => $params['name']));
                $message['body'][] = t('Phone: !value <br />', array('!value' => $params['phone']));
                $message['body'][] = t('Barcode: !value <br />', array('!value' => $params['barcode']));
                $message['body'][] = t('Email: !value <br />', array('!value' => $params['email']));
                $message['body'][] = t('Pickup Library: !value <br />', array('!value' => $params['pickuplib']));
                $message['body'][] = t('Notes: !value <br />', array('!value' => $params['notes']));
                $message['body'][] = t('Date: !value <br />', array('!value' => date("F j, Y")));
                $message['body'][] = "</font>";
            break;
        }       
    }

UPDATE: I modified a small chunk of the submitForm code:

$newmail = \Drupal::service('plugin.manager.mail');
        if ($newmail->mail('itemrequest', 'patronitemrequest', $dest, 'en', $params, NULL, TRUE)) {
            $log = t('An email notification has been sent to @email ', array('@email' => $dest));
            drupal_set_message(t("Your item request has been successfully submitted."));
            \Drupal::logger('custom_mail_sending_log')->error($log);
        } else {
            drupal_set_message(t("Your request failed. Please contact your local library or email [email protected]."));
        }

In the Drupal log, it shows a severe error but I don't have any other details. I've checked the mail log but it doesn't show anything. How can I troubleshoot this? Thank you!

UPDATE: Complete code.

<?php

namespace Drupal\itemrequest\Form;

use \Drupal\Core\Form\FormBase;
use \Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;


class ItemrequestForm extends FormBase {
    public function getFormId() {
        return 'itemrequest.form';
    }

    public function buildForm(array $form, FormStateInterface $form_state) {

        $form['covid-19'] = array(
            '#markup' => Markup::create('<div style="border: 2px solid red;padding:10px;margin-bottom:10px;"><strong>Due to the COVID-19 situation, we will only be purchasing e-books and e-audiobooks at this time.</strong> You are welcome to enter your information for print books, DVDs, or CDs, and they will be purchased when the libraries re-open.  Inter-library Loans [loans from outside Yavapai County] are suspended for now.</div>'),
        );
        $form['intro'] = array(
            '#markup' => "Can't find it in the catalog? Submit your request here.",
        );
        $form['title'] = array(
            '#title' => t('Title'),
            '#size' => 100,
            '#maxlength' => 100,
            '#type' => 'textfield',
            '#required' => TRUE,
        );
        $form['author'] = array(
            '#title' => t('Author'),
            '#size' => 50,
            '#maxlength' => 50,
            '#type' => 'textfield',
            '#required' => TRUE,
        );
        $form['materialtype'] = array(
            '#title' => t('Material Type'),
            '#type' => 'select',
            '#options' => array(
                'E-Book (OverDrive)' => 'E-Book (OverDrive)',
                'E-Audiobook (OverDrive)' => 'E-Audiobook (OverDrive)',
                'Print Book' => 'Print Book',
                'DVD Video' => 'DVD Video',
                'Blu-ray Video' => 'Blu-ray Video',
                'Audiobook on CD' => 'Audiobook on CD',
            ),
            '#required' => TRUE,
        );
        $form['ISBN'] = array(
            '#title' => t('ISBN'),
            '#size' => 13,
            '#maxlength' => 13,
            '#type' => 'textfield',
        );
        $form['edition'] = array(
            '#title' => t('Edition'),
            '#size' => 30,
            '#maxlength' => 30,
            '#type' => 'textfield',
        );
        $form['name'] = array(
            '#title' => t('Patron Name'),
            '#size' => 50,
            '#maxlength' => 50,
            '#type' => 'textfield',
            '#required' => TRUE,
        );
        $form['phone'] = array(
            '#title' => t('Phone Number'),
            '#size' => 15,
            '#maxlength' => 15,
            '#type' => 'textfield',
            '#required' => TRUE,
        );
        $form['barcode'] = array(
            '#title' => t('Library Card Number'),
            '#size' => 14,
            '#maxlength' => 14,
            '#type' => 'textfield',
            '#required' => TRUE,
        );
        $form['email'] = array(
            '#title' => t('Email Address'),
            '#size' => 30,
            '#maxlength' => 30,
            '#type' => 'textfield',
        );
        $form['pickuplib'] = array(
            '#title' => t('Pickup Library'),
            '#type' => 'select',
            '#options' => array(
                'Ash Fork Public Library' => 'Ash Fork Public Library',
                'Bagdad Public Library' => 'Bagdad Public Library',
                'Beaver Creek Public/School Library' => 'Beaver Creek Public/School Library',
                'Black Canyon City Community Library' => 'Black Canyon City Community Library',
                'Clark Memorial Library' => 'Clark Memorial Library',
                'Congress Public Library' => 'Congress Public Library',
                'Cordes Lakes Public Library' => 'Cordes Lakes Public Library',
                'Crown King Public Library' => 'Crown King Public Library',
                'Dewey-Humboldt Town Library' => 'Dewey-Humboldt Town Library',
                'Mayer Public Library' => 'Mayer Public Library',
                'Paulden Public Library' => 'Paulden Public Library',
                'Seligman Public Library' => 'Seligman Public Library',
                'Spring Valley Library' => 'Spring Valley Library',
                'Wilhoit Public Library' => 'Wilhoit Public Library',
                'Yarnell Public Library' => 'Yarnell Public Library',
                'Yavapai Free Library District' => 'Yavapai Free Library District',
            ),
            '#required' => TRUE,
        );
        $form['notes'] = array(
            '#title' => t('Notes'),
            '#description' => t('Anything else you want to tell us?'),
            '#type' => 'textfield',
        );  
        $form['requiredfield'] = array(
            '#markup' => "<span style='color:red'>*</span> required field",
            '#suffix' => "<br /><br />",
        );
        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => 'Submit Your Request',
        );
        return $form;
    }

    public function validateForm(array &$form, FormStateInterface $form_state) {

    }

    public function submitForm(array &$form, FormStateInterface $form_state) {
        $params = array(
            'title' => $form_state->getValue('title'),
            'author' => $form_state->getValue('author'),
            'materialtype' => $form_state->getValue('materialtype'),
            'isbn' => $form_state->getValue('ISBN'),
            'edition' => $form_state->getValue('edition'),
            'name' => $form_state->getValue('name'),
            'phone' => $form_state->getValue('phone'),
            'barcode' => $form_state->getValue('barcode'),
            'email' => $form_state->getValue('email'),
            'pickuplib' => $form_state->getValue('pickuplib'),
            'notes' => $form_state->getValue('notes')
        );

        $dest = \Drupal::config('itemrequest.settings')->get('itemrequest_email');

        $newmail = \Drupal::service('plugin.manager.mail');
        if ($newmail->mail('itemrequest', 'patronitemrequest', $dest, 'en', $params, NULL, TRUE)) {
            $log = t('An email notification has been sent to @email ', array('@email' => $dest));
            drupal_set_message(t("Your item request has been successfully submitted."));
            \Drupal::logger('custom_mail_sending_log')->error($log);
        } else {
            drupal_set_message(t("Your request failed. Please contact your local library."));
        }
    }

}

function itemrequest_mail($key, &$message, $params) {
    $headers = array(
        'MIME-Version' => '1.0',
        'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
        'Content-Transfer-Encoding' => '8Bit',
        'X-Mailer' => 'Drupal'
    );
    foreach ($headers as $key1 => $value) {
        $message['headers'][$key1] = $value;
    }
    switch ($key) {
        case 'patronitemrequest': 
            $message['subject'] = t('Item Request - !value', array('!value' => $params['pickuplib']));
            $message['body'][] = "<font color='black'>";
            $message['body'][] = t('Title: !value <br />', array('!value' => $params['title']));
            $message['body'][] = t('Author: !value <br />', array('!value' => $params['author']));
            $message['body'][] = t('Material Type: !value <br />', array('!value' => $params['materialtype']));
            $message['body'][] = t('ISBN: !value <br />', array('!value' => $params['isbn']));
            $message['body'][] = t('Edition: !value <br />', array('!value' => $params['edition']));
            $message['body'][] = t('Name: !value <br />', array('!value' => $params['name']));
            $message['body'][] = t('Phone: !value <br />', array('!value' => $params['phone']));
            $message['body'][] = t('Barcode: !value <br />', array('!value' => $params['barcode']));
            $message['body'][] = t('Email: !value <br />', array('!value' => $params['email']));
            $message['body'][] = t('Pickup Library: !value <br />', array('!value' => $params['pickuplib']));
            $message['body'][] = t('Notes: !value <br />', array('!value' => $params['notes']));
            $message['body'][] = t('Date: !value <br />', array('!value' => date("F j, Y")));
            $message['body'][] = "</font>";
        break;
    }       
}
8
  • drupal.stackexchange.com/questions/276930/… --- outside of that, I advise using Webform.
    – Kevin
    Mar 31, 2020 at 23:24
  • After further research, I've determined that I'm not getting the $dest value (although I've confirmed there is a value saved there). $dest = $this->config('itemrequest.settings')->get('itemrequest_email'); Is my query right for pulling a config value? Is there a package I need to include?
    – Chanel
    Apr 1, 2020 at 0:00
  • It's not clear why there is a itemrequest_mail() method and what you expect it to do.
    – apaderno
    Apr 1, 2020 at 13:44
  • As I understand it, $newmail->mail('itemrequest',... will call it and send the email. Am I wrong?
    – Chanel
    Apr 1, 2020 at 14:01
  • itemrequest_mail() is an implementation of hook_mail(), and thus has to be in itemrequest.module and NOT in your form class. It's unclear from your post whether this is what you did. Likewise, it's unclear whether you're using config() correctly because you don't show all your code. Actually, a lot of things are unclear from your post - it would be easier to get help if you showed what your "severe error" is, among other things. Right now all we can do is guess at what you did or didn't do ...
    – anonymous
    Apr 1, 2020 at 19:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.