Laravel Not sending Email - php

am having some sort of an issue with sending mails from my website with Laravel 4, I followed the on-line documentation but haven't successfully sent a mail with Laravel my code is as show below.
Mail::send('emails.contactmail', $data, function($message)
{
$name = Input::get('name');
$email = Input::get('email');
$message->from($email, $name);
$message->to('info#mysite.org', 'Info at My Site')
->subject('Website contact form');
});

I use following code for sending temporary password in case of forgot password
public function postForgotPassword(ForgotPasswordRequest $request){
$email=$request->email;
$objUser=DB::table('users')
->where('email','=',$email)
->select('email','id','first_name','last_name','user_group_id')
->first();
$string = str_random(15);
$pass=\Hash::make($string);
$objUser2=User::find($objUser->id);
$CURRENT_TIMESTAMP=new DateTime();
$objUser2->temporary_pass=$pass;
$objUser2->pass_status=1;
$objUser2->updated_at=$CURRENT_TIMESTAMP;
$objUser2->save();
$data=array("email"=>$email,"pass"=>$string,"first_name"=>$objUser->first_name,"last_name"=>$objUser->last_name);
$email=array("email"=>$email);
Mail::send('emails.forgot_password',$data, function($message) use($email) {
$message->to($email['email'],'MyProject')->subject('Password Recovery ');
});
return Redirect::to('auth/login')->with('flash_message','Check your email account for temporary password');
}
And I write email template in forgot_password.blade.php which is located in view.emails folder.

Related

Laravel Mail, "from" always need to be the same as "username"?

If I'm firing an e-mail from mysite#mysite.com.br but if I change the from to another e-mail like company#company.com.br it falls into Mail::failures()
It DOESN'T Works
// username = 'mysite#mysite.com.br'
// password = 'mypassword'
Mail::send("mail.test", [], function ($message) {
$message->to('client#client.com')
->from('company#company.com' , 'Company');
});
client#client.com falls into Mail::failures()
It Works
but if I change it to:
// username = 'mysite#mysite.com.br'
// password = 'mypassword'
Mail::send("mail.test", [], function ($message) {
$message->to('client#client.com')
->from('mysite#mysite.com.br' , 'My Site');
});
The problem was in my mail server, I talked with the support from my server and he said It its block for fraude reasons

Laravel how to pass the name of the user when I sent the forgot password function

I want to make my mail more detailed when the user has sent a forgot password reset link to his/her email. This is the sample of the picture when receiving a reset password link.
I want to add some details here that the Hello should be Hello! (user name here)
Here is the code that I added in my SendsPasswordResetEmails.php
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email')
);
$applicant_name = Applicant::where('email', $request->email)->get()->value('name');
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
}
and it should pass the data to app\Notifications\ApplicantResetPasswordNotification.php
public function toMail($notifiable)
{
return (new MailMessage)
->from('vcc3dummy#gmail.com', 'CCV3')
->greeting('Hello! Applicant Name') // Applicant name pass here
->line('You are receiving this email because we received a password request for your account.')
->action('Click here to Reset Password', route('applicant.reset', $this->token))
->line('If you did not reset your password, no further action is required.');
}
Looking for help on how to pass the data or how to query it.
Would appreciate if someone could help me
Thanks in advance.
In your ApplicationResetPasswordNotification.php you can use the $notifiable variable as follows:
public function toMail($notifiable)
{
return (new MailMessage)
->from('vcc3dummy#gmail.com', 'CCV3')
->greeting('Hello!' . $notifiable->name) // Applicant name
...
}
Please mark as answer if that works for you!
This is the another way to send the mail in laravel -
Put that data you want to use/show in email template.
$data = [
'email' => $email,
'remember_token' => $remember_token,
'name' => $applicant_name
];
Mail::send('emails/forgotmail', $data, function ($message) use ($data) {
$message->from('youremail#gmail.com');
$message->to( $data['email'] )->subject('Forgot Password Link');
});
Where as 'email/forgotmail' is in 'resources/views/email/forgotmail.blade.php' that you have to create. So that here you can put your email template over here and make use of $data in it .

How to check email sent or not in laravel 5

I have a function to send a email for registered users. This is how i am checking if an email should be sent or not.
$email=$details->email;
$subject = 'Looking for blood donor';
$status=Mail::send('emails.welcome', $data, function($message)
use($subject,$email){
$message->from('no-reply#bloodlink.com', 'Blood Link');
$message->bcc('no-reply#bloodlink.com');
$message->to($email)->subject($subject);
});
I am using if for check email sent or not but it not work..
if($status)
{
return Response::json(array('status'=>'success',
'data'=>("Your email has been sent successfully")
), 200);
}else{
return Response::json(array('status'=>'error',
'data'=>("something went wrong..!!")
), 200);
}
The Mail::send() method doesn't return anything.
You can use the Mail::failures() (introduced in 4.1 I think) method to get an array of failed recipients, in your code it would look something like this.
Mail::send('emails.users.reset', compact('user', 'code'), function($m) use ($user)
{
$m->to($user->email)->subject('Activate Your Account');
});
if(count(Mail::failures()) > 0){
$errors = 'Failed to send password reset email, please try again.';
}
Have a look into failure method, here

how to edit email body before sending using Laravel

I have generated email function, that sends email to multiple people using laravel.
Now I want to generate an edit window so that i can write the body of email,like in Gmail, if we are sending mail, we first edit body and hit send mail.
So, if anyone know how can I implement this, leave a comment.
It should be as simple as
Mail::send([], array('yourValue' => $yourValue), function($message) use ($yourValue) {
$MailBody = 'Your Custom Body';
$message->setBody($MailBody, 'text/html');
$message->to('yourtoaddress#yourdomain.com');
$message->subject('Your Custom Subject');
});
Though I am fairly new to Laravel myself, I could try to help you out with this. Firstly, set up your routes in the Routes.php file. For e.g.
Route::get('myapp/sendEmail', 'EmailController#returnComposeEmail');
Route::post('myapp/sendEmail', 'EmailController#sendEmail');
The first route when visited should return a view to the user where he can compose his email. This basically is a form which will be submitted by the POST method when the user clicks the 'Send' button. The second route is for that method which would collect the submitted data and use it appropriately then to send the email.
If you go by the routes I have provided, you should have a controller file named EmailController.php with the following methods:
public function returnComposeEmail()
{
return view('pages.ComposeEmail');
}
public function sendEmail(Request $input)
{
$input = $input->all();
$dataArray = array();
$dataArray['emailBody'] = $input['emailBody'];
$to = $input['to'];
$subject = $input['subject'];
Mail::send('email.body', ['dataArray' => $dataArray], function ($instance) use ($to, $subject)
{
$instance->from(env('MAIL_USERNAME'), 'Your Name Here');
$instance->to($to, 'Recipient Name');
$instance->subject($subject);
$instance->replyTo(env('MAIL_REPLY_TO', 'some#email.id'), 'Desired Name');
});
}
You may use use the $dataArray in the email/body.blade.php file as is or as per your requirement.
Do let me know if I could be of help. :-)
Controller:
public function showForm(Request $request )
{
//Get Content From The Form
$name = $request->input('name');
$email = Input::get('agree');
$message = $request->input('message');
//Make a Data Array
$data = array(
'name' => $name,
'email' => $email,
'message' => $message
);
//Convert the view into a string
$emailView = View::make('contactemail')->with('data', $data);
$contents = (string) $emailView;
//Store the content on a file with .blad.php extension in the view/email folder
$myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
fwrite($myfile, $contents);
fclose($myfile);
//Use the create file as view for Mail function and send the email
Mail::send('emails.email', $data, function($message) use ($data) {
$message->to( $data['email'], 'Engage')->from('stifan#xyz.com')->subject('A Very Warm Welcome');
});
// return view();
}
Routes:
Route::post('contactform', 'ClientsController#showForm');
Route::get('/', 'ClientsController#profile');
The view contactemail has the data to be sent, and the view email we are sending through mail function. When the user puts data in the form, that data will get saved in email.blade.php because of these lines of code:
//Convert the view into a string
$emailView = View::make('contactemail')->with('data', $data);
$contents = (string) $emailView;
//Store the content on a file with .blad.php extension in the view/email folder
$myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
fwrite($myfile, $contents);
fclose($myfile);

laravel 5 send email with html elements and attachment using ajax

I am stuck on the point where i want to send an email using ajax. Find code below.
$user = \Auth::user();
Mail::send('emails.reminder', ['user' => $user], function ($message) use ($user) {
$message->from('xyz#gmail.com', 'From');
$message->to($request['to'], $name = null);
// $message->cc($address, $name = null);
// $message->bcc($address, $name = null);
$message->replyTo('xyz#gmail.com', 'Sender');
$message->subject($request['subject']);
// $message->priority($level);
if(isset($request['attachment'])){
$message->attach($request['attachment'], $options = []);
}
// Attach a file from a raw $data string...
$message->attachData($request['message'], $name = 'Dummy name', $options = []);
// Get the underlying SwiftMailer message instance...
$message->getSwiftMessage();
});
return \Response::json(['response' => 200]);
Now i want to send this email using ajax and upon request complete i want to display message on same page that your message sent successfully.
i found the solution for this, but this is to send plain message directly using Mail::raw() function which is working perfectly. But i am not able to attach files here or some html codes.
Any suggestion in this regard.

Categories