What are some alternatives to PHP mailer for sending emails in PHP?

Using alternative libraries such as Swift Mailer or PHPMailer can provide more features and better security compared to PHP mailer for sending emails in PHP.

// Using Swift Mailer library
require_once 'path/to/swift-mailer/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 25)
  ->setUsername('your_username')
  ->setPassword('your_password');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Subject')
  ->setFrom(array('sender@example.com' => 'Sender Name'))
  ->setTo(array('recipient@example.com' => 'Recipient Name'))
  ->setBody('Message body');

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

if ($result) {
  echo 'Email sent successfully';
} else {
  echo 'Failed to send email';
}