Error code 454 when trying to send an email with Symfony mailer

I’m using:

  • Symfony 5.1.7
  • PHP 7.4.3
  • Ubuntu 20.04

I’m trying to setup a mailer service in Symfony, but I get the following error message when I try to send an email:

Expected response code "250/251/252" but got code "454", with message "454 4.7.1 <[email protected]>: Relay access denied".

Here is my config:

First, my mailer service:

// AppServicesMailerService.php

<?php

namespace AppServices;

use ...

class MailerService {

    private $mailerInterface;

    public function __construct(MailerInterface $mailerInterface)
    {
        $this->mailerInterface = $mailerInterface;
    }

    public function send(string $from, string $to, string $subject, string $htmlTemplate, array $context): void
    {
        $email = (new TemplatedEmail())
            ->from($from)
            ->to(new Address($to))
            ->subject($subject)
            ->htmlTemplate($htmlTemplate)
            ->context($context)
        ;
        $this->mailerInterface->send($email);

    }

}

Here is the controller of my Property entity; the contact form that is causing trouble is supposed to be a way for the user to ask questions about a property implementing that class:

// AppControllerPropertyController.php

<?php

namespace AppController;

use ...

class PropertyController extends AbstractController {
  
    /**
     * @var PropertyRepository
     */
    private $repository;
    /**
     * @var EntityManagerInterface
     */
    private $em;

    public function __construct(PropertyRepository $repo, EntityManagerInterface $em) {
        $this->repository = $repo;
        $this->em = $em;
    }

    /**
     * @Route("/biens", name="property.index")
     * @return Response
     */
    public function index(PaginatorInterface $paginator, Request $request):Response {

        $search = new PropertySearch();
        $form = $this->createForm(PropertySearchType::class, $search);
        $form->handleRequest($request);

        $properties = $paginator->paginate(
            $this->repository->findAllVisibleQuery($search),
            $request->query->getInt('page', 1),
            12
        ) ;
        return $this->render('property/index.html.twig', [
            'current_menu' => 'properties',
            'properties' => $properties,
            'form' => $form->createView()
        ]);
    }

    /**
     * @Route("/biens/{slug}-{id}", name="property.show", requirements={"slug": "[a-z0-9-]*"})
     * @return Response
     */
    public function show($slug, $id, Request $request, MailerService $mailerService): Response 
    {
        $property = $this->repository->find($id);

        $contact = new Contact();
        $contact->setProperty($property);
        $form = $this->createForm(ContactType::class, $contact);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {

            $this->addFlash('success', 'Votre email a bien été envoyé');

            $mailerService->send('[email protected]', '[email protected]', '[MaSuperAgence] - À propos du bien '.$contact->getProperty()->getTitle(),
                'emails/contact.html.twig', [$contact]);

            return $this->redirectToRoute('property.show', [
                'id' => $property->getId(),
                'slug' => $property->getSlug(),
            ]);
        }

        return $this->render('property/show.html.twig', [
            'property' => $property,
            'current_menu' => 'properties',
            'translation_domain' => 'forms',
            'form' => $form->createView()
        ]);
    }

}

Here is the page where my contact form (the one that is supposed to send the email) is (base.html.twig just contains the CSS and JavaScript of Bootstrap):

{# templates/property/show.html.twig #}

{% extends "base.html.twig" %}

{% block body %}
<div class="jumbotron">
    <div class="container">
        <div class="row">
            <div class="col-md-8">
                {% if property.imageFile %}
                <img src="{{ asset(path_image_upload ~'/'~ property.imageFile) }}" alt="card-img-top" style="width: 100%; height: auto;">
                {% endif %}
            </div>
            <div class="col-md-4">
                <h1>{{ property.title }}</h1>
                <h2>{{ property.rooms }} pièces - {{ property.surface }} m²</h2>
                <div class="text-primary" style="font-size: 4rem; font-weight: bold;">{{ property.formattedPrice }}</div>
                <a href="#" class="btn btn-primary">Contacter l'agence</a>
                <div id="contactForm" class="mt-4">
                    {{ form_start(form) }}
                    <div class="row">
                        <div class="col">{{ form_row(form.firstname) }}</div>
                        <div class="col">{{ form_row(form.lastname) }}</div>
                    </div>
                    <div class="row">
                        <div class="col">{{ form_row(form.phone) }}</div>
                        <div class="col">{{ form_row(form.email) }}</div>
                    </div>
                    {{ form_rest(form) }}
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">Envoyer</button>
                    </div>
                    {{ form_end(form) }}
                </div>
            </div>
        </div>
    </div>
</div>
<div class="container mt-4">

    <p>
        {{ property.description | nl2br }}
    </p>

    <div class="row">
        <div class="col-md-8">
            <h2>Caractéristiques</h2>
            <table class="table table-striped">
                <tr>
                    <td>Surface habitable</td>
                    <td>{{ property.surface }} m²</td>
                </tr>
                <tr>
                    <td>Pièces</td>
                    <td>{{ property.rooms }} </td>
                </tr>
                <tr>
                    <td>Chambres</td>
                    <td>{{ property.bedrooms }} </td>
                </tr>
                <tr>
                    <td>Etage</td>
                    <td>{{ property.floor }} </td>
                </tr>
                <tr>
                    <td>Chauffage</td>
                    <td>{{ ('property.heat.' ~ property.heat) |trans }} </td>
                </tr>
            </table>
        </div>
        <div class="col-md-4">
            <h2>Spécificités</h2>
            <ul class="list-group">
                {% for option in property.options %}
                    <li class="list-group-item">{{ option.name }}</li>
                {% endfor %}
            </ul>
        </div>
    </div>
</div>
{% endblock %}

{% block title %}

{% endblock %}

Here is my environment:

# .env

APP_ENV=dev
MAILER_DSN=smtp://in-v3.mailjet.com:587?encryption=tls&auth_mode=login&username=MY_USERNAME&password=MY_PASSWORD
DATABASE_URL=mysql://root:[email protected]_domain:3306/masuperagence?serverVersion=5.7

And here is the simple email template I use for testing:

{# templates/emails/contact.html.twig #}

<html>
    <body>
        Bonjour !
    </body>
</html>

Any idea, or suggestion?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *