Sending emails in local development environment with Swiftmailer


/ Published in: PHP
Save to your folder(s)

This is a little snippet to prevent sending emails to customers, in a local development environment, with Swiftmailer.
I didn't find any documentation about the RedirectPlugin, so I made this example.
Please refer to the official documentation to install Swiftmailer.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $local = true;
  4.  
  5. // Create the Mailer using any Transport
  6. $mailer = Swift_Mailer::newInstance(
  7. Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  8. );
  9.  
  10. // Create the Message
  11. $message = Swift_Message::newInstance()
  12. ->setSubject('My subject')
  13. ->setFrom(array('[email protected]' => 'John Doe'))
  14. ->setTo('[email protected]')
  15. ->setCc('[email protected]')
  16. ->setBcc('[email protected]')
  17. ->setBody('My content');
  18.  
  19. if( $local ) {
  20. // Register the RedirectPlugin, only in local development environment
  21. $mailer->registerPlugin(new Swift_Plugins_RedirectingPlugin('[email protected]'));
  22. // and then, no email will be sent to [email protected] or [email protected] or [email protected]
  23. }
  24.  
  25. // Send the Message
  26. $mailer->send($message);

URL: http://swiftmailer.org/docs/introduction.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.