Using Swiftmail to send email in Laravel

  1. Configs must be in .env. Not working in /config/mail.php (Unknown reason)

2. Add ‘stream’ config in config/mail.php, or you won’t be able to send emails on local machine.

    'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ],

3. Debug: /vendor/laravel/framework/src/illuminate/Mail/TransportManager.php

4. Clear config cache. (Optional)

php artisan config:clear

Mail Example

php artisan make:mail RegMail

 

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Models\Emails;

class RegMail extends Mailable
{
    use Queueable, SerializesModels;
    protected $user = null;
    protected $reg_email_id = 1;
    protected $mail_data = null;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        //
        $this->user = $user;
        $this->mail_data = Emails::find($this->reg_email_id);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $data = [
            'subject' => $this->mail_data->subject, 
            'msg' => $this->mail_data->msg, 
            'user' => $this->user
        ];

        return $this->subject($data['subject'])
            ->view('reg_email')
            ->with($data);
    }
}

Controller to send email

<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use Carbon\Carbon;

use App\Models\B2BMemberM;

use App\Mail\B2bRegMail;
 

class Test extends Controller
{
    public function __construct(){
    }

    private function build_data($member){
        $params = [
            'id' => $member->id, 
            'secret' => md5($member->id . $member->email)
        ];
        $url = url('/b2b/verify/?') . http_build_query($params);

        $data = [
            'verify_url' => $url, 
            'member' => $member
        ];

        return $data;
    }

    public function email(){
        $member = B2BMemberM::find(2);

        //stdClass() works only. Don't user array and collection.
        $user = new \stdClass();
        $user->name = $member->name;
        $user->email = $member->email;

        //To test view
        //$data = $this->build_data($member);
        //return view('email.b2b_reg', $data);

        //Mail will return null event email is sent successfully.
        Mail::to($user)->send(new B2bRegMail($member));
        logg('ok');
    }
}

 

Reference:

  1. https://stackoverflow.com/questions/36454920/swiftmailer-php-how-to-disable-ssl-certificate-validation
  2. Add custom subject:
    https://laracasts.com/discuss/channels/laravel/give-an-email-a-subject?page=1