Drupal Contact Form & using SMTP to Send Email

Published Jan 14, 2021
Updated Mar 5, 2021
By Simon

To enable a contact form in Drupal 9 is easy. Drupal 9 has the contact module, you can start using this straight away but be aware that your server needs to be able to send mail or you need to use an SMTP server to send your mail. I will cover this in detail after setting up the contact form but be warned this is important.

Info

Actively maintained Yes, ships with core.
Requires Part of Core with No extra requirements.

Basics

Since Contact is installed with the standard install all you need to do is check some basic configurations of the form. To do this visit the Contact forms page.

Manage > Structure > Contact forms
/admin/structure/contact

Before looking at the configuration, however, let's look at what the Contact module consists of as there are 2 types of forms. I have outlined what these are.

Types of forms

Site user contact form

You can send email between users of the site. I won't cover this as on a simple install the chances are you will only have 1 or 2 users. However, it is good to know that you can have an internal message system out-of-the-box.

Site-wide Contact

By default, there is one feedback form already available and you can manage it at /contact/manage/feedback

This will be the one you will want to use. However, you can create as many different forms that you please.

The default URL is /contact/feedback. It can also be viewed and used at /contact

Edit contact form

/contact/manage/feedback

The basic settings that need to be configured are on this page, the ones with an * are required the others are optional.

  • Label*
  • Recipients*
  • Message - a message that is displayed in the highlighted regions, leave blank if you don't want to use it. The case where you might want it to blank is if you use the next options which is a custom path and page that a person submitting the form gets redirected to.
  • Redirect path - A path to a page, could be a custom thank you page with valuable information and another CTA or message.
  • Auto-reply - Send an automated message telling the person what to expect, I.e. we will be in contact shortly.
  • Weight - this is a Drupal thing, it is the order in which it is displayed on the contact form overview page.
  • Make this the default form - The default form is shown at /contact URL all other forms are at contact/name-of-form

Add Fields

/contact/manage/feedback/fields

You can add fields just like any other content/entity type to customize the data collected. Read more about Adding Fields to Drupal if you are unsure about what to do.

The field types that are available include select menus, checkboxes, input, text areas, and images. You do have to be careful about what you allow an anonymous user to upload, however, so keep this in mind.

The default fields are enough to get started. If you want to remove any of the default fields you can do that on the manage form display page.

Manage form display

/contact/manage/feedback/form-display

This re-arranges the order in which the form fields are displayed to the customer or user. You can also drag the field to the disabled area if you don't want one of the fields to show.

Remove the Preview Button

By default the preview button and the functionality to remove it doesn't work, you need to patch it to remove it, the patch has been committed and will be in 9.1.3. If you read this before 9.1.3 is released here is the issue. Make the Preview button on the Contact Forms to obey the "Manage form display" field settings.

Manage display

/contact/manage/feedback/display

Only has the message field on it but default.

Okay with that done we should test that it can send a message. In the next section, we will look at sending email from a Drupal site.

Sending Messages from Your Site

If you try to send the form you will probably get errors, if you don't then you are lucky. The main possible reason for this is that the server doesn't have the mail modules installed. Even though it is possible to install these, check the details at the end for that, you are best to use an SMTP server to send email from your Drupal site. To do that we need to use a PHP class, PHP mailer seems to be the most wildly used and to use that we can install SMTP Module.

SMTP Authentication Support

To use SMTP you don't need to install anything on the server as PHP Mailer, the library that SMTP module enables, connects to your mail's outgoing server. This is by far the best option for delivery and security of mail. I will outline the other options in the last section so you can see what I mean. To use SMTP please have a look at the SMTP module guide.

Another SMTP Drupal Option

PHPMailer SMTP

https://www.drupal.org/project/phpmailer_smtp

The PHPMailer SMTP module is for SMTP only, it does not help to format emails with HTML. The suggested SMTP module allows you to enable sending email in HTML format, but this module doesn't allow that. For a basic contact form this is probably not an issue, but for anything else use the recommended SMTP Module.

Storing Data with Contact Storage

So you have that setup and your emails are being sent and received.

As you get more and more messages it is nice to have them stored on the site as a backup in the case that mail delivery gets interrupted (I have had this happen in the past) and this is where contact storage comes in.

Info

https://www.drupal.org/project/contact_storage

Actively maintained Yes
Requires No extra requirements

Basics

If you don't know how to install a Drupal module visit the installing a module page and then return here.

Once installed the module enables 2 new tabs on the Contact form page and a multitude of new settings like being about to customize the submit button text.

Manage > Structure > Contact form
/admin/structure/contact

These tabs are Lists and Contact Settings

Lists

/admin/structure/contact/messages

A page that lists all the submitted messages with a filter to easily filter on contact forms if you have many and an operation to bulk deleted messages. Simple but effective way to keep original submissions safe.

Always keep off site and server database back ups, even if you install contact storage as a back up safe guard you should also make sure that your complete site is safe.

Contact Settings

This page has one setting that enables you send email HTML, this requires using another module to set up correctly so I won't go down that rabbit whole here but I will follow up with an article on configuring HTML email in the near future. For now I will leave it unchecked.

Other Things to Think About

Other things to think about when setting up an email form are needing to send auto-replies, sending a copy of the message to the person sending the form, and formatting the messages you send. Both these features are built into the modules so it has you covered. Remember SMTP is needed for the HTML formatting. I will cover this stuff in future content but in the meantime if you need help be sure to get in contact.

Since contact comes with Drupal it is a great option for basic contact forms and by adding SMTP to send your forms and contact storage if you want to save them to somewhere safe you have a great solution to get going.

Overtime if you want to create more complex forms, Webform is a great choice. Read on to find out about Webform and also other options to send mail.

Another Option for Webforms

Webform

Webform I have used extensively in the past and I can say that it is exhaustive in what it can do as a point and click form builder, it has storage out-of-the-box, multi-step forms, and a plethora of other features, too many to outline here but if you need help you can shoot me a message on the contact form.

I do intend to review Drupal 9 Webform in time but my experience is with Webform for Drupal 7 and Drupal 8 with a basic use case. Some things I achieved with Webform and in some cases with customization are:

  • Automatically populating values from other pages.
  • Forwarding data onto a CRM.
  • Send HTML autoresponders this is possible with Contact form (white paper)

For Drupal 9 Webform is really the only other solution which is a good thing as it means that everybody's energy is focused on making that better.

Other Options for sending email

Mail delivery from your domain to Gmail or GSuite, in particular, is problematic if you don't use SMTP. I have experienced this with domains not using SMTP to send email from the domain and using GSuite business as the email service provider. So as I have now said multiple times you are best to use SMTP to send email.

A quick review of other options so you can make the decision if SMTP is your best option. I assure you it is, but with the information below you can make the decision yourself.

Install Apache Modules

One of the following 2 modules need to be installed

  • sendmail
    Does work but prolonged, you will probably run into delivery problems in the end.
     
  • postfix
    I found this worked, you need to have a domain name to use on install.



VPS such as Hostgator or Shared Hosting

I have used these in the past and they seemed to work so the modules above must have been installed by default. However, the same problem I have encountered with mail delivery will ultimately happen so in the end, you are best to use an outgoing SMTP server.

A Mail Server

Setting up a mail server I will mention as an option but since I have never done it I can only tell you what I have read and that is unless you have time and budget to do so you are better to use a paid ESP (email service provider) and then use SMTP. A mailer will need all that a good ESP provides with good security amongst other things, not something to take lightly and possibly outside most small and medium-size businesses budgets.

I hope this shows you how easy Drupal makes it to have a contact form on your site and what other others there are if you need them. All you really need to do though is install SMTP and use the default pre-installed form and you are good to go. Thanks for reading and be sure to sign up for the Newsletter.

Tags