email in laravel not working - php

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)

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);
});
}

I am trying to send email in Laravel 5.2. It giving me erros as "InvalidArgumentException in Mailer.php line 410: Callback is not valid."

I have provided host ,username ,password,port no. in .env and config/mail.php. I am sending email from localhost.
public function sendEmail(){
$data = array('name'=>"Swati Jadhav");
$arrCategory=Category::getCategory();
Mail::send('frontend.welcome',['arrCategory'=>$arrCategory] ,$data, function($message) {
$message->from('sender#gmail.com', 'text');
$message->to('receiver#gmail.com', 'text')->subject('Welcome!');
});
}
Mail::send needs the callback as the 3rd parameter (not 4th). If you need to combine two parameter arrays you can do:
Mail::send('frontend.welcome',array_merge(['arrCategory'=>$arrCategory] ,$data), function($message) {
$message->from('sender#gmail.com', 'text');
$message->to('receiver#gmail.com', 'text')->subject('Welcome!');
});

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');
}

email sends whole html page(instead of email body) in laravel 5.1

I'm making a contact form. If someone fills in the form, the data (text,subject) should be sent to my mail. Everything works, but when I fill in form and send the email, it sends the whole HTML page (not just the text from the form).
Here's how it looks like
Here's my controller where I send the mail
public function sendEmail(Request $request)
{
$data["username"] = $request->username;
$data["email"] = $request->email;
$data["subject"] = $request->subject;
$data["mail_text"] = $request->mail_text;
Mail::send('contact', ["data" => $data], function ($mail) use ($data){
$mail->to("leribobo#gmail.com")->from($data["email"])->subject($data["subject"]);
});
return redirect()->back();
}
as I guess it sends 'contact' page, which is first parameter in method
Mail::send('contact' ....
You are actually sending the contact.blade.php file. This is not how we should send a mail.
Mail::send('emails.contact', $data, function ($message) {
//
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
You've to make a file in emails.contact.blade.php and pass $data to the blade file as showed above.
Check here: https://laravel.com/docs/5.1/mail#sending-mail

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