Laravel Call to a member function send() on a non-object - php

I am trying to send a mail using my laravel controller, a very simple mail which is sent in my localhost with no problems but once in server I get this error :
20170325T153701: /public/index.php
PHP Fatal error: Call to a member function send() on a non-object in /public/index.php on line 56
PHP Fatal error: Call to a member function send() on a non-object in /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php on line 107
The browser only shows a 500 error, the error is only seen in my error log. Here is the code used to send the mail
use Illuminate\Support\Facades\Mail; // just to say I'm calling it
$v_code = str_random(30);
$mail_content = array('code' => $v_code);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['pass']),
]);
if ($user) {
Mail::send('User::mail.verifyMail', $mail_content, function ($message) use ($data) {
$message->to($data['email'], $data['name'])
->subject('Verify your email address');
});

The solution was on casting the $name with a (string). I don't know why, but anytime the name contains a space (' ') it simply fails and the $name is considered as null. Thus, a (string) resolved this.

Remove that use line.
\Mail::send('User::mail.verifyMail', $mail_content, function ($message) use ($data) {
$message->to($data['email'], $data['name'])
->subject('Verify your email address');
});

Related

Exception in mail queue send function

In my app I am trying to send an email using Mail::queue().
I get an exception saying that serialization of closure failed.
ErrorException in SerializableClosure.php line 93: Serialization of
closure failed: Serialization of 'Closure' is not allowed
I have a this as the send function:
public function send()
{
$view = view('emails.welcome');
$data = [
'user' => Auth::user()
];
return $this->mailer->queue($view, $data, function($message){
$message->to($this->to)->subject($this->subject);
});
}
I've only recently begun using Laravel so any help would be great.
The issue it that you're trying to use $this inside Closure.
Please provide parameters $to and $subject using use keyword like in this example:
return $this->mailer->queue($view, $data, function($message) use ($to, $subject) {
$message->to($to)->subject($subject);
});
The issue is using $this inside of the closure.
$this->to and $this->subject are references to fields on the Class and not in the Closure so to fix the code make them local variables and pass them to closure like as below:
public function send()
{
$to = $this->getTo();
$subject = $this->getSubject();
return $this->mailer->queue( $this->getView(), $this->getData(), $this->getData(),
function($message) use($to, $subject) {
$message->to($to)->subject($subject);
});
}

Testing message passed to view

How can I test that I get this message:
public function doSomething()
{
if($ok){
return view('message-page')
->with('title','The message?')
}
}
What can I assert to check the message that is passed to the view?
Updated:
Laravel 5.5
I think it is tricky because I am not doing a HTTP call, which would return a response. I am just doing a function call ($foo->doSomething();), so I don't get a response returned.
I can't do a GET because I need to pass in a mocked object. Here is my test so far:
public function do_the_test()
{
//Arrange
$mockObject = Mockery::mock('App\MockObject');
$authCode = 123456;
$mockObject->shouldReceive('doSomething')->once()->with($authCode)->andReturn([
'code' => '123456',
'name' => 'joe',
'username' => 'smith',
'email' => 'joe#yahoo.co.uk',
'domain' => 'yahoo.co.uk'
]);
//Act
$object = new Foo($mockObject);
$object->doSomething();
//Assert
??
//check that view is returned with message text
}
Progress:
I have hacked this by setting a session variable (instead of passing the messages with the view) and then checking that with assertEquals();
Would be nice to find a better way.
Not sure which laravel version you have, but still, you can use assertViewHas($key, $value) function:
public function testViewDoSomethingHasCorrectTitle()
{
$response = $this->call('GET', '/my_route');
$this->assertViewHas('title', 'The message?')
}
https://laravel.com/docs/5.5/http-tests#assert-view-has
just do a dd() and you'll know if it passed to the controller.
In your view do a {{ dd($title) }}

Laravel : I can't use Request object two times at the same function

I'm trying to build contact form and I want it to send the user message for the website email and i want it to send message for the user mail inform him that his message received so I'm using this code in controller :
public function mail(Request $request) {
Mail::send('mail.mail', ['name'=>"$request->name" , 'email'=>"$request->email" , 'msg'=>"$request->message"], function($message) {
$message->to('housma.elma#gmail.com', 'Housma')->subject('Housma.com enquiry');
});
Mail::send('mail.mailResponse', ['name'=>"$request->name" ], function($message ) {
/*line 29 */
$message->to("$request->email", "$request->name")->subject('Housma.com : Auto reply');
});
return Redirect::to('/contact')->with('successful', 'Your message has been sent');
}
The first message for my email is working fine, but when Laravel reaches the second message, I get this error
ErrorException in pagesController.php line 29: Undefined variable: request
It's not that you can't use it twice, but that the Mail::send can't access it. You need to pass it in with the use statement:
Mail::send('mail.mailResponse', ['name'=>"$request->name" ], function($message ) use ($request) {
Replace line 28 with
Mail::send('mail.mailResponse', ['name'=>"$request->name" ],
function($message) use($request) {
In PHP, if you want to use a variable in a closure, you need to use use ($variablename)
May be you should pass $request to the closure.
like this !
Mail::send('mail.mailResponse', ['name'=>"$request->name" ], function($message ) use ($request) {
/*line 29 */ $message->to("$request->email", "$request->name")->subject('Housma.com : Auto reply');
});
return Redirect::to('/contact')->with('successful', 'Your message has been sent');
}

Laravel 4 Email Error

I have been trying to send Email from Laravel and I am getting this error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException syntax error, unexpected '$message' (T_VARIABLE)
Here is my code:
class SubmitBooking extends \BaseController {
public function submit() {
$data = array('name' => 'test', 'message' => 'test message');
Mail::pretend('emails.booking', $data, function($message)
{
$message->to("me#gmail.com", "Name Name")->subject('Welcome to My Laravel app!');
$message->from("from#maldivehosting.net", "Name Name");
  });
}
}
I am using PHP latest version, I have tested this on local and and site5 hosting too.
Somebody please help me!
Per the Laravel documention, you cannot use $message when creating an email. Try $msg instead
$data = array('name' => 'test', 'msg' => 'test message');
I don't know why you are using Mail::pretend to send your e-mail, it must be a typo. The correct syntax should be:
Mail::send('emails.booking', $data, function($msg)
{
$msg->to("me#gmail.com", "Name Name")->subject('Welcome to My Laravel app!');
$msg->from("from#maldivehosting.net", "Name Name");
});

Laravel Mail queues with Iron.io

I have been able to get my Iron.io push queue subscription to work great, but not with mail queues for some reason. In the code below I am pushing a job onto the queue with an email address and username in the $data array. For reasons unknown to me, the email address and username are not getting passed to the Mail function. The job just sits there, failing to send.
FooController.php
<?php
$data = array(
'email_address' => Input::get('email'),
'username' => Input::get('username'),
);
Queue::push('SendEmail#reserve', $data);
routes.php
<?php
// iron.io push queue path
Route::post('queue/push', function()
{
return Queue::marshal();
});
class SendEmail
{
public function reserve($job, $data)
{
Mail::send('emails.reserve', $data, function($message)
{
$message->to($data['email_address'], $data['username'])->subject($data['username'] . ', welcome to RockedIn!');
});
$job->delete();
}
}
?>
You need to pass $data to the closure.
public function reserve($job, $data)
{
Mail::send('emails.reserve', $data, function($message) use ($data)
{
$message->to($data['email_address'], $data['username'])->subject($data['username'] . ', welcome to RockedIn!');
});
$job->delete();
}

Categories