Test SMTP on PHP

This is an SMTP sample code based on the PEAR mailing package which is common on most web hosts. The code aims to test/debug mail sending/reception.

<?php
/* SMTP Diagnostic script | PHP 5.6 | PEAR Package: Mail */
error_reporting(E_ALL);
require_once "Mail.php"; // A predefined system package

$headers = array(
'From' => "user@example.com",
'To' => "user@example.com",
'Subject' => "SMTP TEST",
'Reply-To' => "user@example.com",
);

$smtp = Mail::factory('smtp', array(
'host' => "mail.example.com", // For SSL/TLS, prefix it with ssl://mail.example.com
'port' => "26", //26 or 465 for the SSL-based port
'auth' => TRUE,
'username' => "user@example.com",
'password' => "XXXXXXXX",
'debug' => TRUE
));

$mail = $smtp->send(
$headers['To'],
$headers,
"Hello, This is a test message."
);

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *