skip to Main Content

I am trying to send Mail through PHP Pear Mail package, I have 2 scenarios here , In first scenario mail is sending properly without any error but in second scenario I am getting error message .

Scenario 1 :

File Name : testmail.php

<?php
 require_once "Mail.php";

 $from = "[email protected]";
 $to = "[email protected]";
 $subject = "Landing Page Enquiry From -";
 $body = "Hello just testing";

 $host = "ssl://mail.askspidy.com";
 $port = "465";
 $username = "[email protected]";
 $password = "askspidy@1234";

 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject,
   'MIME-Version' => 1,
    'Content-type' => 'text/html;charset=iso-8859-1'
   );

 $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>");
  }
 ?>

If I directly run file in url then mail is sent without any errors .

Scenario 2 :
File Name : sendmail.php

<?php


    function send_mail($subject,$body)
    {
         require_once "Mail.php";

         $from = "[email protected]";
         $to = "[email protected]";

         $host = "ssl://mail.askspidy.com";
         $port = "465";
         $username = "[email protected]";
         $password = "askspidy@1234";

         $headers = array ('From' => $from,
           'To' => $to,
           'Subject' => $subject,
           'MIME-Version' => 1,
            'Content-type' => 'text/html;charset=iso-8859-1'
           );

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


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

    }

?>

Now i am calling this send_mail function from different file like this

File Name : service/process.php

<?php

require_once("../sendmail.php");

$subject="Landing page enquiry";
$email_body="Hello Just Testing! ";

send_mail($subject,$email_body);

?>

When this file is executed into browser , i am getting error message on line

send_mail($subject,$email_body);

Error :

Warning: include_once(Net/SMTP.php): failed to open stream: No such file or directory in /usr/local/lib/php/Mail/smtp.php on line 348

Warning: include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /usr/local/lib/php/Mail/smtp.php on line 348

Fatal error: Class 'Net_SMTP' not found in /usr/local/lib/php/Mail/smtp.php on line 349

In Scenario 1 everything is working fine then why in scenario 2 I am getting this error, i guess there is problem with path but i am not sure what should be the path and where should i include that.

Folder Structure :

enter image description here

Include code in file Mail/smtp.php

 require_once "Net/SMTP.php";

Note: I have manually installed PEAR Package in Cpanel account and done no settings in php.ini file

2

Answers


  1. Chosen as BEST ANSWER

    Added dirname(FILE) to the path name everywhere in code and it worked

    require_once dirname(__FILE__)."/Net/SMTP.php";
    

  2. You can use __DIR__ for the directory path for current file

    require_once __DIR__ . "/Net/SMTP.php";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search