Call to undefined method Illuminate\Notifications\Messages\MailMessage::via() - php

I've added the following to my App\User model:
public function sendPasswordResetNotification($token)
{
$message = (new MailMessage)
->from(config('myapp.email'), config('myapp.title'))
->subject('Reset Password')
->view('emails.password_reset', compact('token'));
$this->notify($message);
}
This is causing the following error:
Call to undefined method Illuminate\Notifications\Messages\MailMessage::via()
?

You need to use Notifications, not Emails. This is why it requires the via method.
If you simply want to send an email, use the Mail facade instead.

Related

PHP: Storing callback functions with arguments on array

I am writing a simple router class which would allow you to execute a function when a route match is found. So, when you are defining routes, you would do something like this:
$message = 'Good morning!';
$router->addRoute('GET', 'welcome', function($message) {
echo $message;
});
Then, in the router class, this gets added to an array like:
public function addRoute($method, $pattern, $handler) {
$this->routes[$pattern] = ['method' => $method, 'handler' => $handler];
}
Once a match is found by comparing the pattern with the requested URL, I have:
return call_user_func($setting['handler']);
This gives me an error: Fatal error: Uncaught ArgumentCountError: Too few arguments to function
...so I tried using:
return call_user_func_array($setting['handler'], array($message));
...but this gives me: Notice: Undefined variable: message
How can I pass a function (including arguments, if existing) to execute within the router class using values stored on an array?
If you don't want to pass $message as an argument at call time, it should not be in the function parameter list. You're probably just looking to use the variable from the surrounding scope:
$message = 'Good morning!';
$router->addRoute('GET', 'welcome', function () use ($message) {
echo $message;
});

Preview Mail Notification in browser

With Laravel, according to the documentation, I can return a Mailable via a controller to display it in the browser. It helps to preview mails.
Is there a way to preview Mail Notifications in browser?
I tried:
return (new MyNotification())->toMail($some_user);
But it does not work:
The Response content must be a string or object implementing __toString(), "object" given.
In your controller's function :
$message = (new \App\Notifications\MyNotification())->toMail('example#gmail.com');
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.notifications.email', $message->data());
Just change the name of the notification class (and also pass arguments if necessary) and hit the url in your browser to see the preview.
You can't render Notification. You can render Mailable that you use in toMail(). For example if that Mailable is called SomeMailable:
public function toMail($user)
{
return (new SomeMailable($user))->to($user->email);
}
Then you can render the Mailable with:
return new SomeMailable($some_user);
In Laravel 5.8 you can now preview it just like you would a Mailable.
Route::get('mail-preview', function () {
return (new MyNotification())->toMail($some_user);
});
More details here:
https://sampo.co.uk/blog/previewing-mail-notifications-in-laravel-just-got-easier
For me, with the particular notification that I wanted to preview toMail() method required a Notifiable instance rather than just an email address, so the following code was what worked for me:
$notification = new \Illuminate\Auth\Notifications\VerifyEmail();
$user = \App\User::where('email', 'example#gmail.com')->first(); // Model with Notifiable trait
$message = $notification->toMail($user);
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.notifications.email', $message->toArray());
Try this (Test pass after Laravel 5.6)
$message = (new \App\Notifications\YourNotification()->toMail($notifiable);
return app()->make(\Illuminate\Mail\Markdown::class)->render($message->markdown, $message->data());

unable to send email in laravel

It shows $data as not defined.
ErrorException in MailController.php line 22:
I tried my best with the help of laravel's tutorial point site. But I was unable to send the mail dynamically.
Below is my mailcontroller.php
MailController.php
public function contact(Request $request)
{
echo $email=$request->input('email');
echo $name=$request->input('name');
echo $message=$request->input('message');
$data = array('name'=>$name,'email'=>$email,'message'=>$message);
Mail::send(['text'=>'mail'], ['data'=>$data], function($message)
{
$message->to('aa#gmail.com',$data->message)->subject
('Feedback');
$message->from($data->email,$data->name);
});
echo "HTML Email Sent. Check your inbox.";
}
Trying using the 'use' parameter inside Mail::send() as follows:
Mail::send(['text'=>'mail'], function($message) use($data) {
$message->to('aa#gmail.com',$data->message)->subject('Feedback');
$message->from($data->email,$data->name);
});

OctoberCMS - Mail::send(), Undefined variables - Laravel

I am trying to send the SAME email to multiple addresses given from an array $emails.
I created a class called SendMail, and inside is a sendPost() method that accepts 2 arguments:
($post, $emails)
Here is my code:
class SendMail {
public static function sendPost($post, $emails)
{
Mail::send('acme.blog::mail.message', $post, function($message) {
$message->to($emails);
$message->from('mail#compuflexcorp.com', 'Compuflex Mail');
$message->subject($post['subject']);
$message->replyTo($post['email']);
});
}
}
The problem is, I keep receiving an error:
"Undefined variable $emails" on Line 14 of C:\...\SendMail.php
Line 14: $message->to($emails);
What I have tried:
I checked to see if I can access the $post and $emails variables inside of sendPost(), but outside of Mail::send(). And the answer is YES, I can access the information inside of $post and $emails inside of sendPost(), so the variables are, in fact, being passed to the sendPost() method.
I, at first, thought it had something to do with the fact that $emails is not one of the arguments for Mail::send(), so I put $post and $emails into one array called $vars, but then I got the error:
"Undefined variable $vars" on Line 14 of C:\...\SendMail.php
So, I realized that the issue seems to be that I can't pass any variables to Mail::send(), or in other words, I just don't know how to...
Any help would be greatly appreciated...
Thomas Yamakaitis
You need to pass the $emails variable as follows:
class SendMail {
public static function sendPost($post, $emails)
{
Mail::send('acme.blog::mail.message', $post, function($message) use ($emails) {
$message->to($emails);
$message->from('mail#compuflexcorp.com', 'Compuflex Mail');
$message->subject($post['subject']);
$message->replyTo($post['email']);
});
}
}

Mailgun sending email laravel

i use mailgun and the setting is done and i've test it and work, but i dont understand why i can't send email without array, here i tried using array but idk why it's error said Undefined variable: data
public function kirim(Request $request){
$data = array(
'email_address'=>$request->email_address,
'cc'=>$request->cc,
'subject'=>$request->subject,
'keterangantambahan'=>$request->keterangantambahan
);
Mail::send('laporan.kirim', $data, function($message) {
$message->from('christian7andrew#gmail.com', 'PuraBox');
$message->to($data['email_address']);
});
return redirect('/');
}
any idea how to use array corectly ??
Use a use.
Looks like you are using a php version which supports closures
Mail::send('laporan.kirim', $data, function($message) use ($data) {
$message->from('christian7andrew#gmail.com', 'PuraBox');
$message->to($data['email_address']);
});
The second parameter of the send() method is to set mail options. Does not place the variable inside the function body.
The use puts variables into the body of the function

Categories