How send email in Laravel 5.8

2019-07-18 09:30发布

问题:

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($request)
    {
        $this->validate($request, [
        'name' => 'required|string',
        'topic' => 'required|string',
        'email' => 'required|email',
        'message' => 'required|string',
        'captcha' => 'required|captcha',
        'acceptReg' => 'required|integer',
    ]);
    $adminEmail = $this->frontendRepository->getSystemAdminEmail();

    $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/>
    <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::send('emails.welcome', $message, function ($message) {
        //$message->from('us@example.com', 'Laravel');
        $message->to($adminEmail, $name = null);
        $message->from($email, $name = null);
        $message->sender($email, $name = null);
        $message->replyTo($adminEmail, $name = null);
        $message->subject($title);
        //$message->attach($pathToFile, array $options = []);
        //$message->attachData($data, $name, array $options = []);
        $message->getSwiftMessage();
    });
    }

$adminEmail - this is my email

How can I send this message as email?

I have error: Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string given, called in /var/www/vanessa/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 237

How can I repeir it?

回答1:

  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



回答2:

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.