<?php
namespace App\Services;
use App\Entity\User;
use Swift_Attachment;
use Twig\Environment;
use App\Entity\Command;
use App\Entity\Company;
use App\Entity\Location;
use App\Model\ContactDTO;
use App\Entity\Abonnement;
use PhpParser\Builder\Param;
use App\Entity\ContactProduct;
use Symfony\Component\Mime\Email;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Mime\Address;
use App\Repository\CautionRepository;
use App\Repository\CommandRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Mime\Crypto\DkimSigner;
class Mail
{
/**
* @var \Swift_Mailer
*/
private $swift_Mailer;
/**
* @var Environment
*/
private $environment;
private $commandrepo;
private $em;
/**
* @var CautionRepository
*/
private $caution;
// private $domainName;
private $parameterBag;
/**
* @var MailerInterface
*/
private $mailer;
public function __construct(
\Swift_Mailer $swift_Mailer,
Environment $environment,
CommandRepository $commandrepo,
EntityManagerInterface $em,
CautionRepository $caution,
// EnvVarLoaderInterface $domainName
ParameterBagInterface $parameterBag,
MailerInterface $mailer
) {
$this->swift_Mailer = $swift_Mailer;
$this->environment = $environment;
$this->commandrepo = $commandrepo;
$this->em = $em;
$this->caution = $caution;
// $this->domainName = $domainName;
$this->parameterBag = $parameterBag;
$this->mailer = $mailer;
}
public function contact(ContactDTO $contactDTO)
{
$message = (new \Swift_Message('Contact ' . ucfirst($_ENV['PLATEFORM_NAME'])))
->setFrom($contactDTO->getEmail())
->setTo('contact@providerstravel.fr')
->setBody(
$this->environment->render(
'front/email/contact.html.twig',
['contact' => $contactDTO]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function contactProduct(ContactProduct $contactProduct, string $message)
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Réponse à votre demande'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($contactProduct->getEmail())
->setBody(
$this->environment->render(
'front/email/contact_product.html.twig',
['contactProduct' => $contactProduct, 'message' => $message]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function contactPlatform(Company $company, string $subject, string $message)
{
$message = (new \Swift_Message('Contact ' . ucfirst($_ENV['PLATEFORM_NAME'])))
->setFrom($company->getEmail())
->setTo('contact@providerstravel.fr')
->setBody(
$this->environment->render(
'front/email/contact_platform.html.twig',
['company' => $company, 'subject' => $subject, 'message' => $message]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function contactProvider(Company $company, ContactProduct $contactProduct)
{
$message = (new \Swift_Message('Contact ' . ucfirst($_ENV['PLATEFORM_NAME'])))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($company->getEmail())
->setBody(
$this->environment->render(
'front/email/contact_provider.html.twig',
['company' => $company, 'contactProduct' => $contactProduct]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function ActivationCompte(Company $company)
{
$message = (new \Swift_Message('Activation compte ' . ucfirst($_ENV['PLATEFORM_NAME'])))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($company->getEmail())
// ->setTo('nyainatester@gmail.com')
;
// $imgActive = $message->embed(\Swift_Image::fromPath('images/mail/activation_img.jpg'));
$message->setBody(
$this->environment->render(
'front/email/activation_compte.html.twig',
['company' => $company]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function ActivationCompteUser(User $user)
{
$domainName = $_ENV['EXECUTION_ENV'] === 'local' ?
$this->parameterBag->get('kernel.project_dir') . '\public' :
$_ENV['DOMAIN_NAME'];
$message = (new \Swift_Message('Activation compte ' . ucfirst($_ENV['PLATEFORM_NAME'])))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($user->getEmail())
->setBody(
$this->environment->render(
'front/email/activation_compte_client.html.twig',
['user' => $user]
),
'text/html'
)
->attach(
\Swift_Attachment::fromPath($domainName . '/images/recap_souscription.pdf')->setFilename('recapitulatif_souscription.pdf')
);
$this->swift_Mailer->send($message);
}
public function ConfirmationClient(User $user)
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation inscription'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($user->getEmail());
$message->setBody(
$this->environment->render(
'front/email/confirmation_compte_client.html.twig',
['user' => $user],
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function ConfirmationNewAdmin(User $user, string $password)
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation inscription'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($user->getEmail())
->setBody(
$this->environment->render(
'front/email/confirmation_new_admin.html.twig',
['user' => $user, 'password' => $password]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function ConfirmationPaiementClient(User $user, Command $command, string $path)
{
$message = (new \Swift_Message($_ENV['PLATEFORM_NAME'] . ' - confirmation d’achat'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($user->getEmail())
// ->setTo('nyainatester@gmail.com')
;
// $img1 = $message->embed(\Swift_Image::fromPath('images/img-mail-1.jpeg')) ?? "";
// $img2 = $message->embed(\Swift_Image::fromPath('images/img-mail-2.jpeg')) ?? "";
$message->setBody(
$this->environment->render(
'front/email/confirmation_paiement.html.twig',
compact('user', 'command')
),
'text/html'
)
->attach(Swift_Attachment::fromPath($path));
$this->swift_Mailer->send($message);
}
/**
* method to call after a company's subscription is finished
*/
public function confirmCompanySSubscription(Company $company, Abonnement $abonnement, string $path)
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation d’achat'))
->setFrom($_ENV['MAILER_USERNAME'])
// ->setTo('contact@providerstravel.fr');
->setTo($company->getEmail());
//$img1 = $message->embed(\Swift_Image::fromPath('images/img-mail-1.jpeg')) ?? "";
//$img2 = $message->embed(\Swift_Image::fromPath('images/img-mail-2.jpeg')) ?? "";
$message->setBody(
$this->environment->render(
'company_space/subscription/billing_email_template.html.twig',
compact('company', 'abonnement')
),
'text/html'
)
->attach(Swift_Attachment::fromPath($path));
$this->swift_Mailer->send($message);
}
public function confirmationCommandCompany(array $companies)
{
/** @var Company $company */
foreach ($companies as $company) {
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Commande effectuée'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($company->getEmail())
->setBody(
$this->environment->render(
'front/email/confirmation_paiement_company.html.twig'
),
'text/html'
);
$this->swift_Mailer->send($message);
}
}
public function subscriptionConfirm(Company $company)
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation inscription'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($company->getEmail())
->setBody(
$this->environment->render(
'front/email/subscription_confirm.html.twig',
['company' => $company]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function subscriptionConfirmRoAdmin()
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Inscription prestataire'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($_ENV['GOOGLE_EMAIL_ADMIN_'])
->setBody(
$this->environment->render(
'front/email/subscription_confirm_admin.html.twig'
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function forgotPassword(string $newPassword, $user)
{
if ($user instanceof User) {
$template = 'front/email/forgot_password_user.html.twig';
} elseif ($user instanceof Company) {
$template = 'front/email/forgot_password_company.html.twig';
} else {
throw new NotFoundResourceException('Template not found');
}
$email = (new TemplatedEmail())
->from($_ENV['MAILER_USERNAME'])
->to(new Address($user->getEmail()))
->subject('Réinitialisation de mot de passe!')
// path of the Twig template to render
->htmlTemplate($template)
// pass variables (name => value) to the template
->context([
'password' => $newPassword,
'user' => $user
])
;
$this->mailer->send($email);
}
public function DemmandeAvisClient()
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - demmande avis au client'))
->setFrom('radokoadolphe@gmail.com')
->setTo('rajoharisonadolphe@gmail.com')
->setBody(
$this->environment->render(
'front/email/avis.html.twig'
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function confirmavis()
{
$commandes = $this->commandrepo->getCommandPasAvis();
foreach ($commandes as $command) {
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Demande avis'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($command->getClient()->getEmail())
->setBody(
$this->environment->render(
'front/email/confirmation_avis_client.html.twig'
),
'text/html'
);
$this->swift_Mailer->send($message);
$command->setIsAvis(true);
$this->em->persist($command);
$this->em->flush();
}
}
public function confirmRetourCaution()
{
$cautions = $this->caution->findAll();
$domainName = $_ENV['DOMAIN_NAME'];
foreach ($cautions as $caution) {
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Demande avis'))
->setFrom($_ENV['MAILER_USERNAME'])
->setTo($caution->getUser()->getEmail())
->setBody(
$this->environment->render(
'front/email/confirma_retour_cation.html.twig',
[
'domain_name' => $domainName
]
),
'text/html'
);
$this->swift_Mailer->send($message);
}
}
public function testmail()
{
$message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - demmande avis au client'))
->setFrom('toetrainfo@gmail.com')
->setTo('nyainalovanirina@gmail.com');
$img1 = $message->embed(\Swift_Image::fromPath('images/img-mail-1.jpeg')) ?? "";
$img2 = $message->embed(\Swift_Image::fromPath('images/img-mail-2.jpeg')) ?? "";
$imgLogo = $message->embed(\Swift_Image::fromPath('images/logo noir.png')) ?? "";
$message->setBody(
$this->environment->render(
'front/email/confirmation_paiement.html.twig',
compact('imgLogo', 'img1', 'img2')
),
'text/html'
);
$this->swift_Mailer->send($message);
}
public function confirm_location_refund(Location $location)
{
$domainName = $_ENV['DOMAIN_NAME'];
$email = (new TemplatedEmail())
->from($_ENV['MAILER_USERNAME'])
->to(new Address($location->getProduct()->getCompany()->getEmail()))
->addTo(new Address($location->getClient()->getEmail()))
->subject('Thanks for signing up!')
// path of the Twig template to render
->htmlTemplate('front/email/confirmation_refund.html.twig')
// pass variables (name => value) to the template
->context([
'domain_name' => $domainName,
'location' => $location,
])
;
$this->mailer->send($email);
}
public function testmailer(string $destination = 'hentsraf@gmail.com') {
$email = (new TemplatedEmail())
->from($_ENV['MAILER_USERNAME'])
->to($destination)
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->htmlTemplate('front/email/test_email.html.twig');
// ->html('<p>See Twig integration for better HTML integration!</p>');
// $signer = new DkimSigner('file://'.$_ENV['DKIM_PRIVATE_KEY_PATH'], $_ENV['DKIM_DOMAIN'], $_ENV['DKIM_SELECTOR']);
// $signedEmail = $signer->sign($email);
$this->mailer->send($email);
}
}