Laravel How to solve Swift Message attach Data error? - laravel

$pdf = Pdf::loadHTML($html);
Mail::send(array(), array(), function ($message) use ($fullname, $email, $html, $pdf) {
$message->to([$email => $fullname])
->subject('New Test Mail')
->from('noreply#siteaddress.com', 'Test Mail')
->setBody($html, 'text/html')
->attachData($pdf->output(), "text.pdf");
});
I'm doing the above sending email, but I'm getting an error. I tried all the ways but couldn't find a solution. What could be the cause of this problem?
Error Message:
Call to undefined method Swift_Message::attachData()

Just swap setBody method with attachData like this:
$pdf = Pdf::loadHTML($html);
Mail::send(array(), array(), function ($message) use ($fullname, $email, $html, $pdf) {
$message->to([$email => $fullname])
->subject('New Test Mail')
->from('noreply#siteaddress.com', 'Test Mail')
->attachData($pdf->output(), "text.pdf")
->setBody($html, 'text/html');
});
It worked for me.

Related

Laravel 7- "Undefined index: email", when trying to send email

I am trying to send email using Laravel, but I am getting the error message
"Undefined index: email"
Here is my code
public function contact_us(Request $request){
$cu_full_name = $request->cu_full_name;
$cu_email = $request->cu_email;
$cu_message = $request->cu_message;
date_default_timezone_set("Asia/Kolkata");
$cu_received_dt = date("Y-m-d,H:i:s ");
$data= array([
'name'=>$cu_full_name,
'email'=> $cu_email,
'message'=>$cu_message
]);
Mail::send('emails.home',$data,function ($message) use($data){
$message->from('example#example.com');
$message->to($data['email'])->subject('New Message Received.'); **<-error message for this line**
});
//Other codes
Why are you wrapping array inside array ?
try to define data like that :
$data= [
'name'=>$cu_full_name,
'email'=> $cu_email,
'message'=>$cu_message
];
Or,
$data= array(
'name'=> $cu_full_name,
'email'=> $cu_email,
'message'=> $cu_message
);

Undefined variable: toemail Laravel mail issue

I am going to send mail using laravel
so i have
$mailD = DB::table('users')->select('name','email')->where('id', $request->input('Appraiserid'))->get();
$toemail = $mailD[0]->email;
When i echo $toemail it will echo value.
And when i send mails
if ($update) {
$datamail = [
'title' => $getcycle[0]->Heading,
'Heading' => 'Form Reject',
'Name' => $mailD[0]->name,
'email' => $mailD[0]->email
];
Mail::send('voyager::users.send', ["data1" => $datamail], function ($message) {
$message->subject('Form reject');
$message->from('test#gmail.com');
$message->to($toemail);
});
}
Got an error like
ErrorException in Users.php line 631: Undefined variable: toemail
Any help would be appreciated.
try this code for more see
Mail::send('voyager::users.send', ["data1"=>$datamail], function ($message) use ($toemail) {
$message->subject('Form reject');
$message->from('test#gmail.com');
$message->to($toemail);
});

Laravel - sending recovery mail

I'm trying to send a recovery mail using Laravel. I have the following recovery method:
public function recovery(Request $request)
{
$validator = Validator::make($request->only('email'), [
'email' => 'required'
]);
if($validator->fails()) {
throw new ValidationHttpException($validator->errors()->all());
}
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject(Config::get('boilerplate.recovery_email_subject'));
});
switch ($response) {
case Password::RESET_LINK_SENT:
return $this->response->noContent();
case Password::INVALID_USER:
return $this->response->errorNotFound();
}
}
I tried to output $request->email and the reset email is the output, but for some reasons I get the following error:
Undefined index: email
at
"/home/pokemoti/public_html/api/vendor/laravel/framework/src/Illuminate/Auth/Passwords/PasswordBrokerManager.php" on line 74
Any idea what could have gone wrong?
Fixed it by adding the following line in my config/auth.php passwords->users array:
'email' => 'auth.emails.password',
took it from another project where it worked.

Passing data in laravel view mail

im trying to pass more than one variable in my controller to go to the view, but is giving me the error of undifined variable.
I been looking to the documentation and other posts, and still not working.
Controller:
Mail::send('emails.send-references', ['user' => $user,'price'=>$ref->value], function ($m) use ($user) {
$m->from('no-reply#myemal', 'Myname');
$m->to($user->email, $user->name)->subject('My Suject RefMe!');
});
Views mail:
Hello{{$user->name}}blabla:<br>
Ref:{{$refnumber}} <br>
Value: {{$price}}€
You are not sending the $refnumber value to the view:
You can send it like this:
$data = [
'user' => $user,
'price'=>$ref->value,
'refnumber' => 'foo'
];
Mail::send('emails.send-references', $data, function ($m) use ($user) {
$m->from('no-reply#myemal', 'Myname');
$m->to($user->email, $user->name)->subject('My Suject RefMe!');
});
Views mail:
Hello{{$user->name}}blabla:<br>
Ref:{{$refnumber}} <br>
Value: {{$price}}€

codeigniter access model from library

I am trying to integrate the following code into my project. it is held in a library
function do_std_login($email, $password) {
$CI =& get_instance();
$login = $CI->users_model->login($email, md5($password));
if($login){
$session_array = array(
'user_id' => $login->user_id,
'name' => $login->name,
'type' => 'Standard'
);
$CI->session->set_userdata($session_array);
// Update last login time
$CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);
return true;
} else {
$this->errors[] = 'Wrong email address/password combination';
return false;
}
}
I am calling it this way:
$login = $this->jaclogin->do_std_login($this->input->post('email'),$this->input->post('password'));
but when I run it I get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$users_model
Filename: libraries/jaclogin.php
Line Number: 45
I have check I am do load the correct library in the codeigniter autoload file.
Any Ideas?
Thanks
Jamie Norman
Using your CI instance, load your model explicitly in the library like so..
function do_std_login($email, $password) {
$CI =& get_instance();
//--------------
$CI->load->model('users_model'); //<-------Load the Model first
//--------------
$login = $CI->users_model->login($email, md5($password));
if($login){
$session_array = array(
'user_id' => $login->user_id,
'name' => $login->name,
'type' => 'Standard'
);
$CI->session->set_userdata($session_array);
// Update last login time
$CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);
return true;
} else {
$this->errors[] = 'Wrong email address/password combination';
return false;
}
}

Resources