Veljko Stefanovic
Veljko Stefanovic

Reputation: 511

Emailing in Laravel

I am trying to send a email in Laravel. It works finicky to say at least. got couple of classes for sending email for different conditions. Only difference would be markup of sent email and blade file which data is sent to before sending. I tried using use Mail; in controller from whom i'm sending it, with \Mail::(... and use Illuminate\Support\Facades\Mail; with Mail::(... and vice-versa. So it ain't it.

Here are the mail files:

ToHR file:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;

class toHR extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $job;
    public $jobCount;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user,$job,$jobCount)
    {
        $this->user = $user;
        $this->job = $job;
        $this->jobCount = $jobCount;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('mail.toHR');
    }
}

ToHRFirst file:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;

class toHRFirst extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $job;
    public $jobCount;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user,$job,$jobCount)
    {
        $this->user = $user;
        $this->job = $job;
        $this->jobCount = $jobCount;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('mail.toHRFirst');
    }
}

And my test file JobCreated:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;

class JobCreated extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $job;
    public $jobCount;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user,$job,$jobCount)
    {
        $this->user = $user;
        $this->job = $job;
        $this->jobCount = $jobCount;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('mail.job-created');
    }
}

They'll all the same, yet error occurs. What gives? Did i miss something? Any help will be appreciated, just point me in the right direction.

Code which i use to send email: Variables used get values from queries which are rather long, but they are not an issue anyways.

Ver1:


        Mail::to($user->email)->send(

            new JobCreated($user,$job,$jobCount)

        );

Ver2:


        Mail::to($user->email)->send(

            new toHRFirst($user,$job,$jobCount)

        );

Ver3:


        Mail::to($user->email)->send(

            new toHR($user,$job,$jobCount)

        );

Sending works if it's used first version. Other cannot be found and that's the error.

Upvotes: 0

Views: 45

Answers (1)

pimarc
pimarc

Reputation: 4145

Try renaming your files to follow Laravel convention;

You can see that the file JobCreated has a class JobCreated. You need to respect the uppercases:

  • Rename the ToHR.php file into ToHr.php, then:
  • Rename the class class toHRinto class ToHr

  • Rename the ToHRFirst.php file into ToHrfirst.php, then:

  • Rename the class class toHRFirst into class ToHrfirst

Or if you want to keep the uppercases, just rename the classes like so:

  • class toHRinto class ToHR
  • class toHRFirstinto class ToHRFirst

Finally, don't forget to import your job class at the top of your controller file (or wherever you use the Mail method:

use App\Mail\ToHR;

and

use App\Mail\ToHRFirst;

Upvotes: 2

Related Questions