Sending emails

📘

This is an article for advanced users of Frontastic.

Most emails should be sent by the order processing systems and not by Frontastic itself. But, Frontastic does send all account-related emails, like registration confirmations, and forgot password emails. The easiest way to do this is to configure an SMTP server, but you can also write a custom mailer which talks to a web service for sending these emails — this is especially useful to maintain email templates outside of Frontastic.

Configure outgoing mail server

The easiest way to set up mail handing is by configuring an SMTP server which has to be reachable from the production servers with their changing IPs. For this use the committed environment file inside your project directory, for example, <customer>_<project>/environment and add the following settings:

smtp_host=in-v3.mailjet.com
smtp_port=587
smtp_encryption=tls
smtp_user=<myMailJetUser>
smtp_password=<myMailJetPassword>
[email protected]

Frontastic doesn't provide any default email templates at the moment. You can create your own templates in the directory <customer>_<project>/templates/Emails/ (mind the capital E) which can then be used for the email templates listed below. We send emails as text and HTML by default, so there are 2 templates for every email Frontastic sends:

  • Registration: register.html.twig and register.txt.twig
  • Reset Password: reset.html.twig and reset.txt.twig
  • On top of this, it makes sense to create an email layout template for HTML emails: layout.html.twig

An example template for the register.txt.twig could look like:

Hello {{user.salutation}} {{user.lastName}},

To complete the registration please click on the following link. This link
will be valid until {{user.tokenValidUntil|date('d.m.Y H:i')}}.

{{absolute_url(path('Frontastic.Frontend.Master.Account.confirm', {confirmationToken: token}))}}

Best regards,
...

The information provided for an email template to reset a password will look very similar:

Hello {{user.salutation}} {{user.lastName}},

We've received a request to reset the password for your account. Click
the link below to reset your password. 

If you didn't request this password reset, you can ignore this mail. Your
link is valid until {{user.tokenValidUntil|date('Y-m-d H:i')}}.

{{absolute_url(path('Frontastic.Frontend.Master.Account.forgotPassword', {confirmationToken: token}))}}

Best regards,
...

Custom mailer

🚧

This method is Experimental so use at your own risk.

If you can't send out emails using a common SMTP server, you can overwrite the mailer class and use any service to trigger sending these emails. For this, you can use the default Symfony mechanism in the dependency injection container to overwrite the default mailer. To do this, just add the below lines to a services.xml in one of your project bundles (if there isn't a bundle yet, you can create one using bin/console frontastic:create:bundle MyDecorators, for example):

<service
         id="Frontastic\Common\CoreBundle\Domain\Mailer"
         class="Acme\MyDecoratorsBundle\Domain\MyMailer" />

The customer mailer class must extend and overwrite the Frontastic\\Common\\CoreBundle\\Domain\\Mailer class, which only has a single method:

namespace Acme\MyDecoratorsBundle\Domain;

use Frontastic\Common\CoreBundle\Domain\Mailer;

class MyMailer extends Mailer
{
    public function sendToUser($user, string $type, string $subject, array $parameters = array())
        {
            // @TODO: Send mail
        }
}

The type parameter should be register or reset as used in the template names before. The token will be part of the $parameters array.

Checking emails

If you're using a Frontastic sandboxFrontastic sandbox - A virtual machine hosted in the cloud that you can use for backend and some frontend development. They're provisioned from the developer area of the Frontastic studio., once you've run frontastic run (which creates the entry in /etc/hosts), you can run open http://<machineHost>:8025/ and you'll see the sent emails there.


Did this page help you?