Want to send mail through Swift Mailer in symfony

Posted By : Rahul kothari | 31-Jan-2022

We will provide you with information to use Swift mailer to send mail in symfony.

Add swift mailer configuration in config.yml.

 

# Swiftmailer Configuration
swiftmailer:
    transport: '%mailer_transport%'
    host: '%mailer_host%'
    username: '%mailer_user%'
    password: '%mailer_password%'
    encryption: 'tls'

 

Create a function like this in which create a interface  and setSubject, setfrom, setTo and setBody  function and use $this->get('mailer')->send($message) to send a email to the given email-id.

 public function quitarDistincionAction(Request $request)
    {

        $em = $this->getDoctrine()->getManager();

        $postulacion = $em->getRepository('AssertsoftMunicipalidadBundle:Postulacion')->findOneBy(array('id' => $request->get('postulacion_id')));
        $cantidad_autoevaluaciones = count($em->getRepository('AssertsoftMunicipalidadBundle:Autoevaluacion')->findBy(array('estado' => 'Aprobada')));
        $cantidad_postulaciones = count($em->getRepository('AssertsoftMunicipalidadBundle:Postulacion')->findAll());

        $postulacion->setDistincion(false);
        $postulacion->setEstado('Pendiente');
        $em->flush($postulacion);
        //$email = $postulacion->getAutoevaluacion()->getUsuario()->getEmail();
        $email = "[email protected]";
        $html = '<div>Estimado:</div>
        <div> Se le informa que a su municipalidad se le ha retirado la distinción de Municipalidad Turística de Sernatur.</div>  
        ';
        / Enviando el correo para notificar sobre la distincion  /
        $message = \Swift_Message::newInstance()
            ->setSubject('Distinción retirada')
            ->setfrom('[email protected]')
            ->setTo($email)
            ->setBody($html, 'text/html');
        $this->get('mailer')->send($message);

        return $this->render('@AssertsoftMunicipalidad/municipalidad/distincion_otorgada.html.twig', array(

            'postulacion' => $postulacion, 'cantidad_autoevaluaciones' => $cantidad_autoevaluaciones,
            'cantidad_postulaciones' => $cantidad_postulaciones,
            'distincion' => 'no'
        ));
    }

you can also provide the number of header field to describe the content. For Example in this you can Tell the repliers that this mail is automated please do not reply to this mail. 

$email = (new Email())

    ->getHeaders()

        ->addTextHeader('X-Auto-Response-Suppress', 'OOF, DR, RN, NRN, AutoReply')

;

You can provide the credentials and smtp details for swift mailer in your config file.

About Author

Author Image
Rahul kothari

Rahul is a highly experienced Backend developer, with broad range of skills and expertise in various technologies. He has practical experience working with PHP, Javascript, HTML, CSS, Laravel, Drupal, Symfony, Wordpress, REST API, GRAPH QL, and various databases. He is also knowledgeable in AWS, Jenkins, GIT, and agile methodology tools like JIRA, Confluence, and Trello. In his various projects, such as Babyflix, Script TV, Dollibar, and Sernatur, he has contributed to both front-end and back-end development, including REST API development, module development, and working with AWS services. Rahul is dedicated to delivering high-quality work and ensuring client satisfaction.

Request for Proposal

Name is required

Comment is required

Sending message..