how to encoding email address inside email content - php

I'm using laravel 8.
I want to sending reset password link. but when I want to append email address to token as parameter:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('.....')
->action('Reset', url('password/reset', $this->token.urlencode('&email='.$notifiable->email)))
}
the result of email content is:
http://****/password/reset/326e15aad30c49acc55d39ff6983ccf33a838311b48c00318bc23e1a354b2c5b%2526email%253DS.M_Emian%2540yahoo.com
this output cannot decode from javascrpit.

You can send the Password Reset link using the existing functionality in Laravel 8 as documented here
use Illuminate\Support\Facades\Password;
$status = Password::sendResetLink(
$request->only('email')
);
More info about Password reset

Related

Add email next to token in Laravel forgot password template email link

I'm using Laravel 8.5 with authentication system. When user wants to change password it sends an email with link. Default email contains link which looks like this:
http://mywebsite/api/forgot-password?token=2ccece360b031db4dcadea0cbdf8dd47a1712632b727487a7226f19f8f607cc7&email=MyEmail%40gmail.com
I did some changes in email template. In ResetPasswordNotification.php I added this:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('MyApp password reset')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', $this->url)
->line('This password reset link will expire in 60 minutes.')
->line('If you did not request a password reset, no further action is required.');
}
and in User.php I have:
public function sendPasswordResetNotification($token)
{
$url = 'http://mywebsite/en/change-password?token=' . $token;
$this->notify(new ResetPasswordNotification($url));
}
and now I receive this link:
http://mywebsite/en/change-password?token=8f87fc2d504507as385b4c47cb015cee4192749d3f1d641863524d513abb2a39
which only contains token and not email. How do I add email to this so it would look something like this:
http://mywebsite/en/change-password?token=8f87fc2d504507as385b4c47cb015cee4192749d3f1d641863524d513abb2a39&email=UserEmail#gmail.com
Try passing email to the url.
public function sendPasswordResetNotification($token)
{
$url = 'http://mywebsite/en/change-password?token=' . $token . '&email=' .$this->email;
$this->notify(new ResetPasswordNotification($url));
}
Just add $this->email next to $token.
public function sendPasswordResetNotification($token)
{
$url = 'http://mywebsite/en/change-password?token=' . $token . '&email=' . $this->email;
$this->notify(new ResetPasswordNotification($url));
}

Get password reset URL

I'm trying to modify the default Laravel 5.6 Auth in a way to email new users a link to create their password, since this is an invite only system and I don't want to email created users their password as plaintext.
In 5.3 what I was able to do was grab the reset token from the password_resets table and send them a Notification with a "Create Password" button.
In 5.6 (not sure when this changed) it seems to be an encrypted version of their password reset token in the database. How would I then call the correct url in a custom notification for users to be able to create a password?
Here's what I had in 5.3:
controller
......
$token = strtolower(str_random(64));
DB::table('password_resets')->insert([
'email' => $request->email,
'token' => $token,
'created_at' => Carbon::now()
]);
$user->notify(new UserCreated($user));
......
password create email
.....
$token = DB::table('password_resets')->where('email', $user_email)->pluck('token')->first();
$url = url('/password/reset/' . $token);
......
Copying the same code over to 5.6, it tells me my reset tokens are invalid. It appears the tokens in the database no longer match the token in the url when doing a normal password reset. Now they appear to be encrypted or something?
I've made sure in the email the url and the token match exactly whats in the database, with the valid period set to like a week (to test), and every token created this way it says is invalid.
How then do you do auth for an invite only system, or how do you just manually create a reset token and then send it in a custom email? The docs mentions being able to replace the password reset email, but I don't want that, I want to supplement it.
Creating and saving the token can be done in a single line using the built-in functionality. Took me an hour and prodigious use of dd() to figure it out, but I added this to my User model:
<?php
namespace App;
use App\Notifications\AccountCreated;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
class User extends Authenticatable
{
use Notifiable;
public function generatePassword()
{
$this->password = Hash::make(str_random(32));
}
public function sendWelcomeEmail()
{
// Generate a new reset password token and save it to the database
$token = app("Password")::getRepository()->create($this);
// Send notification
$this->notify(new AccountCreated($this, $token));
}
}
The notification gets the user and the token, same as your code, so that they can be included in the email text. Then in UserController::store() I can just do:
$user = new User($request->all());
$user->generatePassword();
$user->save();
$user->sendWelcomeEmail();
In the email notification, you can use this to get the actual URL:
$url = route("password.reset", $token)
Solved it!
Here's my new controller and notification in 5.6 for manually sending a different password create email. All I really had to do was encrypt the token before storing it in the database! Then you pass the unencrypted token to the email for the url, which checks against the encrypted one in the DB.
controller
.....
$random_token = strtolower(str_random(60));
$encrypted_token = bcrypt($random_token);
DB::table('password_resets')->insert([
'email' => $request->email,
'token' => $encrypted_token,
'created_at' => Carbon::now()
]);
$user->notify(new AccountCreated($user, $random_token));
.....
In the email I'm just importing the user and the token...
.....
public $user;
public $token;
public function __construct(User $user, $token)
{
$this->user = $user;
$this->token = $token;
}
.....

Laravel how to pass the name of the user when I sent the forgot password function

I want to make my mail more detailed when the user has sent a forgot password reset link to his/her email. This is the sample of the picture when receiving a reset password link.
I want to add some details here that the Hello should be Hello! (user name here)
Here is the code that I added in my SendsPasswordResetEmails.php
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email')
);
$applicant_name = Applicant::where('email', $request->email)->get()->value('name');
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
}
and it should pass the data to app\Notifications\ApplicantResetPasswordNotification.php
public function toMail($notifiable)
{
return (new MailMessage)
->from('vcc3dummy#gmail.com', 'CCV3')
->greeting('Hello! Applicant Name') // Applicant name pass here
->line('You are receiving this email because we received a password request for your account.')
->action('Click here to Reset Password', route('applicant.reset', $this->token))
->line('If you did not reset your password, no further action is required.');
}
Looking for help on how to pass the data or how to query it.
Would appreciate if someone could help me
Thanks in advance.
In your ApplicationResetPasswordNotification.php you can use the $notifiable variable as follows:
public function toMail($notifiable)
{
return (new MailMessage)
->from('vcc3dummy#gmail.com', 'CCV3')
->greeting('Hello!' . $notifiable->name) // Applicant name
...
}
Please mark as answer if that works for you!
This is the another way to send the mail in laravel -
Put that data you want to use/show in email template.
$data = [
'email' => $email,
'remember_token' => $remember_token,
'name' => $applicant_name
];
Mail::send('emails/forgotmail', $data, function ($message) use ($data) {
$message->from('youremail#gmail.com');
$message->to( $data['email'] )->subject('Forgot Password Link');
});
Where as 'email/forgotmail' is in 'resources/views/email/forgotmail.blade.php' that you have to create. So that here you can put your email template over here and make use of $data in it .

Laravel : Password reset email check against user

I have the following code to send the password reset email to the users email which is working:
$response = $this->passwords->sendResetLink($request->only('email'),function($message)
{
$message->subject('Password Reminder');
});
What i want is that user should write their username instead of email, And i will check the email against that username , And will send the email.So i came up with this idea.
$usernameToEmail = User::where('name','=', Input::get('username'))->first();
$response = $this->passwords->sendResetLink(['name' => $usernameToEmail],function($message)
{
$message->subject('Password Reminder');
});
Which is not working also.
Am i missing something ?
You're close, but your $usernameToEmail variable contains a User object, not an email string. Most likely you just need to add a method to your chain:
$usernameToEmail = User::where('name','=', Input::get('username'))->first()->email;

Laravel Not sending Email

am having some sort of an issue with sending mails from my website with Laravel 4, I followed the on-line documentation but haven't successfully sent a mail with Laravel my code is as show below.
Mail::send('emails.contactmail', $data, function($message)
{
$name = Input::get('name');
$email = Input::get('email');
$message->from($email, $name);
$message->to('info#mysite.org', 'Info at My Site')
->subject('Website contact form');
});
I use following code for sending temporary password in case of forgot password
public function postForgotPassword(ForgotPasswordRequest $request){
$email=$request->email;
$objUser=DB::table('users')
->where('email','=',$email)
->select('email','id','first_name','last_name','user_group_id')
->first();
$string = str_random(15);
$pass=\Hash::make($string);
$objUser2=User::find($objUser->id);
$CURRENT_TIMESTAMP=new DateTime();
$objUser2->temporary_pass=$pass;
$objUser2->pass_status=1;
$objUser2->updated_at=$CURRENT_TIMESTAMP;
$objUser2->save();
$data=array("email"=>$email,"pass"=>$string,"first_name"=>$objUser->first_name,"last_name"=>$objUser->last_name);
$email=array("email"=>$email);
Mail::send('emails.forgot_password',$data, function($message) use($email) {
$message->to($email['email'],'MyProject')->subject('Password Recovery ');
});
return Redirect::to('auth/login')->with('flash_message','Check your email account for temporary password');
}
And I write email template in forgot_password.blade.php which is located in view.emails folder.

Categories