PHP Mail With PEAR

Recommended Way to Send Email Using PHP and PEAR on Windows Servers

When running PHP on Windows servers, the most reliable way to send email is by using the PEAR Mail package with SMTP authentication. This method ensures proper delivery, avoids local mail restrictions, and works with all modern mail servers.

Below is a recommended example showing how to send an email using PEAR Mail with SMTP authentication.


PHP + PEAR SMTP Example
<?php

require_once "Mail.php";

$from     = "Your Name <you@your-domain.com>";
$to       = "Recipient Name <recipient@your-domain.com>";
$subject  = "Test Email";
$body     = "Hi,\n\nThis is a test email sent using PEAR Mail.";

$host     = "mail.YOUR-DOMAIN.COM";
$username = "you@your-domain.com";
$password = "YOUR-EMAIL-PASSWORD";

$headers = array(
    'From'    => $from,
    'To'      => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
    'host'     => $host,
    'auth'     => true,
    'username' => $username,
    'password' => $password
));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo "<p>" . $mail->getMessage() . "</p>";
} else {
    echo "<p>Message successfully sent!</p>";
}

?>
        

Replace the SMTP host, username, and password with your actual email account details.


Important Notes

  • Use your full email address as the SMTP username.
  • Ensure your SMTP hostname is correct (e.g., mail.your-domain.com).
  • PEAR Mail must be installed on your hosting environment.
  • For improved security, use port 587 with TLS if supported by your mail provider.

PEAR Mail remains a reliable option for legacy PHP applications, especially on Windows servers where built‑in mail functions may be restricted.


Learn More About Fast2host

Fast2host has been providing reliable UK hosting, domains, cloud servers and business email since 2002. Find out more about who we are:

About Fast2host →
Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 9061

Need more information or have a question ?