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);
});
}
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'm using Swiftmailer to send email. I had installed
+ "guzzlehttp/guzzle": "^6.3"
+ "swiftmailer/swiftmailer": "^6.0".
I create controller and route for it. But it has issue "Call to undefined method Swift_SmtpTransport::newInstance()". Please check my code and guide me how to fix it? Thanks so much
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Swift_Mailer;
use Swift_MailTransport;
use Swift_Message;
class EmailController extends Controller
{
public function sendEmail(){
// Configuration
$smtpAddress = 'smtp.zoho.com';
$port = 465;
$encryption = 'ssl';
$yourEmail = 'ptc#gmail.com';
$yourPassword = '********';
// Prepare transport
$transport = \Swift_SmtpTransport::newInstance($smtpAddress, $port, $encryption)
->setUsername($yourEmail)
->setPassword($yourPassword);
$mailer = \Swift_Mailer::newInstance($transport);
// Prepare content
$view = View::make('email_template', [
'message' => '<h1>Hello World !</h1>'
]);
$html = $view->render();
// Send email
$message = \Swift_Message::newInstance('Test')
->setFrom(['ptc#gmail.com' => 'Our Code World'])
->setTo(["hik#gmail.com" => "mail#mail.com"])
// If you want plain text instead, remove the second paramter of setBody
->setBody($html, 'text/html');
if($mailer->send($message)){
echo "Check your inbox";
}
echo "Something went wrong :(";
}
}
Found in GitHub issues, for SwiftMailer "^6.0"
::newInstance() method has been deprecated, along with
Swift_MailTransport.
Try to change:
\Swift_SmtpTransport::newInstance()
\Swift_Mailer::newInstance()
to:
new \Swift_SmtpTransport();
new \Swift_Mailer()
More information about:
The GitHub issue
Documentation for ^6 version, read Basic Usage
Don't overcomplicate things.
You are rewriting something that comes bundled with laravel.
https://laravel.com/docs/5.5/mail
No need to setup swiftmailer yourself, just use the laravel classes and set the smtp credentials on
config/mail.php
It can be as easy as
Mail::raw('Text to e-mail', function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
Or you can go fancy and use a view to make your template.
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
$message->attach($pathToFile);
});
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');
});
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');
}
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);
});