In this article, we are going to discuss about How we can create a contact form in Symfony with SwiftMailer. Symfony provides an architecture, components and tools for developers to build complex web applications faster. Choosing symfony allows you to release your applications earlier, host and scale them without problem, and maintain them over time with no surprise.
Swift Mailer is a component based library for sending e-mails from PHP applications. Swift Mailer supports PHP 7.0 to PHP 8.1 included (proc_* functions must be available). Swift Mailer does not work when used with function overloading as implemented by mbstring when mbstring.func_overload is set to 2.
At the end of this article, we will be created a contact form in Symfony using Form Builder and connected with SwiftMailer bundle. So that, the visitor could receive acknowledgement that the messages has been successfully sent.
Step 1: Create Contact Entity with DoctrineFirst of all we need to create and configure the database. Database related information and credentials are available in Application Access details panel. Add these to the app/config/parameters.yml
parameters: database_host: localhost database_port: <PORT> database_name: <DB_NAME> database_user: <DB_user_name> database_password: <DB_password>
Next, we need to create a contact entity with Doctrine. To use Doctrine, open SSH terminal and go to your Symfony project:
Now run the following command to start the entity generator:
For the contact form I need four fields: name, email, subject, message. You can add more fields as per your requirements. The entity name will be Contact and the shortcut name will be AppBundle:Contact. Annotation will be Yaml, so just press the Enter key on this.
Now go to src/AppBundle/Entity. You will see a Contact.php file in which Doctrine has created a new method for every field. I will use these methods to set values in the Controller:
Next, we will work on the Controller and the form View.
Step 2: Create the Form in DefaultController.php
The next step is to create a form in the controller. You can also create a form in the Twig view. However, in this article, I will initialize the form fields using the Symfony’s form builder, and then show the form Widget in the View.Open DefaultController.php and add the following namesapaces and the entity (created earlier):
Now in the createAction() method, create an entity object and pass it to the form builder. The form contains the input fields (as discussed earlier).
Step 3: Create View for the Contact Form
To view this form on the Twig template, create a file form.html.twig in app/Resources/views/default and add the form widget to it.
I have added bootstrap classes to the code. I will now add the bootstrap CDN to base template to make the classes work.
Open the base.html.twig from app/Resources/views and add the CDN links to it.
Now I need to extend form.html.twig with base.html.twig by simply adding the following line at the top.
At this point, if you hit the /form Route, you will get the following Form:
Step 4: Save Values in the Database
Next, we will save the values in database. We have already created the form in the DefaultController class. First, we will save the values in variables and then pass these variables to the related methods in the entity. Finally, we will save the variables through the persist() method.
Now, when the form is submitted, the values will be available in the database.
Step 5: Send Acknowledgement to the User
It is important to tell the user that their message has been successfully delivered to the website. Many websites send an email that provides all user-submitted data in a proper format.
For this purpose, I will use SwiftMailer, a bundle that comes preinstalled in Symfony 3. To use it, you just need to configure it. To do the necessary changes, open app/config/parameters.yml under parameters
Now in the config.yml, add the following code to get the values:
The configuration process is over. Now, open DefaultController.php and add the code for SwiftMailer. I will use the same data variables in this code:
Notice that you can also send email templates. In this code snippet, I sent the email template I created (sendemail.html.twig) in the views/default. This template has a simple test message.
Now, when you submit the form, you will get an acknowledgment email in your inbox.
If you have a query or would like to contribute to the discussion, do leave a comment.