5

Can anyone help me with Laravel Mail function?

I changed config/mail.php file to

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.concept.kz'),
'port' => env('MAIL_PORT', 25),
'from' => ['address' => 'support@concept.kz', 'name' => 'asdf'],
'encryption' => env('MAIL_ENCRYPTION', null),
'username' => env('support@concept.kz'),
'password' => env('mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',

This is my code in controller

Mail::raw($messageBody, function($message) {
            $message->from('support@concept.kz', 'Learning Laravel');
            $message->to('receiver@mail.ru');
        });

        if (Mail::failures()) {
            echo 'FAILED';
        }
        return redirect()->back();

I made the same changes to .env file, but nothing happens. Can anyone give me advice what to do? Am I doing something wrong?

Yersultan
  • 111
  • 1
  • 2
  • 5

1 Answers1

7

To send an email from your website

  1. create a view from which the user sends an email:

     {!! Form::Open(['url' => 'sendmail']) !!}
    
     <div class="col_half">
        {!! Form::label('name', 'Name: ' ) !!}
        {!! Form::text('name', null, ['class' => 'form-control', 'required']) !!}
     </div>
    
     <div class="col_half col_last">
        {!! Form::label('email', 'E-Mail: ' ) !!}
        {!! Form::email('email', null, ['class' => 'form-control', 'required']) !!}
     </div>
    
     <div class="clear"></div>
    
     <div class="col_full col_last">
        {!! Form::label('subject', 'Subject: ' ) !!}
        {!! Form::text('subject', null, ['class' => 'form-control', 'required']) !!}
     </div>
    
     <div class="clear"></div>
    
     <div class="col_full">
         {!! Form::label('bodymessage', 'Message: ' ) !!}
         {!! Form::textarea('bodymessage', null, ['class' => 'form-control', 'required', 'size' => '30x6']) !!}
     </div>
    
    
     <div class="col_full">
          {!! Form::submit('Send') !!}
     </div>
     {!! Form::close() !!}
    

1a. Create this function in controller:

public function sendMail(Request $request) {
    //dd($request->all());

    $validator = \Validator::make($request->all(), [
                                'name' => 'required|max:255',
                                'email' => 'required|email|max:255',
                                'subject' => 'required',
                                'bodymessage' => 'required']
    );

        if ($validator->fails()) {
            return redirect('contact')->withInput()->withErrors($validator);
        }


    $name = $request->name;
    $email = $request->email;
    $title = $request->subject;
    $content = $request->bodymessage;


        \Mail::send('emails.visitor_email', ['name' => $name, 'email' => $email, 'title' => $title, 'content' => $content], function ($message) {

            $message->to('your.email@gmail.com')->subject('Subject of the message!');
        });


    return redirect('contact')->with('status', 'You have successfully sent an email to the admin!');

}
  1. Create a route to that function in your routes file

  2. create a blade.php view file in /resources/views/emails/visitor_email.blade.php with this content:

       <p>Name: {{ $name }}</p>
       <p>E-Mail: {{ $email }}</p>
       <p>Subject: {{ $title }}</p>
       <p>Message: <br>
       {{ $content }}</p>
    
  3. in .env file put your data for sending email like this

     MAIL_DRIVER=smtp
     MAIL_HOST=smtp.gmail.com
     MAIL_PORT=587
     MAIL_USERNAME=your.email@gmail.com
     MAIL_PASSWORD=email-password
     MAIL_ENCRYPTION=tls
    

There has been a change since Laravel version 6 and above

MAIL_MAILER=smtp    <====== got changed from MAIL_DRIVER to MAIL_MAILER
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
lewis4u
  • 14,256
  • 18
  • 107
  • 148