Skip to content

Instantly share code, notes, and snippets.

@konshensx16
Created September 11, 2019 10:35
Show Gist options
  • Save konshensx16/f52badcb479e8b69b3d4c1e37773c553 to your computer and use it in GitHub Desktop.
Save konshensx16/f52badcb479e8b69b3d4c1e37773c553 to your computer and use it in GitHub Desktop.
Send an email using Gmail Symfony 4
Firs thing to remember is that you need to create an app and use the password that was generated for you.
instead of using your account's password
This is what the .env file should include for example to make this work.
###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://mitseo65@gmail.com:dsnuwmnkfnfduwhh@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=gmail://theemail@gmail.com:therandompassword@localhost
###< symfony/swiftmailer-bundle ###
<?php
namespace App\Controller;
use App\Entity\Event;
use App\Repository\EventRepository;
use App\Repository\SettingsRepository;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcher;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class EventController extends AbstractFOSRestController
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var EventRepository
*/
private $eventRepository;
/**
* @var SettingsRepository
*/
private $settingsRepository;
private $mailer;
public function __construct(EntityManagerInterface $entityManager, EventRepository $eventRepository, SettingsRepository $settingsRepository, \Swift_Mailer $mailer)
{
$this->entityManager = $entityManager;
$this->eventRepository = $eventRepository;
$this->settingsRepository = $settingsRepository;
$this->mailer = $mailer;
}
/**
* @Rest\RequestParam(name="startDate", description="Start date for the event")
* @Rest\RequestParam(name="name", description="The fullname of the person requesting the rendez-vous")
* @Rest\RequestParam(name="email", description="The emial of the person requesting the rendez-vous")
* @Rest\RequestParam(name="telephone", description="The phone number of the person requesting the rendez-vous")
* @Rest\RequestParam(name="sujet", description="The subjet of the event the person is requesting")
* @Rest\RequestParam(name="rendezVous", description="The rendezvousthe user chose for this event")
*/
public function postEventAction(ParamFetcher $fetcher)
{
$dateFormat = 'Y-m-d H:i:s';
$name = $fetcher->get('name');
$startTimestamp = $fetcher->get('startDate');
$email = $fetcher->get('email');
$telephone = $fetcher->get('telephone');
$sujet = $fetcher->get('sujet');
// NOTE: this is the rendez vous the user chose.
$type = $fetcher->get('rendezVous');
if ($name && $email && $startTimestamp) {
// TODO: get the interval from the backend (somewhere)
$interval = 5400 - 900; // one hour and a quarter
// set the start and end date
$startDate = (new \DateTime())->setTimestamp($startTimestamp / 1000);
$endDate = (new \DateTime())->setTimestamp($startTimestamp / 1000);
// TODO: start and end dates will based of type the user choses
// NOTE: this type is not yet persisted to the database
switch ($type) {
case 'Première consultation Hypnose 90 minutes':
$endDate->add(new \DateInterval("PT5400S"));
break;
case 'Première consultation Sophrologie 90 minutes':
$endDate->add(new \DateInterval("PT5400S"));
break;
case 'Suivi Hypnose - 60 minutes':
$endDate->add(new \DateInterval("PT3600S"));
break;
case 'Suivi Sophrologie 60 minutes':
$endDate->add(new \DateInterval("PT3600S"));
break;
case 'Coaching 90 minutes':
$endDate->add(new \DateInterval("PT5400S"));
break;
case 'RDV FORFAITS : 45 minutes':
$endDate->add(new \DateInterval("PT2700S"));
break;
}
// TODO: check if this spot already taken then don;t do anything
// $result = $this->eventRepository->findOneBy([ // OLD CODE
// 'startDate' => $startDate
// ]);
// NOTE: this is checking just for the events that are validated
// might need to change this include eveything
$events = $this->eventRepository->findBy([
'validated' => 1
]);
foreach ($events as $event) {
if (($startDate >= $event->getStartDate() && $startDate <= $event->getEndDate())
|| ($endDate >= $event->getStartDate() && $endDate <= $event->getEndDate())) {
return $this->view('This event cannot be created!', Response::HTTP_CONFLICT);
}
}
// TODO: turn this into a transcation in case of error
$this->entityManager->getConnection()->beginTransaction();
$event = new Event();
$event->setStartDate($startDate);
$event->setEndDate($endDate);
$event->setNom($name);
$event->setEmail($email);
$event->setTelephone($telephone);
$event->setSujet($sujet);
$this->entityManager->persist($event);
$this->entityManager->flush();
// TODO: send an email to the admin
$message = (new \Swift_Message('Hello Email'))
->setFrom('mitseo6@gmail.com') // FIXME: this needs to change ?!
->setTo('mohamedbaza16@gmail.com') // admin email here
->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'emails/event.html.twig',
['event' => $event]
),
'text/html'
)
;
$this->mailer->send($message);
$this->entityManager->commit();
return $event;
}
throw new \Exception('Something went wrong');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment