Jaime Dolor jr.
Jaime Dolor jr.

Reputation: 919

Why can't my Laravel script use variables in sending mail

I'm using Laravel 4.x and have a controller script that sends email. If my recipient parameters are hard coded, the script works.

$email_data = array('inv_no' => '12345');

Mail::send('view.emailbody', $email_data, function($message) {
  $message->to('[email protected]', 'ABC Co')->subject('Invoice');
});

But if I use variables on recipient parameters, my script does not work.

$email_data = array('inv_no' => '12345');
$company_email = '[email protected]';
$company_name = 'ABC Co';

Mail::send('view.emailbody', $email_data, function($message) {
  $message->to($company_email, $company_name)->subject('Invoice');
});

I really can't figure out where my error is.

Thanks in advance

Upvotes: 0

Views: 33

Answers (2)

Hasan Tareque
Hasan Tareque

Reputation: 1741

You have to pass those variable then it will work as follows:

$email_data = array('inv_no' => '12345');
$company_email = '[email protected]';
$company_name = 'ABC Co';

Mail::send('view.emailbody', $email_data, function($message) use ($company_email, $company_name) {
  $message->to($company_email, $company_name)->subject('Invoice');
});

Upvotes: 1

Jaime Dolor jr.
Jaime Dolor jr.

Reputation: 919

For some (strange) reason, if the recipient parameters are coming from form submit entries, the script works

$email_data = array('inv_no' => '12345');

Mail::send('view.emailbody', $email_data, function($message) {
  $message->to(Input::get('email'), Input::get('company-name'))->subject('Invoice');
});

Upvotes: 0

Related Questions