1

it's my first time using mail catcher and I was wondering why my code runs through for sending an e-mail but I don't see anything in my mail hog / mail catcher.

Here is how I send my e-mail

Config:

###> symfony/mailer ###
MAILER_DSN=smtp://email@email.com:PASS@sslout.df.eu:465
###< symfony/mailer ###

These are the actual live configs, which are in my .env file

Then I shave a simple form

 <section>

                {{ form_start(form, {'attr': {'class': 'custom-form'}}) }}

                <form method="post" action="#">
                    <div class="fields">
                        <div class="field half">
                            {{ form_row(form.name, {'attr': {'class': 'form-row'}}) }}
                        </div>
                        <div class="field half">
                            {{ form_row(form.email, {'attr': {'class': 'form-row'}}) }}
                        </div>
                        <div class="field">
                            {{ form_row(form.message, {'attr': {'class': 'form-row'}}) }}
                        </div>
                    </div>
                    <ul class="actions">
                        {{ form_row(form.submit, {'attr': {'class': 'form-row'}}) }}
                    </ul>
                </form>
            </section>

            {{ form_end(form) }}

And this is how I send my e-mail

    /**
 * @var MailerInterface
 */
private MailerInterface $mailer;

/**
 * @param MailerInterface $mailer
 */
public function __construct(MailerInterface $mailer)
{
    $this->mailer = $mailer;
}


#[Route('/', name: 'app_contact')]
public function index(Request $request)
{
    $form = $this->createForm(ContactFormType::class);
    $form->handleRequest($request);

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

        $data = $form->getData();

        try {
            $this->sendMail(
                $data['name'],
                $data['email'],
                $data['message'],
            );
            
        } catch (\Exception $e) {

            $e->getMessage();


        }

        header('/');

      //  return $this->render('contact/success.html.twig');

    }



    return $this->render('contact/contact.html.twig', [
        'form' => $form->createView()
    ]);
}

/**
 * @param MailerInterface $mailer
 * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
 */
public function sendMail($name, $email, $message)
{
    $sendTo = 'contact@build-yourself';

    $email = (new Email())
        ->from($email)
        ->to($sendTo)
        ->subject('Contact Email from: ' . $name)
        ->text($message);

    $this->mailer->send($email);

}

When I debug the send function it says that everything is okay and it runs, why can't I see my sent e-mail inside my mailhog

  mailhog:
image: mailhog/mailhog
ports:
  - 1025:1025 # smtp server
  - 8025:8025 # web ui
MewTwo
  • 465
  • 4
  • 14
  • 2
    Try changing your mailer DSN to `MAILER_DSN="smtp://127.0.0.1:1025"` for mailhog – Brent Jan 03 '23 at 13:12
  • @Brent I was experiment with different settings. I tried with localhost and 127 and i get `"Connection could not be established with host "127.0.0.1:1025": stream_socket_client(): Unable to connect to 127.0.0.1:1025 (Connection refused)"` – MewTwo Jan 03 '23 at 13:15
  • I also tried `MAILER_DSN=smtp://localhost` but here I get `Connection could not be established with host "localhost:25": stream_socket_client(): Unable to connect to localhost:25 (Cannot assign requested address)` – MewTwo Jan 03 '23 at 13:16
  • 1
    Correct me if i'm wrong, but it seems like you're using docker. Docker networking can be annoying. Try [@MewTwo](https://stackoverflow.com/users/9953906/mewtwo)'s answer and change it to `smtp://mailhog:1025` – Brent Jan 03 '23 at 13:46

2 Answers2

0

it seems that you can't upvote an answer if it works for you since you don't have enough reputation but you can answer

what worked for me and what answered some of you, for those using Docker

adding in .env MAILER_DSN=smtp://{container name}:1025

I was using the schickling/mailcatcher docker image for mailcatcher and the MAILER_DSN=smtp://mailer:1025 worked

Ioan
  • 11
  • 2
-1

Here is how it worked for me

 MAILER_DSN=smtp://mailhog:1025
MewTwo
  • 465
  • 4
  • 14