I am sending one mail from laravel code. I want to add priority in it. But it is not showing. Below is my code. I am using laravel 6 version.
public function build()
{
if (! empty($this->attributes['replyTo'])) {
return $this->from($this->from)
->view($this->view)
->with($this->with)
->subject($this->subject)
->replyTo($this->attributes['replyTo']);
} elseif ($this->attributes) {
return $this->from($this->from['address'], $this->from['name'])
->view($this->view)
->with($this->with)
->subject($this->subject)
->priority($this->attributes);
} //elseif (! empty($this->with)) {
// return $this->from($this->from['address'], $this->from['name'])
// ->view($this->view)->with($this->with)
// ->subject($this->subject);
// }
return view($this->view);
}
Related
I have google OATH setup via socialite (only for within our organisation) and everything is working fine.
One thing I'd like to try and do is catch this "error" and get redirected back to our login page with a custom message telling the user that they do not belong to our organisation.
In principle this works fine, they can just hit the back button... but for fluidity and design, I'd like to catch this and redirect back to our home page.
Is this even possible? If so, how would you recommend I go about it?
public function show()
{
return view('auth.login');
}
public function redirectToProvider($driver)
{
if( ! $this->isProviderAllowed($driver) ) {
return $this->sendFailedResponse("{$driver} is not currently supported");
}
try {
return Socialite::driver($driver)->redirect();
} catch (Exception $e) {
return $this->sendFailedResponse($e->getMessage());
}
}
public function handleProviderCallback( $driver )
{
try {
$user = Socialite::driver($driver)->user();
} catch (Exception $e) {
return $this->sendFailedResponse($e->getMessage());
}
// check for email in returned user
return empty( $user->email )
? redirect()->intended('/login?failed=1')
: $this->loginOrCreateAccount($user, $driver);
}
protected function sendSuccessResponse()
{
return redirect()->intended('/');
}
protected function sendFailedResponse($msg = null)
{
return redirect()->intended('/login?failedResponse='.$msg);
}
protected function loginOrCreateAccount($providerUser, $driver)
{
// check for already has account
$user = User::where('email', $providerUser->getEmail())->first();
// if user
if( $user ) {
// update the avatar and provider that might have changed
$user->update([
'avatar' => $providerUser->avatar,
'provider' => $driver,
'provider_id' => $providerUser->id,
'access_token' => $providerUser->token
]);
} else {
return redirect()->intended('/login?noUser=1');
}
// login the user
Auth::login($user, true);
return $this->sendSuccessResponse();
}
private function isProviderAllowed($driver)
{
return in_array($driver, $this->providers) && config()->has("services.{$driver}");
}
I am creating login page & after logout my session value can not destroy. Any problem with code? I am using flush method,forget method to remove previous session value.
public function userLogin(Request $req)
{
$username=$req->input('username');
$password=$req->input('password');
$finduser = Users::where(['email'=>$username,'password'=>$password])
->orwhere(['mobile'=>$username,'password'=>$password])
->first();
Session::put('username', $finduser->name);
Session::put('userid', $finduser->id);
$session_id=Session::get('session_id');
if($username != $finduser->mobile and $username != $finduser->email)
{
Session::put('message','Email or mobile number does not exists');
return redirect::to('/login');
}
else if($password != $finduser->password)
{
Session::put('message','Your Password is incorrect');
return redirect::to('/login');
}
else if($finduser)
{
return redirect::to('/home');
}
}
public function logout(Request $req)
{
Session()->forget(['userid', 'username','session_id']);
Session()->flush();
//Session::flush();
return redirect('/login');
}
Try \Session()->flush(); instead of Session()->flush();
i am having this erro clicking in on the menu (Build) so i dont understand. someone can help me please? Why this is happining in this line below?
public function run($url = null)
{
$params = RouterHelper::segmentizeUrl($url);
// Handle NotFoundHttpExceptions in the backend (usually triggered by abort(404))
Event::listen('exception.beforeRender', function ($exception, $httpCode, $request) {
if (!$this->cmsHandling && $exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return View::make('backend::404');
}
}, 1);
/*
* Database check
*/
if (!App::hasDatabase()) {
return Config::get('app.debug', false)
? Response::make(View::make('backend::no_database'), 200)
: $this->passToCmsController($url);
}
the erro start happining at this line---------->
$controllerRequest = $this->getRequestedController($url);
if (!is_null($controllerRequest)) {
return $controllerRequest['controller']->run(
$controllerRequest['action'],
$controllerRequest['params']
);
}
/*
* Fall back on Cms controller
*/
return $this->passToCmsController($url);
}
To make a View you don't need to make Response. Instead of
Response::make(View::make('backend::no_database'), 200)
use
return View::make('backend::no_database');
or this for custom page
return View::make('author.plugin::backend.file'); # htm is on views/backend/file.htm
By the way check above on same file, it has return View::make('backend::404');
I have a function that can send mail on Laravel5 using this
/**
* Send Mail from Parts Specification Form
*/
public function sendMail(Request $request) {
$data = $request->all();
$messageBody = $this->getMessageBody($data);
Mail::raw($messageBody, function ($message) {
$message->from('yourEmail#domain.com', 'Learning Laravel');
$message->to('goper.zosa#gmail.com');
$message->subject('Learning Laravel test email');
});
return redirect()->back();
}
/**
* Return message body from Parts Specification Form
* #param object $data
* #return string
*/
private function getMessageBody($data) {
$messageBody = 'dummy dummy dummy dummy';
}
and is sent successfully. But how to check if it was sent or not? Like
if (Mail::sent == 'error') {
echo 'Mail not sent';
} else {
echo 'Mail sent successfully.';
}
I'm just guessing that code.
I'm not entirely sure this would work but you can give it a shot
/**
* Send Mail from Parts Specification Form
*/
public function sendMail(Request $request) {
$data = $request->all();
$messageBody = $this->getMessageBody($data);
Mail::raw($messageBody, function ($message) {
$message->from('yourEmail#domain.com', 'Learning Laravel');
$message->to('goper.zosa#gmail.com');
$message->subject('Learning Laravel test email');
});
// check for failures
if (Mail::failures()) {
// return response showing failed emails
}
// otherwise everything is okay ...
return redirect()->back();
}
Hope this helps
The Mail::failures() will return an array of failed emails.
Mail::send(...)
if( count(Mail::failures()) > 0 ) {
echo "There was one or more failures. They were: <br />";
foreach(Mail::failures() as $email_address) {
echo " - $email_address <br />";
}
} else {
echo "No errors, all sent successfully!";
}
source : http://laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent
For Laravel 9.11.0
Mail::failures() // is deprecated in laravel 9.11.0
To check if your email was sent successfully one could wrap mail send in a try catch block:
try {
Mail::to($userEmail)->send($welcomeMailable);
} catch (Exception $e) {
//Email sent failed.
}
or since Mail::to($email)->send($mailable) on success returns an instance of : SentMessage one could check:
$welcomeEmailSent = Mail::to($userEmail)->send($welcomeMailable);
if ($welcomeEmailSent instanceof \Illuminate\Mail\SentMessage) {
//email sent success
} else {
//email sent failed
}
You may additionally can make use "Swift_TransportException" to identify any errors.
try{
//code to send the mail
}catch(\Swift_TransportException $transportExp){
//$transportExp->getMessage();
}
You can use the Mail::failures() function for that.
It will have a collection of failed mails if it exists so you can use the code below to check for it.
public function sendMail(Request $request) {
$data = $request->all();
$messageBody = $this->getMessageBody($data);
Mail::raw($messageBody, function ($message) use ($messageBody) {
$message->from('yourEmail#domain.com', 'Learning Laravel');
$message->to('goper.zosa#gmail.com');
$message->subject($messageBody);
});
// check for failed ones
if (Mail::failures()) {
// return failed mails
return new Error(Mail::failures());
}
// else do redirect back to normal
return redirect()->back();
}
Hello i create website in laravel but i facing one problem. The problem is that when user is not log in and user type www.test.com/notifications that time showing error like this
ErrorException (E_UNKNOWN)
Undefined variable: messages (View: /home/test/app/views/message-page.blade.php)
But i want to when user is not log in and enter www.test.com/notifications so user automatic redirect to index page. Please help me i very confuse.
I using the some code in base controller is as follows:
public function checkLoggedIn(){
if(Auth::user()->check()){
return;
}
else {
return Redirect::to("/");
}
}
You should do it this way:
public function checkLoggedIn(){
if (!Auth::check()) {
return Redirect::to("/");
}
return true;
}
However I assume you want to use this function in another controller so then you should do it this way:
$result = $this->checkLoggedIn();
if ($result !== true) {
return $result;
}
to make redirection.
But Laravel have filters so you can easily check if user is logged.
You can just use in your routes.php:
Route::group(
['before' => 'auth'],
function () {
// here you put all paths that requires user authentication
}
);
And you can adjust your filter in app/filters for example:
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::to('/');
}
}
});