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 protected]:[email protected]: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 SymfonyComponentMailerExceptionTransportExceptionInterface
*/
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
2
Answers
Here is how it worked for me
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