1

Is it possible to get the value of the $string which is stored outside the function (For Mail)

Here i tried.

$string = str_random(5);     

Mail::send([], [], function($message,$string)
    {
        $user = MailTemplate::find(1);
        $mail_body = $user->MailContent;
        $mail_body = str_replace("{password}", $string, $mail_body);
        $message->setBody($mail_body, 'text/html');
        $message->to(Session::get('sess_mail'));
        $message->subject('Password Details - Ma$na Taxi');
    });

But it shows error

Missing argument 2 for LoginController::{closure}()

How can i pass the value of $string inside the mail function like

Mail::send([], [], function($message,$string)

So it should be accessible at $mail_body = str_replace("{password}", $string, $mail_body);

1 Answer 1

2

Your call to the static method Mail::send() needs to match the signature specified here - http://laravel.com/docs/4.2/mail#basic-usage

$string = str_random(5);     

Mail::send('name-of-view-used-for-email',
     array('foo' => $string), function($message) use ($string)
    {
        $user = MailTemplate::find(1);
        $mail_body = $user->MailContent;
        $mail_body = str_replace("{password}", $string, $mail_body);
        $message->setBody($mail_body, 'text/html');
        $message->to(Session::get('sess_mail'));
        $message->subject('Password Details - Ma$na Taxi');
    });

How can i pass the value of $string inside the mail function like so it should be accessible at $mail_body = str_replace("{password}", $string, $mail_body);

You have a scope issue - in order to get be pass $string you need to use use ($string) before your function call.
As Ive shown in the updated code.

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

3 Comments

Thanks, If i need to use $string1, $string2 then i should do array('foo' => $string, 'foo1' => $string1, 'foo2' => $string2), function($message) use ($string,$string1, $string2 ) Like this right ?
(assuming you are using blade for the email view ) what you pass in the array can be accessed in email.blade.php as {{ $foo }} {{ $foo1}}
use ($string,$string1, $string2 ) would only be necessary if you wanted to access those variables inside your function Mail::send(.... function () { $string,$string1, $string2 })

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.