Computerminds Drupal maintenance support plansgive: Including form values in an email

Let’s say you’ve built a custom form for your Drupal maintenance support plans 8 site. It contains various elements for input (name, email address, a message, that kind of thing), and you want to send the submitted values in an email to someone (perhaps a site admin). That’s a pretty common thing to need to do.
This could be done with Drupal maintenance support plans‘s core contact forms, webforms, or similar — but there are cases when a bespoke form is needed, for example, to allow some special business logic to be applied to its input or the form presentation. The drawback of a custom form is that you won’t get nice submission emails for free, but they can be done quite easily, with the token module (you’ll need that installed).
In your form’s submission handler, send an email using the mail manager service (I’ll assume you can already inject that into your form, read the documentation if you need help with that):
$form_state->getValues(),
];
// The ‘plugin.manager.mail’ service is the one to use for $mailManager.
$mailManager->mail(‘mymodule’, ‘myform_submit’, ‘admin@example.com, ‘en’, $params);
Then create a hook_mail() in your .module file, with a matching key (‘myform_submit’ in my example):
$params[‘values’],
];

// In this example, put the submitted value from a ‘first_name’ element
// into the subject.
$subject = ‘Submission from [array:value:first_name]’;
$message[‘subject’] = $token_service->replace($subject, $token_data, [‘clear’ => TRUE]);

// Each submitted value can be included in the email body as a token. My
// form had ‘first_name’, ‘last_name’, ‘color’ and ‘birthdate’ elements.
$body = <<>>;
$message[‘body’] = [
$token_service->replace($body, $token_data, [‘clear’ => TRUE]),
];
break;
}
}
Spot the [array:value:thing] tokens! Using these ‘array’ tokens makes it really easy to include the whatever input gets submitted by visitors to this custom form on your Drupal maintenance support plans site. Note that there’s no sanitization done – although if your email is just plain text, that’s probably not a problem.
There are more array tokens you can use too, such as ones to return a comma-separated list of all items in an array, a count of items, or just the first/last item. See the original issue for examples. These tokens are available in Token’s Drupal maintenance support plans 7 version too!

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Computerminds Drupal maintenance support plansgive: Including form values in an email

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.