Mailtrap does not show the body of the message - laravel

The function here is to send a six digit pin to email so as to recover password. Mailtrap inbox does not show the six digit pin, just a "button text". How do you send the recovery code /pin to the recovery mail.
class UserPasswordPin extends Mailable {
use Queueable, SerializesModels;
public $pin;
public function __construct($pin)
{
$this->pin = $pin;
}
public function build()
{
return $this->markdown('emails.userpin')->with([
'pin' => $this->pin,
]);
}
}
The recover password controller send the six digit code. it works in postman but when it send email to mailtrap its blank. And also is there an algorithm for the method unwantedPin, like it selects a six digit conbination and prevents sending digits which are predictable?
class RecoverPasswordController extends Controller{
private $unwantedPins = [
111111,222222,333333,444444,555555,666666,777777,888888,999999
];
private function inUnWanted($pin)
{
return in_array($pin,$this->unwantedPins);
}
public function recover(RecoverPasswordRequest $request){
//check if email exist
$user = DB::table('users')->where([
'email' => $request->email
])->first();
if (is_null($user)){
return response([
'errors'=>'The email entered is not registered'
],404);
}
//send a six digit code/pin to the recovery email
$pin = rand( 111111,999999);
foreach ($this->unwantedPins as $unwantedPin){
if ($pin == $unwantedPin){
$pin = rand(111111,999999);
}
}
Mail::to($user)->queue(new UserPasswordPin($pin));
return response([
'pin'=>$pin,
'message'=>'a six digit code has been sent to your email'
],200);
}
}

the problem was in userpin.blade.php
#component('mail::message')
# Introduction
This is your six digit number.
#component('mail::button', ['url' => ''])
{{$pin}}
#endcomponent
Thanks,<br>
{{ config('app.name') }}
#endcomponent

Related

Send a mail inside botman (laravel 5.7)

I struggle to send a mail inside a botman reply.
I use mailhog, with botman 2.0 and laravel 5.7.
I've tested mailhog with simple php code , it work.
Here my env file config :
MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
The conversation start here :
<?php
namespace App\Conversations\SASI\CreationTicket;
//use Validator;
use Illuminate\Support\Facades\Validator;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Conversations\Conversation;
use App\Conversations\SASI\CreationTicket\EndTicketResume;
class EndTicket extends Conversation
{
public function askProblem()
{
$this->ask('Décrivez succintement votre problème :', function(Answer $answer) {
$this->bot->userStorage()->save([
'problem' => $answer->getText(),
]);
$this->askEmail(); //passe à la fonction suivante
});
}
public function askEmail()
{
$this->ask('Quel est votre email?', function(Answer $answer) {
$validator = Validator::make(['email' => $answer->getText()], [
'email' => 'email',
]);
if ($validator->fails()) {
return $this->repeat('Cette email ne semble pas valide. Entrez un email valide.');
}
$this->bot->userStorage()->save([
'email' => $answer->getText(),
]);
$this->askMobile();
});
}
public function askMobile()
{
$this->ask('Quel est votre numéro pro?', function(Answer $answer) {
$this->bot->userStorage()->save([
'mobile' => $answer->getText(),
]);
$this->say('Merci');
//renvoi a une autre conversation app\Conversations\SelectServiceConversation.php
$this->bot->startConversation(new EndTicketResume());
});
}
public function run()
{
$this->askProblem();
}
}
As you can see the bot say "Merci" (thanks) and go to another conversation. If add my mail code the bot stop before he say "Merci"
So the next conversation :
<?php
namespace App\Conversations\SASI\CreationTicket;
use Illuminate\Support\Facades\Mail;
use BotMan\BotMan\Messages\Conversations\Conversation;
class EndTicketResume extends Conversation
{
public function confirmTicket()
{
$user = $this->bot->userStorage()->find();
$message = '------------------------------------------------ <br>';
$message .= 'Probleme : '.$user->get('problem').'<br>';
$message .= 'Email : '.$user->get('email').'<br>';
$message .= 'Tel : '.$user->get('mobile').'<br>';
$message .= '------------------------------------------------';
$this->say('Votre demande va être envoyé. <br><br>'.$message);
$email = $user->get('email');
$data = [
'first' => 'test',
'last' => 'TEST'
];
// send email with details
Mail::send('emails.justshoot', $data, function($message) use ($email) {
$message->subject('Welcome');
$message->from($email, 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('test#gmail.com');
});
}
/**
* Start the conversation.
*
* #return mixed
*/
public function run()
{
$this->confirmTicket();
}
}
If I remove this
Mail::send('emails.justshoot', $data, function($message) use ($email) {
$message->subject('Welcome');
$message->from($email, 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('test#gmail.com');
});
The bot say his last message, all is ok. But if I add this code , as I said previously, the bot won't even go to another conversation.
What I am doing wrong ?
Try to put the mail in a seperate function and different controller
you can debug using network tab in the console

how to getting the email verification link in Unit Test Laravel

I am developing a Laravel application. I am doing unit testing to my application. Now I am having a trouble with testing the verification process.
What I am trying to do now is that I am registering a user, then test if the verification email is sent, then I will get the verification link for that registration, then I will do something with that link.
The first issue is that the email is not sent.
The second issue is that I do not know how to retrieve the
verification email link?
This is my test
public function test_user_can_be_verified_and_redirected_based_on_role()
{
Notification::fake();
$user = $this->registerUser();
Notification::assertSentTo($user, SendEmailVerificationNotification::class);
}
protected function registerUser()
{
$user = factory(User::class)->make();
$this->post(route('register'), [
'name' => $user->name,
'email' => $user->email,
'password' => 'testing',
'password_confirmation' => 'testing',
])->assertRedirect();
return User::whereEmail($user->email)->first();
}
But the issue is that the notification is not sent even if it is sent when I register from the browser. I also like to retrieve the verification link and do something. How can I do that?
Not a perfect solution but it does the trick, if somebody breaks this functionality somehow I'll know about it. :)
First overwrite the protected method verificationUri of the VerifyEmail notification and make it public
class EmailVerificationNotification extends VerifyEmail
{
public function verificationUrl($notifiable) {
return parent::verificationUrl($notifiable);
}
}
Then use it to generate a link and assert against it..
/** #test */
public function an_user_can_verify_his_email_address()
{
$notification = new EmailVerificationNotification();
$user = factory(User::class)->create(['email_verified_at' => null]);
$uri = $notification->verificationUrl($user);
$this->assertSame(null, $user->email_verified_at);
$this->actingAs($user)->get($uri);
$this->assertNotNull($user->email_verified_at);
}
A verification URL can be generated outside of the VerifyEmail notification easily by simply running:
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
No need to make the verificationUrl() method public.
Here is my solution:
public function testVerifyEmailValidatesUser(): void
{
// VerifyEmail extends Illuminate\Auth\Notifications\VerifyEmail in this example
$notification = new VerifyEmail();
$user = factory(User::class)->create();
// New user should not has verified their email yet
$this->assertFalse($user->hasVerifiedEmail());
$mail = $notification->toMail($user);
$uri = $mail->actionUrl;
// Simulate clicking on the validation link
$this->actingAs($user)
->get($uri);
// User should have verified their email
$this->assertTrue(User::find($user->id)->hasVerifiedEmail());
}

I build a user invitation system with Laravel it does not work

I am building a user invitation system with Laravel that does not work I have the error here email invitation after sending the email to the user the descriptive image is lower Click here to activate!
my InviteController
public function process(Request $request)
{
// validate the incoming request data
do {
//generate a random string using Laravel's str_random helper
$token = str_random();
} //check if the token already exists and if it does, try again
while (Invite::where('token', $token)->first());
//create a new invite record
$invite = Invite::create([
'email' => $request->get('email'),
'token' => $token
]);
// send the email
Mail::to($request->get('email'))->send(new InviteCreated($invite));
// redirect back where we came from
return redirect()
->back();
}
public function accept($token)
{
// Look up the invite
if (!$invite = Invite::where('token', $token)->first()) {
//if the invite doesn't exist do something more graceful than this
abort(404);
}
// create the user with the details from the invite
User::create(['email' => $invite->email]);
// delete the invite so it can't be used again
$invite->delete();
// here you would probably log the user in and show them the dashboard, but we'll just prove it worked
return 'Good job! Invite accepted!';
}
my image error
Do a dd of $invite before passing to the mail to make sure you have the records:
//create a new invite record
$invite = Invite::create([
'email' => $request->get('email'),
'token' => $token
]);
dd($invite);
// send the email
Mail::to($request->get('email'))->send(new InviteCreated($invite));
Then on InviteCreated make sure you store $invite on a public property so its available on the blade template:
class InviteCreated extends Mailable{
public $invite;
public function __construct($invite){
$this->invite = $invite;
}
...... if you use markdown then pass the `$invite`..
public function build()
{
return $this
->markdown(your markdown mail')
->with([
'invite'=>$this->invite
]);
}
}

Laravel email resulting in view not found error

I am firing an event that sends an email to the user when he requests a password reset.
Here's the event listener that will send an email
class SendResetPasswordLink {
public function handle(UserForgotPassword $event) {
Mail::to($event->user)
->queue(new SendResetPasswordToken($event->user->passwordResetToken));
}
}
Here's my mail class:
class SendResetPasswordToken extends Mailable {
use Queueable, SerializesModels;
public $token;
public function __construct(PasswordReset $token) {
$this->token = $token;
}
public function build() {
return $this->subject('Reset your MyEngine password')
->markdown('emails.password.reset')
->text('emails.password.reset_text');
}
}
I have email files (both html and text) available at
resources/views/emails/password/reset.blade.php
and
resources/views/emails/password/reset_text.blade.php
It is not working and I am getting the following error:
"View [] not found. (View: /home/vagrant/Laravel/youtube/resources/views/emails/password/reset.blade.php)"
What do I need to do? All my blade files are in place.
Here's my reset.blade.php
#component('mail::message')
<strong>Hello {{ $token->user->getFirstNameOrUserName() }}!</strong>
You are receiving this email because we received a password reset request for your account.
If you did not request a password reset, no further action is required.
#component('mail::button', [
'url' => route('password.reset', ['token' => $token,]) . '?email=' . urlencode($token->user->email)
])
Reset Password
#endcomponent
Thanks,<br>
{{ config('app.name') }}
#endcomponent
<hr>
If you’re having trouble clicking the <strong>"Reset Password"</strong> button, copy and paste the URL below into your web browser:<br>
<small>{{ route('password.reset', ['token' => $token,]) . '?email=' . urlencode($token->user->email) }}</small>
#endcomponent
I found the answer,
For whatever reason I had closed(ended) the markdown component twice #endcomponent.

How to process creditcard payment option on your website (without redirect to paypal) paypalexpressceckout with ci-merchant library

I am using ci-merchant library and integrated it succesfully and also works for paypal account owner user.But dont know how to processs for user who dont have paypal acc and wants to pay via credit or debit card on my website only*(without redirect to paypal)* any idea????abt that.....this is the code i use for the normal paypal payment in my controller and works good as well..
$this->load->library('merchant');
$this->merchant->load('paypal_express');
$settings = $this->merchant->default_settings();
$settings = array(
'username' => 'takeout_api1.rest.com',
'password' => '1369227981',
'signature' => 'AnOQDpMvzNQqHN5u7vb9BKLaKYLoALq6R0g3ohOwD4RQgO0DQDI5l7V4',
'test_mode' => true,
);
$this->merchant->initialize($settings);
$params = array(
'amount' => 1500.00,
'currency' => 'CAD',
'return_url' => 'http://192.168.1.7/takeout/order_detail/test',
'cancel_url' => 'http://192.168.1.7/takeout/order_detail/test');
$response = $this->merchant->purchase($params);
function test()
{
$settings = array(
'username' => 'takeout_api1.rest.com',
'password' => '1369227981',
'signature' => 'AnOQDpMvzNQqHN5u7vb9BKLaKYLoALq6R0g3ohOwD4RQgO0DQDI5l7V4',
'test_mode' => true);
$this->merchant->initialize($settings);
$params = array(
'amount' => 1500.00,
'currency' => 'CAD',
'return_url' => 'http://192.168.1.7/takeout/order_detail/test',
'cancel_url' => 'http://192.168.1.7/takeout/order_detail/test');
$response = $this->merchant->purchase_return($params);
if ($response->success())
{
// mark order as complete
echo "yo";
exit;
}
else
{
$message = $response->message();
echo('Error processing payment: ' . $message);
exit;
}
}
You could interface your Merchant services
interface merchantServiceInterface
{
public function initialize();
public function purchase();
public function purchase_return();
}
Paypal
class Paypal implements merchantServiceInterface
{
public function initialize(){}
public function purchase(){}
public function purchase_return(){}
}
Credit/Debit Cards
class Realex implements merchantServiceInterface
{
public function initialize(){}
public function purchase(){}
public function purchase_return(){}
}
Now in your form, have a little radio button group and ask the user to select
either paypal or credit/debit card
<label>Choose a payment Method</label>
<label>Paypal<label>
<input type="radio" name="merchant" value="paypal" />
<label>Credit/Debit Card<label>
<input type="radio" name="merchant" value="debit" />
Merchant Library
class Merchant
{
protected $_service;
public function __construct(merchantServiceInterface $service)
{
$this->_service = $service;
}
public function initialize()
{
// Will either run Paypal/Realex initialize()
// Depending on what instance was passed to _service
//
$this->_service->initialize();
}
}
Controller
class Controller extends CI_Controller
{
public function method()
{
if($this->input->post('merchant') == 'paypal')
{
$service = new Paypal();
}
elseif($this->input->post('merchant') == 'debit')
{
$service = new Realex();
}
$this->load->library('Merchant', $service);
$this->merchant->initialize();
}
}
Edit to answer your comment
I just used Realex as an example
You need to figure out what both libraries have in common, or at a very low level abstraction figure out what they share.
an example would be
They both need a initialize method to configure options
They both need to send a request to an API
They both need a responce
etc etc keep abstracting away
How you handle these, will be unique to the library itself.
interface merchantServiceInterface
{
// Use the facade design pattern here
// so configuration is done in each library
public function initialize();
// Send a request with data
// Paypal - use http_build_query and curl
// Realex - use xml and curl
public function request(array $data);
public function responce();
}
Paypal Express Checkout doesn't support taking credit cards on your site. It is an off-site gateway, so redirect is mandatory.
You need to explore using PayPal Pro, Payflow, or any number of other gateways which support accepting credit cards directly on your website (plus the extra PCI requirements which come with that).

Resources