0

I am trying to send e-mail with Laravel, but I need to send the customer information and the randomly generated password when sending mail.Customer information appears in the outgoing mail, but the password is empty.

Controller function

public function register(RegisterRequest $request)
    {
            $random = str_shuffle('abcçdefgğhıijklmnoöpqrsştuüvwxyzABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ234567890!$%^&!$%^&');
            $password = substr($random, 0, 10);
            $hashed_password = Hash::make($password);
            $activation_key = Str::random(60);
            $request->request->add(["password"=> $hashed_password,"activation_key"=> $activation_key]);
            $customer = Customer::create($request->all());
            if ($customer) {
                Mail::to(request('email'))->send(new CustomerRegisterMail($customer, $password));
                Alert::success('Success', 'Some Success Message');
                return back();
            } else {
                Alert::error('Error', 'Some Error Message');
                return back();
            }
    }

CustomerRegisterMail.php

class CustomerRegisterMail extends Mailable
{
    use Queueable, SerializesModels;

    public $customer;
    public $password;

    public function __construct(Customer $customer, $password)
    {
        $this->customer = $customer;
        $this->password = $password;
    }

    public function build()
    {
        return $this
            ->from('[email protected]','Some Name')
            ->subject(config('app.name'). ' - Customer Register')
            ->view('mails.customer_register')->with(['name' => $this->customer->name, 'surname' => $this->customer->surname, 'email' => $this->customer->email, 'password' => $this->password]);
    }
}

customer_register.blade.php

Hello {{$name}} {{$surname}},

E-Mail : {{$email}}<br>
Password: {{$password}}

And what i get

Hello Behçet Atalay,

E-Mail: [email protected]
Password: 

How can I send two variables to mail view at the same time?

2 Answers 2

1

Update your blade file

you are pass password variable in mail file and tyring to retrieve with sifre in blade

customer_register.blade.php

Hello {{$name}} {{$surname}},

E-Mail : {{$email}}<br>
Password: {!! $password !!}

Actually {{ $password }} is escape your value b"$%JBg±ÄzrÅ"

{!! $password !!} not escape your value

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, when i'm posting here, i'm translating, my all code turkish normally. That's not the problem. I have no error with variable names.
in __construct did you get $password value?
when i used dd($password) in __construct i'm getting password b"$%JBg±ÄzrÅ" like this.
check once again in blade with {!! $password !!}
1

Having them as public properties in your mail class you can just directly use them in your view without passing them using with. So in your customer_register view, you can call {{$customer->name}} and it will work. Same goes for the password, use just {{ $password }} in your view.

Btw, your problem is, you are passing the password using the password key and calling it using $sifre which does not exist hence it is empty.

3 Comments

Sorry, when i'm posting here, i'm translating, my all code turkish normally. That's not the problem. I have no error with variable names.
In your register method, make sure that you are passing the correct password when creating the mail. As I see there wrong variables used as well. I am talking about this call: new CustomerRegisterMail($customer, $password). You can debug this in your constructor of the mail class dumping the password to make sure it is the right one using dd($password)
when i used dd($password) in __construct i'm getting password b"$%JBg±ÄzrÅ" like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.