FatalThrowableError in Laravel Php mailer on live server

时间:2018-01-11 08:42:42

标签: php laravel server host mailer

My laravel project php mailer function is working in local host. But in the live server it's not working: Error: FatalThrowableError in RegisterController.php line 75: Class 'App\CustomClass\CMailer' not found Here is my controller file:

enter code here
<?php

namespace App\Http\Controllers\Auth;

use App\CustomClass\CMailer;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{


use RegistersUsers;

    public function __construct()
    {
        $this->middleware('guest');
    }

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:4|confirmed',
        ]);
    }

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array $data
 * @return User
 */
        protected function create(array $data)
        {

            $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
        if ($user) {
            $mailException = null;
            $view = view('auth.verification', compact('user'))->render();
            try {
            $mail = CMailer::send('Prapti', $data['email'], 'Email verification', $view);
            } catch (Exception $e) {
                $mailException = true;
            }

            }

    return $user;
    }
}

enter code here

My class file:

enter code here

namespace App\CustomClass;


use PHPMailer\PHPMailer\PHPMailer;

class CMailer
{
    protected static $host = "smtp.gmail.com";
    protected static $port = 468;
    protected static $encryption = 'tls';
    protected static $username = "******";
    protected static $password = '***************';
    protected static $from = '**************@gmail.com';

/**
 * Sending email by PHPMailer
 *
 * @param  string $fromName
 * @param  string $to
 * @param  string $subject
 * @param  string $message
 * @return TRUE
 */
    public static function send($fromName, $to, $subject, $message)
    {
    $mail = new PHPMailer(true);
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;
    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';
    //Set the hostname of the mail server
    $mail->Host = self::$host;
    // $mail->Host = gethostbyname('smtp.gmail.com');
    // if your network does not support SMTP over IPv6
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = self::$port;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = self::$encryption;
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication - use full email address for 
     gmail
    $mail->Username = self::$username;
    //Password to use for SMTP authentication
    $mail->Password = self::$password;
    //Set who the message is to be sent from: this is sender email
    $mail->setFrom(self::$from, $fromName);
    //Set who the message is to be sent to
    $mail->addAddress($to, 'Test Service');
    //Set the subject line
    $mail->Subject = $subject;
    $mail->msgHTML($message);
    if (!$mail->send()) {
        // return $mail;
        return false;
    } else {
        return true;
    }
    }
}

** Php mailer and class is okay. I've tested by changing port but same problem. and not working in hosting. Where is the problem? Please suggest me the solution.

2 个答案:

答案 0 :(得分:1)

您需要运行composer dump-autoload来重新加载所有缺失的类。

答案 1 :(得分:0)

If the same code works on your local machine, you need to run composer du command on the live server and include this command to your deployment script.