Send mail using Mail.php / PEAR

Overview

To send mail using Mail.php, two things need to be uploaded to the site: the PEAR library itself, and a .user.ini that tells PHP where to find it. PEAR is not on the include path by default, so without the .user.ini the require_once "Mail.php" line will fail.

The package and setup instructions are below.

Step 1: Download the package

Download elPEAR.zip.

Inside the .zip is:

  • the PEAR directory;
  • a .user.ini;
  • a test script named zsend.php;
  • a script to show the server path to /PEAR, named zpath.php.

The PEAR directory also contains a web.config to prevent browser access to the subdirectory.

Step 2: Upload it

To use Mail.php, upload the /PEAR directory and the .user.ini to the document root of the site (/site/wwwroot/).

If you already have a .user.ini, do not overwrite it. Update your existing one with the include_path to /PEAR instead.

Step 3: Test Mail.php

zsend.php may be used to test whether Mail.php has been properly set up. Open the file in a text editor and update the settings shown as placeholders below.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once "Mail.php";

$from = "[email protected]";
$to = "[email protected]";
$subject = "Mail.php test email";
$body = "Test email sent using Mail.php.";

$host = "sm##.internetmailserver.net";
$username = "[email protected]";
$password = "password";

$headers = array ('Date' => date('r'),
    'From' => $from,
    'To' => $to,
    'Subject' => $subject,
    'Message-ID' => '<' . time() . md5($from . $to) . '@domain.com>');

$smtp = Mail::factory('smtp',
    array ('localhost' => $host,
        'host' => $host,
        'auth' => true,
        'username' => $username,
        'password' => $password));

$mail = $smtp->send($to, $headers, $body);

echo "Mail.php test"
?>

Replace the from address, the recipient address, the mail server host name, the user name and the password with your own values. Your mail server host name is shown in your Everleap mail settings; see Email Client Settings.

Once updated, save the changes and upload the script to the site.

Verify it worked

View the page in a browser. You should see Mail.php test returned, and the message should arrive at the recipient address.

If the email is not received, review the path to PEAR in the .user.ini and the zsend.php settings. For the path, see the next section.

Checking the path to PEAR

The zip includes zpath.php, which shows the server path to /PEAR. Upload it alongside the other files and open it in a browser. Use the path it reports to confirm that the include_path in your .user.ini points at the PEAR directory you actually uploaded.

Remember that a .user.ini does not take effect until the site is recycled. See Reviewing and updating PHP settings.

Remove the test scripts when you are done

To prevent abuse, zsend.php should be deleted from the site after testing. Left in place it is an open mail relay form with your own credentials in it. Delete zpath.php as well once you no longer need it.

Related Articles