Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

557
Views
¿Cómo enviar correo con Symfony Mailer usando parámetros de conexión dinámica?

Estoy tratando de enviar un correo electrónico con parámetros de conexión SMTP dinámicos. Estos parámetros se recuperarán de una base de datos. Por lo tanto, especificar los parámetros en el archivo .env (p. ej.: MAILER_DSN=smtp://user:pass@smtp.example.com:port ) como se explica en los documentos oficiales o definir múltiples transportes en archivos .yaml no se ajusta a mis requisitos.

¿Cómo podría enviar un correo electrónico definiendo el transporte de correo mediante programación? Por ejemplo, me gustaría hacer:

 // I'd like to define $customMailer with some data retrieved from DB $email = (new TemplatedEmail()) ->from(new Address('example-from@example.com', 'Example')) ->to('example-to@example.com') ->subject('Subject') ->htmlTemplate('emails/my-template.html.twig') ->context([]); $customMailer->send($email);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hay algunas consideraciones a tener en cuenta al ejecutar el programa:

 use Symfony\Bridge\Twig\Mime\BodyRenderer; use Symfony\Component\Mailer\Transport; use Symfony\Component\Mailer\Mailer; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\Mime\Address; use Twig\Environment; use Twig\Loader\FilesystemLoader; // In my case this data is extracted from the DB $user = 'example@gmail.com'; $pass = 'my-password'; $server = 'smtp.gmail.com'; $port = '465'; // Generate connection configuration $dsn = "smtp://" . $user . ":" . $pass . "@" . $server . ":" . $port; $transport = Transport::fromDsn($dsn); $customMailer = new Mailer($transport); // Generates the email $email = (new TemplatedEmail()) ->from(new Address('example-from@example.com', 'Example')) ->to('example-to@example.com') ->subject('Subject') ->htmlTemplate('emails/my-template.html.twig') ->context([]); // IMPORTANT: as you are using a customized mailer instance, you have to make the following // configuration as indicated in https://github.com/symfony/symfony/issues/35990. $loader = new FilesystemLoader('../templates/'); $twigEnv = new Environment($loader); $twigBodyRenderer = new BodyRenderer($twigEnv); $twigBodyRenderer->render($email); // Sends the email $customMailer->send($email);

¡Espero que evite que alguien pierda su tiempo!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!