-1

i was trying to send an email and when i want to whrite the email address of the destination (the email is a parameter of the function), i have this error. Can someone help me.

this is my function:

public function email($name, $email2) {
  $data = array('name'=> $name);
  Mail::send('mail',$data,function($message) {
    $message->to($email2, $name)->subject('Deposit Confirmation');
    $message->attach(public_path('document.pdf'));
    $message->from('xxx@xxx.pt', 'XXX');
  });
}

this is my error: "Undefined variable email2"

thank you

Nuno
  • 61
  • 1
  • 10

1 Answers1

0

When you use anonymous function to pass variables you should use use construction

public function email($name, $email2) {
      $data = array('name'=> $name);
      Mail::send('mail',$data,function($message) use ($name, $email2) {
        $message->to($email2, $name)->subject('Deposit Confirmation');
        $message->attach(public_path('document.pdf'));
        $message->from('xxx@xxx.pt', 'XXX');
      });
    }

Here you can find more examples about anonymous functions https://www.php.net/manual/en/functions.anonymous.php

Here simple example from official documentation

$message = "hello";

// Inherit $message
$example = function () use ($message) {
    var_dump($message);
};

$example();

// Output: string(5) "hello"
potiev
  • 546
  • 2
  • 11