0

我需要向在我的应用上注册的任何用户发送验证邮件。我的 Laravel 版本是 5.4.36。我正在使用带有 cpanel 的共享主机。

所以我通过 cpanel 创建了一个电子邮件帐户,它应该发送电子邮件。以下是 cpanel 的推荐设置:

用户名:rutko@mladensblogger.rs 密码:使用电子邮件帐户的密码。接收服务器:mail.mladensblogger.rs

IMAP 端口:993 POP3 端口:995

传出服务器:mail.mladensblogger.rs

SMTP 端口:465

这是我的 .env:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mladensblogger.rs
MAIL_PORT=465
MAIL_USERNAME=rutko@mladensblogger.rs
MAIL_PASSWORD=myaccountpass
MAIL_ENCRYPTION=tls

对我来说一切都很好,这是需要发送邮件的控制器:

注册控制器.php

public function store(Request $request)
{

    // ************************** -- RULES -- ***************************

    $rules = [
        'tbEmail' => 'required|email|unique:users,email',
        'tbUser' => 'required|alpha_num|unique:users,user',
        'tbPass' => 'required|min:6',
        'tbPassConfirm' => 'required|same:tbPass',
        'tbNameSurname' => 'required|min:3',
        'tbPhone' => 'required|regex:/^[0-9]*$/'
    ];

    // ************************** -- MESSAGES -- ***************************

    $messages = [
        'required' => 'Polje :attribute je obavezno',
        'same' => 'Ponovljena lozinka nije ista kao i lozinka',
        'regex' => ':attribute nije u dozvoljenom formatu',
        'email' => 'Email nije u dobrom formatu',
        'tbPhone.regex' => 'Broj telefona mora biti zapisan brojevima i ne sme sadržati slova',
        'tbUser.regex' => 'Korisničko ime mora početi malim slovom, i ne sme imati razmake',
        'tbNameSurname.min' => 'Ime nije u ispravnom formatu',
        'min' => ':attribute mora imati minimum :min karaktera',
        'unique' => ':attribute je već iskorišćen/a'
    ];

    $this->validate($request, $rules, $messages);

    $email = $request->get('tbEmail');
    $username = $request->get('tbUser');
    $pass = $request->get('tbPass');
    $passConfirm = $request->get('tbPassConfirm');
    $nameSurname = $request->get('tbNameSurname');
    $phone = $request->get('tbPhone');

    try{
        $user = new User(); // Model for saving the user
        $user->email = $email;
        $user->pass = $pass;
        $user->user = $username;
        $user->name_surname = $nameSurname;
        $user->phone = $phone;

        $result = $user->saveWithRegularUserRole();

        $verify_user = new VerifyUser();
        $result_for_id = $user->getByUserAndPass();
        $verify_user->user_id = $result_for_id->id;
        $verify_user->token = str_random(40);
        $verify_user->create();

        //Mail::to($user->email)->send(new VerifyMail($user));

        if($result == 1){
            return redirect()->route('index')->with('success', 'Uspešno ste se registrovali');}
        else{
            return redirect()->route('index')->with('error', 'Neuspeh pri registraciji, molimo pokušajte opet!');}
    }
    catch (\Exception $exception){
        \Log::error('Error: ' . $exception->getMessage());
        return redirect('/')->with('error', 'Greška pri radu aplikacije, molimo pokušajte ponovo!');
    }


}

这是验证邮件

class VerifyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.verifyUser');
    }
}

在我存储用户后返回的视图中,如果一切正常,则显示成功,否则显示错误。基本上邮件不会发送并且查看显示错误,但是当我评论 Mail::toline 时,会显示成功。我没有想法,并尝试了多种方法来修改 .env 文件,也许是控制器出了问题?

4

0 回答 0