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");
});
Related
I am trying to use Mail function in Laravel. Heres the code
public function basic_email(){
$data = array('name'=>"Virat Gandhi");
Mail::send(['text'=>'mail'], $data, function($message) {
$message->to('shanipasrooria#gmail.com', 'Tutorials Point')->subject
('Laravel Basic Testing Mail');
$message->from('m.usman5991#gmail.com','Virat Gandhi');
});
echo "Basic Email Sent. Check your inbox.";
}
I have made changes in .env file. Set everything, Heres my route.
Route::get('sendbasicemail','MailController#basic_email');
I get the following Error.
InvalidArgumentException in FileViewFinder.php line 137:
View [mail] not found.
You can try this code
Mail::send([], [], function ($message) {
$message->to('shanipasrooria#gmail.com', 'Tutorials Point')
->subject('subject')
->setBody('some body', 'text/html');
});
you can try this
$html = '<h1>Hi, welcome Virat!</h1>';
Mail::send([], [], function ($message) use ($html) {
$message->to('shanipasrooria#gmail.com', 'Tutorials Point')
->subject('Laravel Basic Testing Mail')
->from('m.usman5991#gmail.com','Virat Gandhi')
->setBody($html, 'text/html'); //html body
or
->setBody('Hi, welcome Virat!'); //for text body
});
Mail::send(['text'=>'mail']< here the mail should be a valid view file.
According to API Documentation, the Mailer Class should receive a String, Array Or MailableContract, Those reference a view. So you need to pass a valid view in the send method.
void send(string|array|MailableContract $view, array $data = [], Closure|string $callback = null)
I have a contact form where someone provides his name and email. I want to send him an email now with Laravel.
I found in the docs
To send a message, use the to method on the Mail facade. The to method
accepts an email address, a user instance, or a collection of users.
and in fact
\Mail::to('example#gmail.com')->send(new \App\Mail\Hello);
works. But is it also possible to provide the name for the email receipt?
I wanted to look that up in the Laravel API for the Mail Facade but to my surprise the facade has no to function?
So how can I find out what the to function really does and if I can pass a name parameter as well?
In laravel 5.6, answer to your question is: use associative array for every recpient with 'email' and 'name' keys, should work with $to, $cc, $bcc
$to = [
[
'email' => $email,
'name' => $name,
]
];
\Mail::to($to)->send(new \App\Mail\Hello);
You can use the Mail::send() function that inject a Message class in the callable. The Message class has a function to($email, $name) with the signature you're searching, i.e.:
Mail::send($view, $data, function($message) use ($email, $name) {
$m->to($email, $name);
$m->from('youremail#example.com', 'Your Name');
$m->subject('Hi there');
})
The $view could be a string (an actual view) or an array like these:
['text'=> 'body here']
['html'=> 'body here']
['raw'=> 'body here']
The $data argument will be passed to the $view.
For Laravel < 5.6 one can use this:
$object = new \stdClass();
$object->email = $email;
$object->name = $user->getName();
\Mail::to($object)->queue($mailclass);
see here
I prefer this solution as more readable (no need to use arrays and static string keys).
\Mail::send((new \App\Mail\Hello)
->to('example#gmail.com', 'John Doe');
You can use Mailable class in Laravel:
https://laravel.com/docs/5.5/mail
php artisan make:mail YouMail
These classes are stored in the app/Mail directory.
In config/mail.php you can configue email settings:
'from' => ['address' => 'example#example.com', 'name' => 'App Name'],
for Laravel 8 is like this:
$user = new User;
$user->email = 'example#example.com';
Mail::to($user)->send(new YourMail);
YourMail is Mailable class created by php artisan make:mail YourMail
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');
});
When I'm trying to send an email using the swiftmailer in laravel I get the folowing error: "Missing argument 2 for UsersController::{closure}()".
My code is below:
Mail::send('emails.default', array('key' => Config::get('settings.WELCOMEMAIL')), function($message, $mail, $subject = 'Welcome!')
{
$message->to($mail)->subject($subject);
});
It's really weird though. The $mail variable contains a valid email address and I'm not using the UsersController at all in this script.
Thanks in advance
You must pass only the $message to the closure. Any additional variable must be passed down with the use keyword:
Mail::send('emails.default', array('key' => Config::get('settings.WELCOMEMAIL')), function($message) use($mail, $subject)
{
$subject = empty($subject) ? 'Welcome!' : $subject;
$message->to($mail)->subject($subject);
});
I'm very new to Laravel and unit testing in general. I'm trying to write some tests for my AccountController and I've run into a road block.
I'm using Sentry to handle users and groups in the site. I'm trying to test that my controller is handling exceptions thrown by Sentry properly. So my controller method that handles the login POST looks like this:
public function postLogin(){
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
try{
$user = $this->authRepo->authenticate($credentials, true);
return Redirect::route('get_posts');
}
catch (Exception $e){
$message = $this->getLoginErrorMessage($e);
return View::make('login', array('errorMsg' => $message));
}
}
authRepository is just a repository that uses Sentry to handle authentication. Now I want to test that when an email address is not specified a LoginRequiredException is thrown and the user sees the error message. Here is my test:
public function testPostLoginNoEmailSpecified(){
$args = array(
'email' => 'test#test.com'
);
$this->authMock
->shouldReceive('authenticate')
->once()
->andThrow(new Cartalyst\Sentry\Users\LoginRequiredException);
$this->action('POST', 'MyApp\Controllers\AccountController#postLogin', $args);
$this->assertViewHas('errorMsg', 'Please enter your email address.');
}
However, the test is not passing. For some reason all it spits out is:
There was 1 error:
1) AccountControllerTest::testPostLoginNoEmailSpecified
Cartalyst\Sentry\Users\LoginRequiredException:
Am I using the andThrow() method incorrectly? If anyone can shed any light on what is going on it would be much appreciated.
Thanks in advance!
So I actually just figured the problem out. Turns out it wasn't a problem with my unit tests at all but was actually just a namespacing issue. I forgot the backslash on the Exception class. So in my controller it should have been:
try{
$user = $this->authRepo->authenticate($credentials, true);
return Redirect::route('get_posts');
}
catch (\Exception $e){
$message = $this->getLoginErrorMessage($e);
return View::make('account.login', array('errorMsg' => $message));
}