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
<?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.