-1

Herewith I'm using 'Mail::raw()' to send and email in Laravel - PHP. After getting request from the database, I assigned the receiver's email address to '$email'. In this case '$message->to('$email');' given an error 'Address in mailbox given [$email] does not comply with RFC 2822, 3.6.2.'. Instead of '$email', 'abcdef@test.com' its working perfectly. And also inside the 'Mail::row()' function, unable to get request values instead of hard code email address. Below is the code.

Controller.php

public function sendEmailToUser(Request $request)
    {
    $email = (String)$request->get('email');
    $messageBody = $request->get('password');

    $response = null;
    $res_type = null;

    \Mail::raw($messageBody, function ($message) {
        $message->from('abcd@gmail.com', 'TESTING');
        $message->to('$email');
        $message->subject('TESTING TITLE');
    });

    // check for failures
    if (\Mail::failures()) 
    {
        $response ="Email sent unsuccess";
        $res_type = 'warning';
    }
    else
    {
        $response ="Email sent successful";
        $res_type = 'success';

    }

    return redirect()->back()->with($response,$res_type);
}
Nuwan Withanage
  • 393
  • 7
  • 19
  • 1
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – miken32 Nov 07 '18 at 04:13
  • In here problem is how to pass variable as a email to Mail::raw function, not the single-quoted or double-quoted string – Nuwan Withanage Nov 07 '18 at 05:26
  • 1
    The problem is you've got your `$email` variable inside single quotes. – miken32 Nov 07 '18 at 05:31
  • No single-quotes is not the problem. Need to use $request within \Mail::raw() function – Nuwan Withanage Nov 07 '18 at 05:44

1 Answers1

0

Finally got a solution is use '$request' with \Mail::row() function.

\Mail::raw($request->password, function ($message) use($request) 
        {
            $message->from($request->fromEmail, 'TESTING');
            $message->to($request->toEmail);
            $message->subject('TESTING TITLE');
        });
Nuwan Withanage
  • 393
  • 7
  • 19