Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dplumptre's avatar

laravel 5 and emails

Hello every one , i decided to upgrade to laravel 5, but im still trying to understand the email part , i send emails with laravel 4 without no issues, in my config/mail.php i usually set the driver to mail. whe sending the mail i get an error "Class 'App\Http\Controllers\Mail' not found"

after surfing the net i discovered because of the new name space io will need to add a slash making it look like '\Mail" view below

\Mail::send('mail.message-baptismform', array('firstname'=> $request->get('firstname')), function($message) { $message->to('myself@yahoo.com','Class')->subject(' Believers Class'); });

i get FatalErrorException in fe7649f89a1edc747e4779375898461f line 43: syntax error, unexpected 'class' (T_CLASS)

it seems laravel 5 is not stable , please any help will be good i am thinking of going back to laravel 4

0 likes
9 replies
toniperic's avatar

@dplumptre Laravel 5 is stable.

By default, Laravel 4 applications did not utilize namespacing within your application code. So, for example, all Eloquent models and controllers simply lived in the "global" namespace.

Try putting this at the top of your controller

use Illuminate\Contracts\Mail\Mailer as Mail;
dplumptre's avatar

this is what i get after adding it

FatalErrorException in fe7649f89a1edc747e4779375898461f line 43: syntax error, unexpected 'class' (T_CLASS)

this is how my code looks like

namespace App\Http\Controllers;

use Illuminate\Contracts\Mail\Mailer as Mail;

use App\Http\Requests\BaptismFormRequest;

use App\Http\Requests\NamingCeremonyRequest;

class WelcomeController extends Controller {

public function baptismForm() { return view('welcome.baptism-form');

} 
public function dobaptismForm(BaptismFormRequest $request)
{

\Mail::send('mail.message-baptismform', array('firstname'=> $request->get('firstname')), function($message) { $message->to('dplumptre@yahoo.com','Class')->subject(' Believers Class'); });

  //  Redirect::to('welcome.naming-ceremony');


}  

no luck still

dplumptre's avatar

also after adding use Illuminate\Contracts\Mail\Mailer as Mail;

when i removed the slash from \Mail making it Mail, i get this error

Non-static method Illuminate\Contracts\Mail\Mailer::send() cannot be called statically, assuming $this from incompatible context

toniperic's avatar

Yeah sorry, my bad actually.

Try using

use Illuminate\Mail\Mailer as Mail;

instead.

After that, you don't need to use \Mail but Mail.

dplumptre's avatar

i did just that and i get Non-static method Illuminate\Mail\Mailer::send() should not be called statically, assuming $this from incompatible context

toniperic's avatar

Hmm... to be honest not quite sure why as I haven't dealt with mails before in L4 either. Don't want to be guessing a lot, but as the safest way I think it should be

app('mailer')->send($whatever)
dplumptre's avatar

i get this syntax error, unexpected 'class' (T_CLASS),

i have actually worked with emails using L4 without any issue i just wanted to upgrade but i think i will just go back to 4 ,

even now i get laravel 5 when trying to get the files using composer create-project laravel/laravel --prefer-dist, i think theres more explanation that needs to be passed across using laravel 5 as the changes are just so much

dplumptre's avatar

hi toniperic it working now,

all i needed to do was to add use Mail;

to the top of my controller and do the normal Mail::send() , the 'class' (T_CLASS) error was as a result using a variable

baptism-class in the array when sending to the email template,

For the sake of someone with similar issues eg

Mail::send('mail.message-baptismform', array(

'believers-class'=>$request->get('believersclass-completion'), // make sure u dont use hyphen

'believersclass'=>$request->get('believersclass-completion'), // this wont give u issues so you can catch it in your email this

way $believersclass ok.

hey toniperic whats your twitter handle im @dplumptre

1 like

Please or to participate in this conversation.