[컴][php] lumen 에서 email 발송

루먼에서 이메일 사용 / 라라벨 이메일 사용 / email library for lumen


lumen 에서 mail 사용

lumen 에서 laravel 의 mail library 를 설치해서 사용할 것이다.


설치


c:\a\programming\php\lumen_project\src>composer require illuminate/mail
Using version ^5.5 for illuminate/mail
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for illuminate/container (locked at v5.4.19) -> satisfiable by illuminate/container[v5.4.19].
    - Conclusion: don't install illuminate/mail v5.5.0
    - Conclusion: don't install illuminate/mail 5.5.x-dev
    - Can only install one of: illuminate/support[5.6.x-dev, v5.4.19].
    - Can only install one of: illuminate/support[v5.4.19, 5.6.x-dev].
    - Can only install one of: illuminate/support[5.6.x-dev, v5.4.19].
    - illuminate/mail 5.6.x-dev requires illuminate/support 5.6.* -> satisfiable by illuminate/support[5.6.x-dev].
    - Installation request for illuminate/mail ^5.5 -> satisfiable by illuminate/mail[5.5.x-dev, 5.6.x-dev, v5.5.0].
    - Installation request for illuminate/support (locked at v5.4.19) -> satisfiable by illuminate/support[v5.4.19].


Installation failed, reverting ./composer.json to its original content.

현재(2017.08.31) illuminate/mail 의 버전은 5.5 인데, 이녀석은 lumen 5.4에서는 설치가 안됐다. 당연한 이야기 일 수 있지만, 같은 5.4 버전을 설치하면 된다.(여기 를 참고하자.)
composer require illuminate/mail:5.4.*


설정

아래 5가지를 설정해 주면 된다.
  1. <root>/bootstrap/app.php
  2. <root>/app/Providers/AppServiceProvider.php
  3. <root>/config/mail.php
  4. <root>/.env.env
  5. MyController.php

<root>/bootstrap/app.php

$app->register(App\Providers\AppServiceProvider::class);


<root>/app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Request;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        ...

    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
     public function boot()
     {
        $this->app->configure('services');
       
        $this->app->singleton('mailer', function ($app) {
            return $app->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer');
        });
       
        $this->app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);
     }
}


<root>/config/mail.php

<?php


return [

    'driver' => env('MAIL_DRIVER'),
    'host' => env('MAIL_HOST'),
    'port' => env('MAIL_PORT'),
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS'),
        'name' => env('MAIL_FROM_NAME'),
    ],
    'encryption' => env('MAIL_ENCRYPTION'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'markdown' => [
        'theme' => 'default',
        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
];


<root>/.env.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=*******@gmail.com
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=tls


MyController.php

app('mailer')->raw('Raw string email', 
                    function($msg) { 
                        $msg->to(['gae@gmail.com']); 
                        $msg->from(['gae@uck.com']); });


References


  1. How to get Laravel Mailer in Lumen 5.4?

댓글 없음:

댓글 쓰기