How send email in Laravel 5.8

前端 未结 3 436
情深已故
情深已故 2021-01-21 01:42

I am beginner in Laravel. I use Laravel 5.8 in my project. I want send email from contact form.

I have this code:

public function sendContactForm($reques         


        
相关标签:
3条回答
  • 2021-01-21 02:07
    1. first of all you have to create a mail class in app/Mail
    2. create you view
    3. on your sendcontactform controller call the mail class
    4. dont forget to also call the mail facades: use Illuminate\Support\Facades\Mail;

    examples:

    //mail class file inside app/Mail directory
    
        <?php
    
        namespace App\Mail;
    
        use Illuminate\Bus\Queueable;
        use Illuminate\Mail\Mailable;
        use Illuminate\Queue\SerializesModels;
        use Illuminate\Contracts\Queue\ShouldQueue;
    
        class SendContact extends Mailable
        {
            use Queueable, SerializesModels;
            public $username, $message;
            /**
             * Create a new message instance.
             *
             * @return void
             */
            public function __construct($username, $message)
            {
                //
                $this->username = $username;
                $this->message= $message;
            }
    
            /**
             * Build the message.
             *
             * @return $this
             */
            public function build()
            {
                return $this->view('contactform');
            }
        }
    //your controller
    
    use Illuminate\Support\Facades\Mail;
    use App\Mail\SendContact;
    
    public function sendContactForm($request)
        {
            $this->validate($request, [
                'name' => 'required|string',
                'topic' => 'required|string',
                'email' => 'required|email',
                'message' => 'required|string',
                'captcha' => 'required|captcha',
                'acceptReg' => 'required|integer',
            ]);
    
    
            $title = $request->input('topic');
            $txt = nl2br($request->input('message'));
            $userName = $request->input('name');
            $email = $request->input('email');
            $ip = $request->getClientIp();
            $dateTime = date('Y-m-d H:i:s');
    
            $message = "Masz wiadomość ze strony". env('APP_NAME')."<br/>";
            $message .= "
            <b>Dane wiadomości:</b> $dateTime [$ip]<br/>
            <b>Tytuł wiadomości:</b> $title<br/>
            <b>Imię:</b> $userName<br/>
            <b>Adres email:</b> $email<br/>
            <b>Wiadomość:</b> $txt<br/>";  
    
             Mail::to($email)->send(new SendContact($userName, $message));
             die('mail sent!');
        }
    
     //your blade contactform.blade.php
    @extends('app.layouts')
    @section('content')
                    Hello, {{ $username }}<br/>
                    Your enquiry has been received<br/>
    
                   <br/>
                    <p>{{ $message }}</p>
    
    
    @endsection
    
     in your .env file define this
        MAIL_DRIVER=smtp
        MAIL_HOST="mail.example.com" //this should be from your webhost
        MAIL_PORT=465
        MAIL_USERNAME="info@example.com" //this is the email from your domain
        MAIL_PASSWORD="************"  //password of the email
        MAIL_FROM_ADDRESS=info@example.com
        MAIL_FROM_NAME='Enquiry Information'
        MAIL_ENCRYPTION=ssl
    

    hope this helps

    0 讨论(0)
  • 2021-01-21 02:17

    Send email in laravel 5.8

    Mail::send('blade.php', $email_data, function($message) use ($officer) {
        $message->from('from@gmail.com','web title');
        $message->subject("Appointment Scheduled");
        $message->to($email_data['email']);
    });
    
    0 讨论(0)
  • 2021-01-21 02:30

    In order to send email, you may need to add something like the code snippet mentioned below:

    Mail::send('email',
       array(
           'name' => $request->get('name'),
           'email' => $request->get('email'),
           'user_message' => $request->get('message')
       ), function($message)
       {
           $message->from('abc@gmail.com');
           $message->to('test@cloudways.com', 'Admin')->subject('Cloudways 
    Feedback');
       });
        }
    

    For detailed information on how to send mail please refer this link.

    0 讨论(0)
提交回复
热议问题