Laravel:不会'发送电子邮件

时间:2016-04-01 18:17:44

标签: laravel swiftmailer

在Laravel中使用Mail功能时,我收到如下错误。

我先尝试过.env文件中的所有数据,然后从config / mail.php中获取数据

然后在mail.php中对其进行硬编码,以防它从那里得到它。

我也尝试了不同的端口,465是预期的端口,但也是587和25但是没有变化。

我也尝试过不同的smtp服务器,我在其他网站上尝试了不同的(哪些工作)邮件。所以这不是我的服务器,也不是那样的。

我用它来注册用户,然后发送确认邮件点击。他们被保存在表格中,但在发送邮件时,它不会出去。

我还尝试在控制器文件中硬编码To:地址,以防它从$ data数组中获取它。没有变化。

Mail::send('auth.emails.register', ['data' => $data ], function($mail) use($data) {        $mail->from('a@chefpi.com', 'Your Application');
                $mail->subject('Confirm your mail');
                $mail->to($data['email'], $data['name']);
                });                
            return redirect("auth/register")
            ->with("message", "A confirmation link has been sent to your email");           
        }

您可以在错误显示中看到它确实获取了To地址和Confirm_token(它们也保存在表中)

错误读取如下:

 Swift_TransportException in AbstractSmtpTransport.php line 404:
    Connection to server.websitehostserver.net:465 Timed Out
    in AbstractSmtpTransport.php line 404
    at Swift_Transport_AbstractSmtpTransport->_getFullResponse('0') in AbstractSmtpTransport.php line 289
    at Swift_Transport_AbstractSmtpTransport->_readGreeting() in 

        AbstractSmtpTransport.php line 117
        at Swift_Transport_AbstractSmtpTransport->start() in Mailer.php line 79
        at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php line 386
        at Mailer->sendSwiftMessage(object(Swift_Message)) in Mailer.php line 173
        at Mailer->send('auth.emails.register', array('data' => array('name' => 'aaaaa', 'email' => 'madina@yandex.ru', 'confirm_token' => 'x5nSRdqgPApFWUvwlNcMLmwvMQ1T64IKpfX0IHjqm7zjwfDYEhCSGPixJyNky2XU5')), object(Closure)) in compiled.php line 6222
        at Facade::__callStatic('send', array('auth.emails.register', array('data' => array('name' => 'aaaaa', 'email' => 'madina@yandex.ru', 'confirm_token' => 'x5nSRdqgPApFWUvwKtwdzwT64IKpfX0IHjqm7zjwfDYEhCSGPixJyNky2XU5')), object(Closure))) in AuthController.php line 84
        at Mail::send('auth.emails.register', array('data' => array('name' => 'aaaaa', 'email' => 'mayarodina@yandex.ru', 'confirm_token' => 'x5nSRdqgPApFbHWqszKtwdzwT64IKpfX0IHjqm7zjwfDYEhCSGPixJyNky2XU5')), object(Closure)) 

    in AuthController.php line 84

我正在添加完整控制器,以防万一:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Mail;
use Auth;
use Illuminate\Support\Facades\Input;

class AuthController extends Controller
{
    /*

    use AuthenticatesAndRegistersUsers;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    public function postRegister(Request $request){

        $rules = [
            'name' => 'required|min:3|max:16|regex:/^[a-záéíóúàèìòùäëïöüñ\s]+$/i',
            'email' => 'required|email|max:255|unique:users,email',
            'password' => 'required|min:6|max:18|confirmed',
        ];

        $messages = [
            'name.required' => 'This field is required',

        ];

        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()){
            return redirect("auth/register")
            ->withErrors($validator)
            ->withInput();
        }
        else{
            $user = new User;
            $data['name'] = $user->name = $request->name;
            $data['email'] = $user->email = $request->email;
            $user->password = bcrypt($request->password);
            $user->remember_token = str_random(100);
            $data['confirm_token'] = $user->confirm_token = str_random(100);
            $user->save();

            Mail::send('auth.emails.register', ['data' => $data ], function($mail) use($data) {

                $mail->from('a@chefpisos.com', 'Your Application');
                $mail->subject('Confirm your mail');
                $mail->to($data['email'], $data['name']);

            });

            return redirect("auth/register")
            ->with("message", "A confirmation link has been sent to your email");



        }


    }

    public function confirmRegister($email, $confirm_token)
    {


    $user = new User;

    $the_user = $user->select()->where('email', '=', $email)
                               ->where('confirm_token', '=', $confirm_token)
                               ->get();

    if (count($the_user) > 0)
    {
       $active = 1;
       $confirm_token = str_random(100);
       $user ->where('email', '=', $email)
       ->update(['active' => $active, 'confirm_token' => $confirm_token]);
       return redirect('auth/register')
       ->with("message", 'Congrats'. $the_user[0]['name'] . 'account activated');
    }

    else{

        return redirect('/');
    }

    }

    public function postLogin(Request $request){







       if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password'), 'active' => 1])) {

             return redirect()->intended($this->redirectPath());   

            }

            else{
                $rules = [
                    'email' => 'required|email',
                    'password' => 'required',
                ];

                $messages = [
                    'email.required' => 'El campo email es requerido',
                    'email.email' => 'El formato de email es incorrecto',
                    'password.required' => 'El campo password es requerido',
                ];

                $validator = Validator::make($request->all(), $rules, $messages);

                return redirect('auth/login')
                ->withErrors($validator)
                ->withInput()
                ->with('message', 'Error al iniciar sesión');
    }


    }









}

1 个答案:

答案 0 :(得分:0)

反对我的网络托管服务提供商声明我必须使用465,我去了587而且我不知道为什么,但这次它起作用。

奇怪的是,我在同一台服务器上托管了另一个网站,我在那里使用了465没有任何问题。

然而,它不会通过我的其他网络中的那个端口出去。我永远不会知道为什么。

我已经在互联网上阅读了所有相似的案例,所有这些案例确实都有端口465的问题,最后使用另一个。

如果您遇到类似的问题,只需复制上面的代码,它就完整且有效。

感谢所有看过这个问题的人。