I'D found how to resolve the problem which is How to add a DSN from Database in Transport with Symfony Mailer...
I'd created a service like that:
namespace App\Services;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\IdentifiantMailing;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Twig\Environment;
class EmailSender
{
private $em;
private $adresse;
private $mdp;
private $serveur;
private $port;
private $from;
private $twig;
public function __construct(EntityManagerInterface $em, Environment $twig)
{
$this->em = $em;
$IdentifiantRepos = $this->em->getRepository(IdentifiantMailing::class);
$identifiant = $IdentifiantRepos->findAll();
$this->adresse = $identifiant[0]->getAdresse();
//$this->adresse = rawurlencode($this->adresse);
$this->mdp = $identifiant[0]->getMotDePasse();
//$this->mdp = rawurlencode($this->mdp);
$this->serveur = $identifiant[0]->getServeur();
$this->port = $identifiant[0]->getPort();
$this->from = $identifiant[0]->getDepuis();
$this->twig = $twig;
}
public function sendEmail($sender, $destinataire, $subject, $pathTwigTemplate, $PJ, $context)
{
$dsn = sprintf('smtp://%s:%s@%s:%d', $this->adresse, $this->mdp, $this->serveur, $this->port);
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);
if(null===$context){
if(null===$PJ){ //$context est nul et $PJ est nul
$email = (new Email())
->from($sender)
->to($destinataire)
->subject($subject)
->html($this->twig->render($pathTwigTemplate));
$headers= $email->getHeaders();
$headers->addTextHeader('X-Auto-Response-Suppress', 'OOF', 'DR', 'RN', 'NRN', 'AutoReply');
} else {//$context est nul et $PJ existant
$email = (new Email())
->from($sender)
->to($destinataire)
->subject($subject)
->html($this->twig->render($pathTwigTemplate))
->attachFromPath($PJ);
$headers= $email->getHeaders();
$headers->addTextHeader('X-Auto-Response-Suppress', 'OOF', 'DR', 'RN', 'NRN', 'AutoReply');
}
} else {
if(null===$PJ){ //si $context est existant et $PJ est null
$email = (new Email())
->from($sender)
->to($destinataire)
->subject($subject)
->html($this->twig->render($pathTwigTemplate, $context));
$headers= $email->getHeaders();
$headers->addTextHeader('X-Auto-Response-Suppress', 'OOF', 'DR', 'RN', 'NRN', 'AutoReply');
} else {//si $context est existant et $PJ est existant
$email = (new Email())
->from($sender)
->to($destinataire)
->subject($subject)
->html($this->twig->render($pathTwigTemplate, $context))
->attachFromPath($PJ);
$headers= $email->getHeaders();
$headers->addTextHeader('X-Auto-Response-Suppress', 'OOF', 'DR', 'RN', 'NRN', 'AutoReply');
}
}
$mailer->send($email);
}
}
An I use this like That:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Services\EmailSender;
final class TestSymf7Controller extends AbstractController
{
#[Route('/test', name: 'test')]
public function index(): Response
{
return $this->render('test/index.html.twig', [
'controller_name' => 'TestSymf7Controller',
]);
}
#[Route('/testmessage', name: 'testmessage')]
public function sendEmail(EmailSender $emailSender): Response
{
/*******************************Preparation de l'email***************************
******************to consume messenger in dev run
*******************php bin/console messenger:consume async -vv**************************************/
//must be not real sender for wouldn't answering or may be real for no spams emails client routing ???
$sender = '[email protected]';
$destinataire = '[email protected]';
$subject = 'Exemple d\'e-mailtest1';
//must be in /templates/emails/..
$pathTwigTemplate = '/emails/inscriptionok.html.twig';
//must be in /public/piecesemails/..
//$PJ = '/public/piecesemails/pj.pdf';$PJ = $this->getParameter('kernel.project_dir') . $PJ;
// ou de type $PJ = null
$PJ = null;
//$context de type array ou = null
$context = ['email' => '[email protected]'];
//$context = null;
$emailSender->sendEmail($sender, $destinataire, $subject, $pathTwigTemplate, $PJ, $context);
/************************************email envoyé********************************/
return new Response(
'<html><body>OK Done !</body></html>'
);
}
}
It Run...
For more the Entity is like that :
namespace App\Entity;
use App\Repository\IdentifiantMailingRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: IdentifiantMailingRepository::class)]
class IdentifiantMailing
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type:"integer")]
private $id;
#[ORM\Column(type:"string", length:255)]
private $adresse;
#[ORM\Column(type:"string", length:255)]
private $motDePasse;
#[ORM\Column(type:"string", length:255)]
private $serveur;
#[ORM\Column(type:"string", length:6)]
private $port;
#[ORM\Column(type:"string", length:255)]
private $depuis;
#[ORM\Column(type:"text")]
private $message;
public function getId(): ?int
{
return $this->id;
}
public function getAdresse(): ?string
{
return $this->adresse;
}
public function setAdresse(string $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getMotDePasse(): ?string
{
return $this->motDePasse;
}
public function setMotDePasse(string $motDePasse): self
{
$this->motDePasse = $motDePasse;
return $this;
}
public function getServeur(): ?string
{
return $this->serveur;
}
public function setServeur(string $serveur): self
{
$this->serveur = $serveur;
return $this;
}
public function getPort(): ?string
{
return $this->port;
}
public function setPort(string $port): self
{
$this->port = $port;
return $this;
}
public function getDepuis(): ?string
{
return $this->depuis;
}
public function setDepuis(string $depuis): self
{
$this->depuis = $depuis;
return $this;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(string $message): self
{
$this->message = $message;
return $this;
}
}