Gagan
Gagan

Reputation: 5656

Send Email error

I am using Laravel queues to get my mail working. My SendMail class is as such

class SendMail {
    public static function sendAbuseEmail($job,$data, $toEmail, 
    $subject, $fromEmail, $fromName) {
        Mail::send(array('html' => 'emails.abuseUrl.abuse'), 
                   $data, function ($m) use ($toEmail, $subject, 
                   $fromEmail, $fromName) {
            $m->from($fromEmail, $fromName);
            $m->to($toEmail)->subject($subject);
        });
        $job->delete();
    }
 }

and I am using it as such

$data = array('name' => Input::get('name'), 
              'quote' => Input::get('quote'), 
              'from' => Input::get('email'),
              'abuseUrl'=> Input::get('abuseUrl'));
$subject = Input::get('subject');
$toEmail = Config::get('settings.adminEmail');
$fromEmail = Input::get('email');
$fromName = Input::get('name');
Queue::push('SendMail@sendAbuseEmail', $data,$toEmail, 
           $subject, $fromEmail, $fromName);

but I am getting an error

[2014-08-20 17:25:41] production.ERROR: exception 'ErrorException' with message 'Missing argument 3 for Codeforge\Mailers\SendMail::sendAbuseEmail(), called in /var/www/laravel/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php on line 96 and defined' in /var/www/laravel/app/Codeforge/Mailers/SendMail.php:1

What am I doing wrong here?

Upvotes: 0

Views: 235

Answers (1)

clarkf
clarkf

Reputation: 3052

Take a look at the method signature for QueueInterface#push():

public function push($job, $data = '', $queue = null);

Where $data can be anything that's json_encodeable. Instead of passing ordered arguments, convention is to pass an associative array:

Queue::push('SendMail@sendAbuseEmail', array(
    'data' => $data,
    'toEmail' => $toEmail,
    'subject' => $subject,
    'fromEmail' => $fromEmail,
    'fromName' => $fromName
));

And in your worker, you can access the values like:

public static function sendAbuseEmail($job, array $data)
{
    $toEmail = $data['toEmail'];
    // etc...
}

This is due to the fact that all drivers (with the exception of the Sync driver) need to serialize this data and write it to some kind of data-store for later reference. I believe it was designed this way because it's just easier to keep track of if it's a single object.

Upvotes: 1

Related Questions