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.

We strongly recommend using an SMTP relay that requires authentication. Sending mail through unauthenticated SMTP servers (including the localhost relay on Cloud Sites) can result in delays or undelivered email due to stringent anti-spam filters.

Sending with PHP SMTP

You will only need to change the following variables:

  • $from
  • $to
  • $subject
  • $body
  • $host
  • $username
  • $password

The host, username, and password values will depend on the provider you're using to send your email.  If you have a legacy Sites email account or are using Rackspace Email the host would be "mail.emailsrvr.com" and the username would be your email address.  If you're using another service such as Mailgun or Gmail to send email, you'll need to fill in the SMTP server for that service. Mailgun server information can be accessed via your Mailgun control panel.

<?php
require_once "Mail.php";
 
$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";
 
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$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
  • $host
  • $username
  • $password
 
<?php
require_once "Mail.php";
 
$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$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 = "webmaster@example.com";
$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>");
}
?>

Note: Mail.php is a PEAR module and is installed on the server. It is included in the default include_path for PHP, so requiring it here will work by default without any additional effort on your part.

See Also



Was this content helpful?




© 2014 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER