Sending authenticated mail with PHP

Thu, Sep 3, 2009

General

Over the past few years, I have seen many hosting companies greatly improve their security from a mail sending perspective. The technique which I see used most commonly; and seems to cause the most problems is the host preventing the “nobody” user from sending mail to remote addresses. This tends to cause issues with users trying to use PHP’s native mail() function. My solution is a simple and fairly well know solution, but I am not aware of anywhere that describes exactly how to do it. My aim is to explain it in simple steps so anyone can use this method.

There are three main stages to getting it working

  1. Check that the required PEAR modules are installed – Just ask your server admin / host if the following modules are installed
    • PEAR::Mail
    • PEAR::Net_Socket
    • PEAR::Net_SMTP
  2. Create an email account on the server that you want to use (make sure that you remember the username and passowrd as you will need to enter them in the next step!
  3. Put the following script on your server and tweak it to your needs using the server details and the details of the account that you setup in the previous step. The variables that you will need to edit are:
    • $from – The address that the mail will come from
    • $to – The email address that you would like the mail to go to
    • $subject – The subject of the message that you are sending
    • $body – The body of the message that you are sending (the main content of the message)
    • $host – The hostname of the mailserver that you want to use (typically mail.yourdomain.com on cPanel servers)
    • $username – The username that you would like to authenticate with (usually the same as the from address)
    • $password – The password that matches the username
    <?php
     
    // Include PEAR::Mail
    require_once "Mail.php";
     
    // The email address that you are sending the 
    // email from - Should be the email address
    // of the account that you will be authenticating
    // with.
    $from = "Your Name <your.email.address@your-domain.com>";
     
    // The intended recipients
    $to = "Their Name <someone@their-domain.com>";
     
    // The message subject
    $subject = "Test message";
     
    // The message body
    $body = "Hi,\n\nThis is your test message!\n\n :-)";
     
    // The mail server which you will use to send
    // the message.
    $host = "mail.your-server.com";
     
    // The username for the email account you are 
    // authenticating with. 
    $username = "your.email.address@your-domain.com";
     
    // The password for the email account you are 
    // authenticating with.
    $password = "YourPassword";
     
    // The message headers
    $headers = array(
     'From' => $from,
     'To' => $to,
     'Subject' => $subject
     );
     
    // Create a mailer instance
    $smtp = Mail::factory(
     'smtp',
     array(    'host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password
     )
     );
     
    // Attempt to send the message
    $mail = $smtp->send($to, $headers, $body);
     
    // Check if the mail was sent successfully
    if (PEAR::isError($mail)) {
     // The message was NOT sent successfully. This 
     // outputs reason why the message sending failed
     echo("<p>" . $mail->getMessage() . "</p>");
    }else{
     // The message was sent successfully
     echo("<p>Message successfully sent!</p>");
    }
     
    ?>

Once you have completed the three steps, simply navigate to the location of the php script in your web browser. You should see a message saying “Message successfully sent!”. If you see an error like

Fatal error: Class ‘Net_SMTP’ not found in /usr/local/lib/php/Mail/smtp.php on line 210

it means that one of the required modules is not installed. In this case “Net_SMTP”, to fix this you will need to get your admin / host to install it for you!

If you need any help, please leave a comment and I will get back to you!

1 Comments For This Post

  1. teddie Says:

    you just found a new daily reader

Leave a Reply

View in: Mobile | Standard