0

I have two php files and I already created a folder called "ExternalClasses" and added the file to that folder. In my php file, I add this line:

namespace App\ExternalClasses;

and in my controller I add this line:

use App\ExternalClasses\CCheckMail;

and this is how I use it:

$pricesClass = new CCheckMail(); 
$email2 = ['myemail@gmail.com']; 
$prices = $pricesClass->execute ($email2); 
return view('pages.home', compact('prices'));

but it gave me an error:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'App\ExternalClasses\CCheckMail' not found

Here is my php file (CCheckMail.php):-

<?php 
namespace App\ExternalClasses;
/*
*   This script was writed by Setec Astronomy - setec@freemail.it
*
*   This script is distributed  under the GPL License
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   http://www.gnu.org/licenses/gpl.txt
*
*/
define ('DEBUG_OK', false);
class CCheckMail
{
    var $timeout = 10;
    var $domain_rules = array ("aol.com", "bigfoot.com", "brain.net.pk", "breathemail.net",
                               "compuserve.com", "dialnet.co.uk", "glocksoft.com", "home.com",
                               "msn.com", "rocketmail.com", "uu.net", "yahoo.com", "yahoo.de");

    function _is_valid_email ($email = "") 
    { return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $email); }  

    function _check_domain_rules ($domain = "")
    { return in_array (strtolower ($domain), $this->domain_rules); }

    function execute ($email = "")
    {
        if (!$this->_is_valid_email ($email))
        { return false; }

        $host = substr (strstr ($email, '@'), 1);

        if ($this->_check_domain_rules ($host))
        { return false; }

        $host .= ".";

        if (getmxrr ($host, $mxhosts[0],  $mxhosts[1]) == true) 
        { array_multisort ($mxhosts[1],  $mxhosts[0]); }
        else
        {
            $mxhosts[0] = $host;
            $mxhosts[1] = 10;
        } 
        if (DEBUG_OK) { print_r ($mxhosts); }

        $port = 25;
        $localhost = $_SERVER['HTTP_HOST'];
        $sender = 'info@' . $localhost;

        $result = false;
        $id = 0;
        while (!$result && $id < count ($mxhosts[0]))
        {
            if (function_exists ("fsockopen"))
            {
                if (DEBUG_OK) { print_r ($id . " " . $mxhosts[0][$id]); }
                if ($connection = fsockopen ($mxhosts[0][$id], $port, $errno, $error, $this->timeout))
                {
                    fputs ($connection,"HELO $localhost\r\n"); // 250
                    $data = fgets ($connection,1024);
                    $response = substr ($data,0,1);
                    if (DEBUG_OK) { print_r ($data); }

                    if ($response == '2') // 200, 250 etc.
                    { 
                        fputs ($connection,"MAIL FROM:<$sender>\r\n");
                        $data = fgets($connection,1024);
                        $response = substr ($data,0,1);
                        if (DEBUG_OK) { print_r ($data); }

                        if ($response == '2') // 200, 250 etc.
                        { 
                            fputs ($connection,"RCPT TO:<$email>\r\n");
                            $data = fgets($connection,1024);
                            $response = substr ($data,0,1);
                            if (DEBUG_OK) { print_r ($data); }

                            if ($response == '2') // 200, 250 etc.
                            { 
                                fputs ($connection,"data\r\n");
                                $data = fgets($connection,1024);
                                $response = substr ($data,0,1);
                                if (DEBUG_OK) { print_r ($data); }

                                if ($response == '2') // 200, 250 etc.
                                { $result = true; }
                            }
                        }
                    }

                    fputs ($connection,"QUIT\r\n"); 
                    fclose ($connection);
                    if ($result) { return true; }
                }
            }
            else
            { break; } 
            $id++;
        }  
        return false;
    }
}
?>

The second php file:

<?php 
/*
*   This script was writed by Setec Astronomy - setec@freemail.it
*
*   On row 41 of CCheckMail.php substitute the following line
*
*   if (getmxrr ($host, $mxhosts[0],  $mxhosts[1]) == true) 
*
*   with
*
*   if (getmxrr_portable ($host, $mxhosts[0],  $mxhosts[1]) == true) 
*
*   to have a fully working portable (*nix and Windows) CCheckMail class
*
*   This script is distributed  under the GPL License
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   http://www.gnu.org/licenses/gpl.txt
*
*/
function getmxrr_win ($hostname = "", &$mxhosts, &$weight)
{
 $weight = array();
 $mxhosts = array();
 $result = false;


 $command = "nslookup -type=mx " . escapeshellarg ($hostname);
 exec ($command, $result);
 $i = 0;
 while (list ($key, $value) = each ($result)) 
 {
    if (strstr ($value, "mail exchanger")) 
    { $nslookup[$i] = $value; $i++; }
 }

 while (list ($key, $value) = each ($nslookup)) 
 {
    $temp = explode ( " ", $value );
    $mx[$key][0] = substr($temp[3],0,-1);
    $mx[$key][1] = $temp[7];
    $mx[$key][2] = gethostbyname ( $temp[7] );
 }

 array_multisort ($mx);

 foreach ($mx as $value) 
 { 
  $mxhosts[] = $value[1];
  $weight[] = $value[0];
 } 

 $result = count ($mxhosts) > 0;
 return $result;
}

function getmxrr_portable ($hostname = "", &$mxhosts, &$weight)
{
 if (function_exists ("getmxrr"))
 { $result = getmxrr ($hostname, $mxhosts, $weight); }
 else
 { $result = getmxrr_win ($hostname, $mxhosts, $weight); }
 return $result; 
}
?>

enter image description here

Miss. Nada
  • 321
  • 1
  • 4
  • 14
  • Can you add the file tree structure too? We need to check your folder hierarchy and naming to ensure that the file is in the right place. We don't need to see so much code. Just file system, name spaces, and class name will do. – Tarek Adam Sep 20 '18 at 12:59
  • I update the question with project structure – Miss. Nada Sep 20 '18 at 13:02
  • Hmm... the PSR4 is pretty specific about naming. What's up with getmxrr.php? That's not psr4 and should not be in that area of the file system. I can't say that's your problem, but it's not gonna help ~ especially when you're trying to `composer dump-auto` and seeing your project blow up. – Tarek Adam Sep 20 '18 at 13:09
  • Functions declared in `getmxrr.php` are not used in `CCheckMail` class, so that file is not relevant to the question. – d3jn Sep 20 '18 at 13:12
  • PSR4 is relevant to your project. If you're having symphony autoload() problems ... you'll want to be following PSR4 in all the directories/* listed in your composer.json file. So ... just humour me and fix it. – Tarek Adam Sep 20 '18 at 13:15
  • FYI the standard way of adding a helper library of functions to make an `abstract` class in the app namespace, then call the functions statically like this `\App\Helpers::myFunk(...)` – Tarek Adam Sep 20 '18 at 13:17
  • so how can I fix it? – Miss. Nada Sep 20 '18 at 13:21
  • @TarekAdam Firstly, I am not the author of this question. Secondly, that file can be whatever, it won't do anything. I suggest you read on autoload in PHP in general before trying to answer questions. The problem here is definitely a typo somewhere. It can be even a trailing space in folder name or something sneaky as that. – d3jn Sep 20 '18 at 13:24
  • actually, getmxrr.php and CCheckMail.php are downloaded from internet. Have you tried to include it in your project and check if it's working? may it will help me to identify problem. – Miss. Nada Sep 20 '18 at 13:27
  • @d3jn I agree typo is the most likely. So stay calm and code on. – Tarek Adam Sep 20 '18 at 13:29
  • @Miss.Nada yes, works on clean Laravel 5.6 project. – d3jn Sep 20 '18 at 13:31
  • @d3jn has put me off trying to be helpful. I'm out. Good luck with it. Miss Nada. – Tarek Adam Sep 20 '18 at 13:34
  • You didn't by chance set the namespace of your app to something else besides `App`, for example by using the command `php artisan app:name`? – user1669496 Sep 20 '18 at 14:14
  • I think it was type issue, now I am getting this error fsockopen(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known. Is because I am using XAMPP? – Miss. Nada Sep 20 '18 at 15:56

2 Answers2

1

Just type composer dumpautoload -o in your terminal.

TheGeeky
  • 962
  • 1
  • 12
  • 34
  • I use these commands and now my whole project is not working!! $ php artisan clear-compiled $ composer dump-autoload $ php artisan optimize – Miss. Nada Sep 20 '18 at 13:04
  • this command autoloads your classes in your app dir and your new classes should be readable right now .. so if you have another error it's not from this command bro ?!! – TheGeeky Sep 20 '18 at 13:18
  • This is the error I got after running the commands: Fatal error: Uncaught RuntimeException: A facade root has not been set. in /Users/Nada/Desktop/nadalaravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:220 Stack trace: #0 /Users/Nada/Desktop/nadalaravel/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(397): – Miss. Nada Sep 20 '18 at 13:25
  • What is the inside bootstrap/app.php is look like that `$app = new Illuminate\Foundation\Application( realpath(__DIR__ . '/../') );` – TheGeeky Sep 20 '18 at 13:35
0

It was typing error, I created a new project and redo my coding and it's working fine.

Miss. Nada
  • 321
  • 1
  • 4
  • 14