fahmi rizal
fahmi rizal

Reputation: 33

Segmentation fault (core dumped) Laravel SwiftMailer

I'm having problem when using SwiftMailer Laravel 5.2. I have a controller for sending email and I set it to run every minute. When I execute my script below with php artisan schedule:run, its keeps return an error message 'Segmentation fault (core dumped)'.

My script:

public static function sendMailCoc()
{
    $mail_log = MailLog::where('status', '!=', 'SENT')->where('type', '3')->orderBy('id', 'asc')->get();
    foreach($mail_log as $mail){
        if($mail->to!='') {
            $coc = json_decode($mail->parameter, true);
            try {
                Mail::queue($mail->file_view,
                    ['to' => $mail->to_name,
                        'coc' => $coc,
                        'notif_id' => $mail->notification_id
                    ],
                    function ($message) use ($mail) {
                        $message->to($mail->to)
                            ->subject($mail->subject);
                    });
                $mail->status = 'SENT';
                $mail->error_message = '';
                $mail->save();
            }
            catch(Swift_TransportException $e){
                $mail->status = 'ERROR';
                $mail->error_message = $e->getMessage();
                $mail->save();
                continue;
            }catch (Exception $e) {
                $mail->status = 'ERROR';
                $mail->error_message = $e->getMessage();
                $mail->save();
                continue;
            }                    

        }
    }
}

Sometimes if I test to send just one email, it works and not return an error message. But when I try to send a lot of email with looping, its always get an error message "Segmentation fault (core dumped)". I'm using Ubuntu Server 14.04.

I don't know what is causing this error. It is my script that causing this problem, Laravel, or Ubuntu Linux?

Is there any way to solve this problem? What should I check first?

Thank you for your help and answer.

Upvotes: 2

Views: 3817

Answers (2)

Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41410

Mailable can have implements ShouldQueue on your class.

Then you pass parameters to __construct and you save it in your class.

Like this:

public function __construct(Sale $sale)
{
    $this->sale         = $sale->withoutRelations();
...

BUT if you forget withoutRelations() function, the system will Serialize your object with links that will be lost after you UnSerialize your data back. That would cause the message

Segmentation fault (core dumped)

Simple test that show you that Queue is at fault if to disable it for testing. For that go to phpunit.xml file and change

<env name="QUEUE_DRIVER" value="sync"/>

to

<env name="QUEUE_DRIVER" value="null"/>

Upvotes: 1

erlangsec
erlangsec

Reputation: 2611

Wild shot, but I've been debugging the same error message in relation to queued jobs recently:

As per Laravel 5.6 documentation (https://laravel.com/docs/5.6/queues#creating-jobs):

"Binary data, such as raw image contents, should be passed through the base64_encode function before being passed to a queued job. Otherwise, the job may not properly serialize to JSON when being placed on the queue."

My problem was resolved (almost!) as soon as I added base64_encode($input) in constructor and base64_decode($this->input) in handle() function.

PS: Since I was using two distributed apps for input and working on the queue, I also had to run composer dump-autoload and php artisan cache:clear on both apps, while also restarting the supervisor worker, after making the code change.

Upvotes: 0

Related Questions