PEAR Mail is a PHP library that provides SMTP-authenticated email sending — useful for legacy applications or servers without a local MTA. However, for modern PHP projects, PHPMailer or Symfony Mailer via Composer are the recommended alternatives. This guide covers both PEAR Mail (for legacy systems) and the modern approach.
Option 1: PEAR Mail (Legacy Systems)
Install PEAR
# AlmaLinux / Rocky Linux / CentOS
dnf install php-pear -y
# Debian / Ubuntu
apt install php-pear -y
Install PEAR Mail Packages
pear install mail
pear install Net_SMTP
pear install Auth_SASL
pear install mail_mime
Restart Web Server
# Apache
systemctl restart httpd # RHEL/CentOS
systemctl restart apache2 # Debian/Ubuntu
Basic PEAR Mail Usage
'smtp.yourdomain.com','port'=>587,'auth'=>true,'username'=>'user@domain.com','password'=>'secret'];
$mail = Mail::factory('smtp', $params);
$headers = ['From'=>'user@domain.com','To'=>'recipient@example.com','Subject'=>'Test'];
$mail->send('recipient@example.com', $headers, 'Hello from PEAR Mail!');
Option 2: PHPMailer via Composer (Recommended for New Projects)
composer require phpmailer/phpmailer
isSMTP();
$mail->Host = 'smtp.yourdomain.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@domain.com';
$mail->Password = 'secret';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('user@domain.com');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Test';
$mail->Body = 'Hello!';
$mail->send();
