How can PHP developers ensure proper mail delivery when testing locally?

When testing PHP scripts that send emails locally, developers can use a tool like Mailtrap to capture and view the outgoing emails instead of sending them to real recipients. This ensures that the emails are delivered and formatted correctly without actually being sent to real email addresses.

// Example code snippet using Mailtrap for testing email delivery locally
$transport = (new Swift_SmtpTransport('smtp.mailtrap.io', 2525))
  ->setUsername('your_username')
  ->setPassword('your_password');

$mailer = new Swift_Mailer($transport);

$message = (new Swift_Message('Test Email'))
  ->setFrom(['your_email@example.com' => 'Your Name'])
  ->setTo(['recipient@example.com' => 'Recipient Name'])
  ->setBody('This is a test email sent using Mailtrap.');

$result = $mailer->send($message);