Tag Archives: Function

How do I test PHP SMTP functionality?

You can test PHP SMTP functions with the following two examples. The first one is standard SMTP while the second one is SMTP with SSL.

Sending with PHP SMTP

You will only need to change the following variables:

  • $from
  • $to
  • $subject
  • $body
  • $username
  • $password
<?php
require_once "Mail.php";

$from = "Web Master <webmas[email protected]>";
$to = "Nobody <[email protected]>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";

$host = "mail.emailsrvr.com";
$username = "[email protected]";
$password = "yourPassword";

$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>");
}
?>

Sending with PHP SMTP with SSL

You will only need to change the following variables:

  • $from
  • $to
  • $subject
  • $body
  • $username
  • $password
<?php
require_once "Mail.php";

$from = "Web Master <[email protected]>";
$to = "Nobody <[email protected]>";
$subject = "Test email using PHP SMTP with SSL\r\n\r\n";
$body = "This is a test email message";

$host = "ssl://secure.emailsrvr.com";
$port = "465";
$username = "[email protected]";
$password = "yourPassword";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    '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>");
}
?>