douzhen5158 2018-08-03 19:06
浏览 72

Laravel使用cpanel发送带共享主机的电子邮件 - 失败

I need to send verification mail to any user who registers on my app. My Laravel version is 5.4.36. I am using shared hosting with cpanel.

So I made an email account through cpanel which should send emails. Here are cpanel's recommended settings:

Username: rutko@mladensblogger.rs Password: Use the email account’s password. Incoming Server: mail.mladensblogger.rs

IMAP Port: 993 POP3 Port: 995

Outgoing Server: mail.mladensblogger.rs

SMTP Port: 465

Here is my .env:

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

Everything looks fine to me, here is the controller where mail sending is needed:

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


}

Here is the 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');
    }
}

In my view which is returned after storing the user, if everything passes correctly, a success is shown, othervise, error is shown. Basically mail won't send and view shows error, but when I comment the Mail::toline, a success is shown. I have no ideas, and have tried multiple ways to modify the .env file, maybe the controller is making the problem?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突