Laravel使用CPanel与共享主机发送电子邮件-失败

时间:2018-08-03 19:06:08

标签: php laravel email cpanel shared

我需要向所有在我的应用程序中注册的用户发送验证邮件。我的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

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

RegisterController.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!');
    }


}

这是VerifyMail

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文件,也许是控制器造成了问题?

0 个答案:

没有答案