WEB HOSTING Sending mail with PHP using SMTP-auth
Sending mail with PHP using SMTP-auth Article ID:394

To protect 3Essentials network, servers, and customers from spamming, 3Essentials requires the use of SMTP-auth for all methods of sending email.  The basic PHP Mail function does not support SMTP-auth, therefore cannot be used. 

To find out more about SMTP-auth and enabling smtp-auth on PHP based open sourced solutions (like phpBB, OScommerce, etc), see this article: What is SMTP-AUTH?

To write your own PHP code to send email using SMTP-auth, we've made the PHP PEAR Mail package available for usage (which does support smtp-auth).  The following PHP code sample shows how to use this feature in your PHP code to send an email:

<?php
require_once "Mail.php";

$from = "Your Name <you@yourdomain.com>";
$to = "John Smith <someone@hisdomain.com>";
$subject = "This is my subject";
$body = "Hi,\n\nHow are you?";

$host = "localhost";
$username = "you@yourdomain.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>");
 }
?>

Be sure to change the applicable values (from, to, subject, body, username, password) to your own. This code will work with either PHP 4 or PHP 5.

  • Please do not name the file as Mail.php as we're giving reference to the Mail.php in the code which is located in the server.  Since this is essential for the code to work, you can name the file as mailer.php or mailsend.php for this code.  
  • If  you happen to get the following error, then add a line in the php.ini file found under your HTTPDOCS:
    • Warning: main(Mail.php) [function.main]: failed to open stream: No such file or directory in E:\inetpub\vhosts\domainname\httpdocs\filename.php on line 2

      Fatal error: main() [function.require]: Failed opening required 'Mail.php' (include_path='.;./includes;./pear') in E:\inetpub\vhosts\domainname.com\httpdocs\filename.php on line 2
    • The line to be added in php.ini is:
    • For WEB16: include_path=".;E:\Program Files\SWsoft\Plesk\Additional\Php\PEAR\;./includes;./pear"
      For WEB20: include_path=".;F:\Program Files\SWsoft\Plesk\Additional\Php\pear;./includes;./pear"
      For WEB22: include_path=".;E:\Program Files\Parallels\Plesk\Additional\Php\pear;./includes;./pear"
    • For WEB24: include_path=".;E:\Program Files\Parallels\Plesk\Additional\Php\pear;./includes;./pear"
    • For WEB9: include_path = ".:/usr/lib/php:/usr/local/lib/php:/usr/local/cpanel/3rdparty/lib/php"
Downloads associated with this article:
  No downloads associated with this article
Back to Search or Browse