Search our database

Sending Email With Pear::Mail

Add the function below to PHP mailing script to enable SMTP authentication.

<?php
 
include("Mail.php");
 
$recipients = "recipient@domain.com";
 
$headers["From"] = "sender@domain.com";
$headers["To"] = $recipients;
$headers["Subject"] = "Your subject";
 
$body = "Hello, This is a test.";
 
$smtpinfo["host"] = "mail.domain.com";
$smtpinfo["port"] = "2525";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "sender@domain.com";
$smtpinfo["password"] = "Sender Email Password";
 
$mailer =& Mail::factory("smtp", $smtpinfo);
 
$mailer->send($recipients, $headers, $body);
 
if (PEAR::isError($mailer)) {
 
  echo("<p>" . $mailer->getMessage() . "</p>");
 
} else {
 
  echo("<p>Message successfully sent!</p>");
 
}
 
?>

Please make sure that Pear Mail has installed on your server. For CentOS, ssh to the server and it can be install with the command below,

pear install Mail
pear install Net_SMTP

Tags: ,