Tommy Yamakaitis Tommy Yamakaitis - 5 months ago 31
PHP Question

OctoberCMS - Mail::send(), Undefined variables - Laravel

I am trying to send the SAME email to multiple addresses given from an array

$emails
.

I created a class called
SendMail
, and inside is a
sendPost()
method that accepts 2 arguments:
($post, $emails)


Here is my code:

class SendMail {

public static function sendPost($post, $emails)
{
Mail::send('acme.blog::mail.message', $post, function($message) {

$message->to($emails);
$message->from('mail@compuflexcorp.com', 'Compuflex Mail');
$message->subject($post['subject']);
$message->replyTo($post['email']);

});
}
}


The problem is, I keep receiving an error:


"Undefined variable $emails" on Line 14 of C:\...\SendMail.php


Line 14:
$message->to($emails);


What I have tried:


  1. I checked to see if I can access the
    $post
    and
    $emails
    variables inside of
    sendPost()
    , but outside of
    Mail::send()
    . And the answer is YES, I can access the information inside of
    $post
    and
    $emails
    inside of
    sendPost()
    , so the variables are, in fact, being passed to the
    sendPost()
    method.

  2. I, at first, thought it had something to do with the fact that
    $emails
    is not one of the arguments for
    Mail::send()
    , so I put
    $post
    and
    $emails
    into one array called
    $vars
    , but then I got the error:




"Undefined variable $vars" on Line 14 of C:\...\SendMail.php


So, I realized that the issue seems to be that I can't pass any variables to
Mail::send()
, or in other words, I just don't know how to...

Any help would be greatly appreciated...
Thomas Yamakaitis

Answer Source

You need to pass the $emails variable as follows:

class SendMail {

public static function sendPost($post, $emails)
{
        Mail::send('acme.blog::mail.message', $post, function($message) use ($emails) {

            $message->to($emails);
            $message->from('mail@compuflexcorp.com', 'Compuflex Mail');
            $message->subject($post['subject']);
            $message->replyTo($post['email']);

        });
    }
}