Why am I getting undefined variable errors in Laravel method? - php

I have created a Laravel method in my PageController to send emails when form data is sent like the following:
public function sendMessage(Request $request)
{
$name = $request->input('name');
$email = $request->input('email');
$message_content = $request->input('message');
// email message
Mail::raw($message_content, function ($message)
{
$message->from($email, $name);
// $message->to(env('APP_ADMIN_EMAIL'));
$message->to("myemail#mail.com");
$message->subject('Website Message');
});
return "message sent";
}
But I get this error when the method is called:
ErrorException Undefined variable: email
Can someone explain what I'm doing wrong?

You need to pass the variables to the closure:
Mail::raw($message_content, function ($message) use ($email, $name)
{
$message->from($email, $name);
$message->to("myemail#mail.com");
$message->subject('Website Message');
});
See docs: https://secure.php.net/manual/en/functions.anonymous.php

Related

email function not passing variable to view

i want to pass a variable $data to my email views but i get undefined variable.
this is the controller method
public function broadcastemail(Request $request)
{
$this->validate($request,
[
'subject' => 'required',
'emailMessage' => 'required'
]);
$emailMessage = $request->emailMessage;
$data['emailMessage'] = $emailMessage;
Mail::send('backend.user.emailMessage', $data, function($message)
{
$subject = request()->subject;
$user = User::find('31');
$email = $user->email;
$name = $user->first_name;
$message->to($email, $name)->subject($subject)->with('data',$data);
});
//Mail::to($to)->send($data);
//send_email($to, $name, $subject, $message);
return back()->withSuccess('Mail Sent Successfuly');
}
and this is my view
<p>{{$data['emailMessage']}}</p>
try to use {{ $emailMessage }} instead of {{$data['emailMessage']}} in your view and use keyword for using inside the closure function of MAIL

unreachable statement in laravel when executing a function

Am trying to execute a send mail function when a record is stored in the database but am getting unreachable statement
public function store(Request $request)
{
$visit = Visit::create($request->all());
return response()->json($visit);
$this->sendEmail($request);
}
This is the send email function
public function sendEmail(Request $request){
$visit = Visit::create($request->all());
$host_email = Db::table('users')
->where('name', '=', $visit->visitor_host)
->value('email');
$to_name = $request->input('visitor_name');
$data = array('name'=> $to_name, "body" => "Test mail");
Mail::send('mails.mail', $data, function($message) {
$message->from('cytonnvisitor#gmail.com','cytonn');
$message->to('mimikiduchu#gmail.com');
$message->subject('Visitor coming notification');
});
}
From the manual:
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call.
Change your code to
$this->sendEmail($request);
return response()->json($visit);
to execute the mail send before your return.
Enter return response()->json($visit); at the end of the store function.

Show the subject and message sending email with Mail::to

I have the code below to send emails using the Mail::to function. But Im not understanding how to set the subject and body of the message with the Mail::to function. I have the code below that is working to send emails but without subject and the $message is also not appearing in the email.
Do you know how to properly achieve that? (Have subject and the $request->message in the email using Mail::to)
public function send(Request $request, $id){
$conference = Conference::find($id);
if($request->send_to == "participant"){
// if is to send only 1 email the Mail::to is called directly here
Mail::to($request->participant_email)->send(new Notification($conference));
return;
}
if($request->send_to == "all"){
// $sendTo = query to get a set of emails to send the email
}
else{
// $sendTo = query to get a another set of emails to send the email
}
foreach($sendTo as $user){
$usersEmail[] = $user->email;
}
$message = $request->message;
$subject = $request->subject;
foreach ($usersEmail as $userEmail){
Mail::to($userEmail)->send(new Notification($conference, $message));
}
}
In the class Notification I have:
class Notification extends Mailable
{
public $conference;
public function __construct(Conference $conference)
{
$this->conference = $conference;
}
public function build()
{
return $this->markdown('emails.notification');
}
}
In the view notifications.blade.php I have:
#component('mail::message')
# Notification relative to {{$conference->name}}
{{$message}}
Thanks,<br>
{{ config('app.name') }}
#endcomponent
Try something like this:
$emailData = array(
/* Email data */
'email' => 'user#email.com',
'name' => 'User name',
'subject' => 'Email subject',
);
Mail::send('emails.template_name', ['emailData' => $emailData], function ($m) use ($emailData) { // here it is a closure function, in which $emailData data is available in $m
$m->from('info#domain.com', 'Domain Name');
$m->to($emailData['email'], $emailData['name'])->subject($emailData['subject']);
});
try
{
Mail::send('emails.contact_form', ['data' => $data],
function($message) use ($data)
{
$message->from('emailasinenv#hosting.com', 'ShortName');
$message->to( $data['adminEmail'] )->subject("Contact Form" );
});
return true;
}
catch (\Exception $ex) {
$ex->getMessage();
return false;
}
As you already have one template in your code use that template, Pass the message to template
$subject = 'Email Subject';
Mail::send('emails.notification', ['message' => $message], function ($mail) use ($userEmail, $subject) {
$mail->from('info#domain.com', 'Domain Name');
$mail->to($userEmail)->subject($subject);
});

Mail not sending in laravel 5 from an ajax post request

I am working on forgot password functionality in my laravel 5.0 application. I am asking user to enter registered email and submit(post method) this form works on jquery ajax post request. After getting email from request i am checking it in db whether it exist or not. if exist i am updating new password, if not sending an error message upto this every this is fine. Now i am trying to send email with new password to the user, it is not working. Password is updating but email not sending.Find my controller code below
public function forgot_pass(Request $request){
$email = $request->input('femail');
$data = ['email' => $email];
$user = Myuser::where($data)->count();
if($user == 0){
return response()->json(['status' => 'Invalid']);
}
else{
$user_details = Myuser::where($data)->get()->first();
$new_pass = $this->generateRandomString();
$md5_pass = md5("EEE".$new_pass);
$status = Myuser::where('email', $email)
->update(['pass' => $md5_pass]);
if($status){
$mail_data = ['name' => $user_details->name, 'new_pass' => $new_pass];
Mail::send('emails.newpassword', $mail_data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to($email);
});
return response()->json(['status' => 'Invalid']);
}
}
}
Actually mail is working if i used $message->to('sometest#testmail.com'); instead of $message->to($email);. Below is the error i am getting.
Feeling strange why $email causing error.
You have to pass the $email to the anonymous function
Mail::send('emails.newpassword', $mail_data, function($message) use ($email) {
...
});

Email class - CodeIgniter PHP - Send email to multiple members

I have been trying to send an email to multiple members within my database. I managed to get the application to send one email to specific users emails, but when I use a list function I get errors (undefined index) that are supposedly in the libraries/email.php file but as far as I am aware I should not have to change the email library file. I am working on my localhost and here is the code:
function send(){
$this->load->model('mail_model', 'emails');
$emails=$this->emails->get_emails();
foreach($emails as $row){
if($row['email']){
$this->email->set_newline("\r\n");
$this->email->from('myemail#test.com', 'my name');
$this->email->to($row['email']);
$this->email->subject('Test multiple receivers');
$this->email->message('Test');
$this->email->send();
echo $this->email->print_debugger();
$this->email->clear();
$path = $this->config->item('server_root');
}
if($this->email->send())
{
//echo 'Your email was sent.';
$data['main_content'] = 'signup_confirmation';
$this->load->view('includes/template', $data);
}
else
{
show_error($this->email->print_debugger());
}
}
}
I have already autoloaded the email library in the config files. This is the code from the model:
class Mail_model extends CI_Model
{
function get($id = null)
{
$this->db->select('mailing_member_id', 'mailing_list_id', 'email_address');
$this->db->from('mailing_members');
if (!is_null($id)) $this->db->where('mailing_member_id', $id);
$this->db->order_by('mailing_member_id', 'desc');
return $this->db->get()->result();
}
Your model's get()method returns an object instead of an array. Try:
return $this->db->get()->result_array();
Also, you need $row['email_address']not $row['email'] for the ifstatement.

Categories