phplaravelemailpyrocms

Laravel - Unable to send Email to webserver from website


I am trying to send a mail through my contact from and i am getting this error:

Swift_TransportException (550) Expected response code 250 but got code "550", with message "550-Your FROM address ( test@email.com , test@email.com ) must match your 550-authenticated email user ( email@example.ro ). Treating this as a spoofed 550 email. "

My .ENV config:


MAIL_HOST=mail.exemple.ro

MAIL_PORT=465

MAIL_ENCRYPTION=ssl

MAIL_PASSWORD=my pass

MAIL_USERNAME=email@exemple.ro

MAIL_DRIVER=smtp

FROM_ADDRESS=email@exemple.ro

My Contoller:

{
    $validator = Validator::make($request->all(), [
        'firstName' => 'required',
        'lastName' => 'required',
        'email' => 'required',
        'services' => 'required',
        'message' => 'required',
    ]);
    if ($validator->fails()) {
        return redirect('en/404')
            ->withErrors($validator)
            ->withInput();
    }

    $form = $request->all();
    $message->success('The message was successfully send!');

    Mail::send('email/contact-form', compact('form'), function ($email_message) use ($form) {

        $email_message->subject($form['services']);
        $email_message->from($form['email']);
        $email_message->to('email@example.ro');

    });
    return back();
}

If i try to write the input Email with email@exemple.ro i get no error but when i try to enter a gmail or other email i am getting the error above.

Please help!


Solution

  • You messed your code here

    Just replace code from

    Mail::send('email/contact-form', compact('form'), function ($email_message) use ($form) {
    
        $email_message->subject($form['services']);
        $email_message->from($form['email']);
        $email_message->to('happy@greenfab.ro');
    
    });
    

    To this below one

    Mail::send('email/contact-form', compact('form'), function ($email_message) use ($form) {
    
        $email_message->subject($form['services']);
        $email_message->from(env('MAIL_USERNAME'),$form['email']);
        $email_message->to(env('MAIL_USERNAME'));
    });
    

    You were messing code in from and to. As you can only enter email in from which can authenticate to your MAIL_HOST server on port MAIL_PORT, thats why you don't get any error when you enter your email address.

    I hope this would help you.