English 中文(简体)
Symfony - Email Management
  • 时间:2024-10-18

Symfony - Email Management


Previous Page Next Page  

Email functionapty is the most requested feature in a web framework. Even a simple apppcation will have a contact form and the details will be sent to the system administration through email. Symfony integrates SwiftMailer, the best PHP email module available in the market. SwiftMailer is an excellent email pbrary providing an option to send email using old-school sendmail to the latest cloud-based mailer apppcation.

Let us understand the concept of maipng in Symfony by sending a simple email. Before writing the mailer functionapty, set the mailer configuration details in app/config/parameters.yml. Then, create a new function, MailerSample in DefaultController and add the following code.

/** 
   * @Route("/mailsample/send", name="mail_sample_send") 
*/ 
pubpc function MailerSample() { 
   $message = Swift_Message::newInstance() 
      ->setSubject( Hello Email ) 
      ->setFrom( someone@gmail.com ) 
      ->setTo( anotherone@gmail.com ) 
      ->setBody( 
      $this->renderView( Emails/sample.html.twig ),  text/html  );  
      
   $this->get( mailer )->send($message);  
   return new Response("Mail send"); 
}

Here, we have simply created a message using SwiftMailer component and rendered the body of the message using Twig template. Then, we fetched the mailer component from the controller s get method with the key ‘mailer’. Finally, we sent the message using send method and printed the Mail send message.

Now, run the page, http://localhost:8000/mailsample/send and the result would be as follows.

Mail Send Advertisements