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?