Troueble to sending email in laravel - laravel-5

I'm trying to send mail with SendGrid, picking up the email that the user wants to send and the message, but I'm having this error when i try to submit:
Failed to authenticate on SMTP server with username "João Gabriel" using 2 possible authenticators. Authenticator LOGIN returned Swift_TransportException: Expected response code 250 but got an empty response in ...
My Mailable:
public $remetente;
public $nome;
public $destinatario;
public $data;
public function __construct($remetente, $nome, $destinatario, $data)
{
$this->remetente = $remetente;
$this->nome = $nome;
$this->destinatario = $destinatario;
$this->data = $data;
}
public function build()
{
//$address = 'janeexampexample#example.com';
$subject = 'This is a demo!';
$name = 'Jane Doe';
return $this->view('emails.test')
->from($this->remetente, $this->nome)
->subject($subject)
->replyTo($this->destinatario, $name);
}
}
My Controller:
public function enviarEmail(Request $request){
$destinatario = $request->input('destinatario');
$mensagem = $request->input('mensagem');
$remetente = \Auth::user()->email;
$nome = \Auth::user()->name;
Mail::to($destinatario)->send(new TestEmail($remetente, $nome, $destinatario, $mensagem));
}
Can someone help me?

Your error message indicates that there is something wrong with the authentication on the SMTP server. There seems to be nothing wrong with your code.
Maybe inspect the error message better, there might be a specific log message somewhere down the stacktrace maybe?
Have you verified your credentials to be correct? Have you verified host and port? Check your .env file and the config/mail.php to see if your settings are correct.

Related

How to send data in the mailgun header with LARAVEL?

I am working with webhooks for the first time, in which I have to pass some 3 variables defined for later when Laravel takes it again I can update an action of the email sent for some reports.
The problem is that I can't pass data in the header of the email.
This is the structure that commonly sent the email to the users:
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($view, $subject, $data)
{
$this->view = $view;
$this->subject = $subject;
$this->data = $data;
}
public function build()
{
$message = $this->data;
// print_r($variables);
// exit;
return $this->from(config('mail.from.address'), config('mail.from.name'))
->view($this->view)
->subject($this->subject); //WORKED
/**NO WORKED*/
->withSwiftMessage(function ($message) use ($v){
$v->getHeaders()
->addTextHeader('Custom-Header', 'HeaderValue1')
->addTextHeader('Custom-Header2', 'HeaderValue2');
});
}
The emails if sent in that there is no problem, with the view and the data that is filled in the mail, but in the header the data is not filled in at least in this case, the 2 variables set ['Custom-Header', 'HeaderValue1', 'Custom-Header', 'HeaderValue2].
I had the same problem too and it took me quite a bit of research and testing. It seems that it needs to be in json format and you need to use 'X-Mailgun-Variables' instead of 'Custom-Header'. It should look like this
->addTextHeader('X-Mailgun-Variables', '{"variable1": "1", "variable2": "2"}')
the webhook should give you this result
"user-variables":{
"variable1":"1",
"variable2":"2"
},

Set email verified to true if password reset is done

I'm trying to set email verified as true if the password reset is completed.
Currently, when a user (email not verified) requests a password reset, it does send an email and the user is able to change password.
As we can confirm that, email in fact belongs to that user, we should be able to set email verified to true. Currently, Laravel doesn't seem to know when an unverified email requests a password reset.
My reset function on ResetPasswordController.php is something like this(overridden to reset function of ResetsPasswords.php)
public function reset(Request $request)
{
$request->validate($this->rules(), $this->validationErrorMessages());
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$response = $this->broker()->reset(
$this->credentials($request),
function ($user, $password) {
$this->resetPassword($user, $password);
}
);
// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
// redirect them back to where they came from with their error message.
return $response == Password::PASSWORD_RESET
? $this->sendResetResponse($request, $response)
: $this->sendResetFailedResponse($request, $response);
}
How can I let laravel know that User now has a verified email?
Thank you
Laravel default "email_verified_at" is indeed a timestamp, so you can handle this in several ways:
in your reset method:
$response = $this->broker()->reset(
$this->credentials($request),
function ($user, $password) {
$this->resetPassword($user, $password);
$user->email_verified_at = Carbon\Carbon::now(); //add this line
$user->save(); //add this line
}
);
Now the user has a valid timestamp and you can "cast" it to a boolean like this in your User model:
On User.php model class:
//Some code
public bool isVerified(){
if(isset($this->email_verified_at)){
return true;
}
else{
return false;
}
}
Now you can use: $user->isVerified() to check if user has verified its email
Hope it helped!

How do I manually send a password reset request in Laravel 5.8

I would like to manually send a password reset request to a specific user (not the one currently logged in) from within a controller. I did some digging around in the Laravel code and I searched many articles but I do not get output.
//...
use Illuminate\Support\Facades\Password;
//...
public function sendResetEmail(Request $request)
{
// I will assueme that you already have $email variable
$response = Password::sendResetLink(['email' => $email], function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
dump('We have e-mailed your password reset link!');
case Password::INVALID_USER:
dump('We can\'t find a user with that e-mail address.');
}
}
You can do it using the Password facade
$email = 'example#domain.com';
$response = \Illuminate\Support\Facades\Password::broker()->sendResetLink($email);
$ok = $response == \Illuminate\Support\Facades\Password::RESET_LINK_SENT;

Auth user not added

This code is work fine but when i use auth::id than show error like this in api Message unauthenticated
API
Route::post('/friend', 'FriendController#index')->middleware('auth');
Working code
public function index(Request $request) {
$sender = Friend::where('sender_id', $request->sender_id)->where('receiver_id',$request->receiver_id)->first();
if(empty($sender)){
Friend::create(['sender_id'=>$request->sender_id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
$response = ['message'=>'Friend Request has been sent','status'=>200];
}else{
$response = ['message'=>'Request has been sent already','status'=>200];
}
return response()->json($response);
}
Not working code, error message unauthenticated
public function index(Request $request) {
//$user = Auth::user()->id;
$sender = Friend::where('sender_id', $request->Auth::user()->id)->where('receiver_id',$request->receiver_id)->first();
if(empty($sender)){
Friend::create(['sender_id'=>$request->Auth::user()->id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
$response = ['message'=>'Friend Request has been sent','status'=>200];
}else{
$response = ['message'=>'Request has been sent already','status'=>200];
}
return response()->json($response);
}
How can i add sender_id is authenticated user id?
auth()->id()
or
$request->user()->id
This will give you id of loggedin User
Also check your middleware it should be
->middleware('auth:api')
You should not use $request->Auth::user()->id to get the id of authenticated user ., Instead use this directly to get id.
Auth::user()->id
Code :
public function index(Request $request) {
//$user = Auth::user()->id;
$sender = Friend::where('sender_id', Auth::user()->id)->where('receiver_id',$request->receiver_id)->first();
if(empty($sender)){
Friend::create(['sender_id'=>Auth::user()->id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
$response = ['message'=>'Friend Request has been sent','status'=>200];
}else{
$response = ['message'=>'Request has been sent already','status'=>200];
}
return response()->json($response);
}
try changing
$request->Auth::user()->id
to
Auth::user()->id

When i am trying to send mail from contactUS form getting this error using swiftmailer in Laravel 5.2

when i am trying to send Mail through Contact Us Form receiving this Error
"Address in mailbox given [] does not comply with RFC 2822, 3.6.2."
I try search to find solution but I cannot find one. I edited config/mail.php
public function sendContactInfo(ContactMeRequest $request)
{
$data = $request->only('name', 'email');
$emailto="******#gmail.com";
$data['messageLines'] = explode("\n", $request->get('message'));
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});
return back()
->withSuccess("Thank you for your message. It has been sent.");
}
with configuration file
i am following this tutorial
Laravel Send Mail
use $data['email']
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto,$data['email']) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});

Resources