Laravel Mail::send how to pass data to mail View - php

How can i pass data from my Controller to my customized mail View ?
Here's my controller's send mail method :
$data = array($user->pidm, $user->password);
Mail::send('emails.auth.registration', $data , function($message){
$message->to(Input::get('Email'), 'itsFromMe')
->subject('thisIsMySucject');
Here's my emails.auth.registration View
<p>You can login into our system by using login code and password :</p>
<p><b>Your Login Code :</b></p> <!-- I want to put $data value here !-->
<p><b>Your Password :</b></p> <!--I want to put $password value here !-->
<p><b>Click here to login :</b> www.mydomain.com/login</p>
Thanks in advance.

Send data like this.
$data = [
'data' => $user->pidm,
'password' => $user->password
];
You can access it directly as $data and $password in email blade

$data = [
'data' => $user->pidm,
'password' => $user->password
];
second argument of send method passes array $data to view page
Mail::send('emails.auth.registration',["data1"=>$data] , function($message)
Now, in your view page use can use $data as
User name : {{ $data1["data"] }}
password : {{ $data1["password"] }}

for those using the simpleMail this might help :
$message = (new MailMessage)
->subject(Lang::getFromJson('Verify Email Address'))
->line(Lang::getFromJson('Please click the button below to verify your email address.'))
->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
$message->viewData['data'] = $data;
return $message;

The callback argument can be used to further configure the mail. Checkout the following example:
Mail::send('emails.dept_manager_strategic-objectives', ['email' => $email], function ($m) use ($user) {
$m->from('info#primapluse.com', 'BusinessPluse');
$m->to($user, 'admin')->subject('Your Reminder!');
});

Related

How to make resend email link function in Laravel

I have this function that registers users pretty much the default from laravel auth, and i added this send email function. But now im wondering how can i make a function that will send again email if they click "resend link" for example if they didnt recieve the first time.
Register function with the send email:
protected function create(array $data)
{
$user = Account::create([
'login' => $data['login'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'verifyToken'=> Str::random(40),
'active' => (env('CONFIRM_EMAIL', true)) ? 0 : 1
]);
$thisUser = Account::findOrFail($user->id);
$this->sendEmail($thisUser);
return $user;
}
And this is the sendEmail function
public function sendEmail($thisUser){
Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
}
Both functions work well, but sometimes when i register new user i dont get the link i need to delete it from database and re-register it.
Set new verify token on user or even reuse the old one. Then send the email again. Loading the user by email so they don't have to be logged in.
Route::post('users/verify', 'UserController#resend')
protected function resend(Request $request)
{
$user = User::where('email', $request->input('email'))->first();
$user->verifyToken = Str::random(40);
$user->save();
$this->sendEmail($user);
return $user;
}
A very basic example form to call the controller. They need to provide the email, as you don't know which user to resend too.
<form action=" {!! route('resendEmail') !!}" method="POST">
<label for="email">Your email</label>
<input type="text" id="email" name="email" value="example#email.com">
<input type="submit" value="Submit">
</form>

how to pass dynamic content(content which is coming from database) to send mail in laravel 5.4

I m new to laravel.i have table email_template and want to send mail to user when user forgot password.i m fetching content dynamically from database but i dont know how to pass it to mail function in laravel.
Mail::send($posts['email_template'], ['USER' =>$post['user] ], function($message)
{
$message->from('test#gmail.com')->subject('Welcome to laravel');
$message->to('test8#gmail.com');
});
where $posts['email_template'] is a content which i want to send and user is a variable which i want to replace in content
Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
$message->from('test#gmail.com', 'Your Application');
$message->to('test8#gmail.com', $user->name)->subject('Welcome to laravel');
});
emails.template is your view - template.blade.php file - /resources/views/emails/template.blade.php
Now, in your view i.e emails.template, you can do:
{{ $user->name }}, {{ $data->address }}
You can Define
ADMIN_EMAIL and CC_EMAIL in the constant file in the config folder
$emailData = array(
'name'=>'toName',
'toEmail'=>$request->email
);
$this->sendEmail($emailData);
Email Function
function sendEmail($emailData){
$this->adminEmail = config('constant.ADMIN_EMAIL');
$this->ccEmail = config('constant.CC_EMAIL');
$this->toEmail = $emailData['toEmail'];
$this->emailTemplate = $emailData['emailTemplate'];
$data['emailInfo'] = array(
'name'=>$emailData['name']
);
Mail::send('emails.yourTemplate', $data, function ($message) {
//$message->attach($pathToFile);
$message->from($this->adminEmail, 'Laravel Email Test');
$message->to($this->toEmail)->cc($this->ccEmail);
});
}

Cannot show data from controller to email blade

I want to show $data form controller to view but it shows this error message. Do you know why and how to fix that? Thank you.
Here's my code:
UserController.php
public function forgotPassword(Request $request){
$data['user']['email'] = $request->input('user.email');
Mail::send('vendor.notifications.resetpassword', $data, function($message) use($data){
$message->from('bemsadmin#gmail.com');
$message->to($data ['user']['email']);
$message->subject('Your Email');
});
return response()->json($data);
}
resetpassword.blade.php
<!DOCTYPE html>
<html>
<body>
<p>{{$data['user']['email']}}</p>
</body>
</html>
the error happens beacuse you did not pass a variable called $data to the email view,
you pass a variable like this
Mail::send('vendor.notifications.resetpassword', ['data' => $data], function($message) use($data){
but with your code what get passed is
['user' => ['email' => 'email#examle.com']]
so try in your view to use the following code, it should do the job.
{{$user['email']}}

how to send text mail in laravel

i want to send text not view by laravel mail , as i use simple form wth textarea to write the content of message , how to seend text or how to convert this text to view ....... thanks
public function sendmail(){
$sendmessage=Input::get('message');
//$messageView=View::make('messageview')->with('message',$sendmessage);
Mail::send($sendmessage, array('name'=>'hossam'),function($message){
$title=Input::get('title');
$mail=Input::get('mailsender');
$message->from($mail,'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($title);
You can create an email view where the only thing it sends is the contents of a variable:
app/views/emails/nonview.blade.php
{{ $contents }}
Then, you can use this view whenever you want to send an email that doesn't have a view:
public function sendmail() {
$from = Input::get('mailsender');
$subject = Input::get('title');
// 'contents' key in array matches variable name used in view
$data = array(
'contents' => Input::get('message')
);
Mail::send('emails.nonview', $data, function($message) use ($from, $subject) {
$message->from($from, 'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($subject);
});
}

Laravel Reset Password Reminder , passing variables to Blade template

Hi I have the following code in my PasswordController.php but its not working?
// get the user info..
$user = DB::table('users')
->where('email', '=' , Input::get('email'))
->first();
if (empty($user))
return Redirect::back()->with('message', "email doesn't exsist!");
// set email template
Config::set('auth.reminder.email', 'emails.auth.admin-reminder');
$response = Password::remind($credentials, function($message) use ($user){
$message->with('name'=> $user->first_name);
$message->subject(trans('assword Reset'));
});
I am trying to pass $user->first_name to my blade email template? How do I do that?
Thanks
Figured it out ,
Before
Password::remind
we add this :
View::composer('emails.auth.admin-reminder', function($view) use ($user) {
$view->with(['name' => $user->first_name]);
});
You can pass to the view as many variables as you want

Categories