본문 바로가기

Server/PHP

[laravel] amazon aws ses 메일 설정 및 사용방법

*** 2개 라이브러리 설치해야 함. 설치 방법은 2가지 있음.


* 설치방법 1
: 서버가 설치된 콘솔창에서 명령어 실행

1. composer require illuminate/mail:5.7
2. composer require aws/aws-sdk-php

 

* 설치방법 2
: composer.json 파일 내 require블럭에 내용 추가 후 명령어 실행

1.
"require": {
  "illuminate/mail": "5.7",
  "aws/aws-sdk-php": "~3.0",
  ...
}

2. composer update

 

이후 절차는 동일
--------------------------------------

3. bootstrap/app.php 파일 내 코드 추가
: Register Service Providers 영역(주석 하단)에 작성

3. bootstrap/app.php 
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
...
  $app->register(Illuminate\Mail\MailServiceProvider::class);
  $app->configure('mail');
  $app->configure('services');

 


4. .env 파일에 코드추가

MAIL_DRIVER = ses   
SES_KEY = 발급받은 본인의 SES_KEY
SES_SECRET = 발급받은 SES_SECRET
SES_REGION = us-east-1 (설정한 리전)

 

5. app 폴더와 같은 레벨에 config/mail.php파일에 driver 옵션을 ses 로 설정

<?php
return 
    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
    |            "sparkpost", "postmark", "log", "array"
    |
    */

    'driver' => env('MAIL_DRIVER', 'ses'),
    ...
]
?>

 

6. config 폴더 내 services.php 파일에 하단 코드 작성 (파일이 없다면 생성하기)

<?php
return [
	'ses' => [
		'key' => env('SES_KEY'),
		'secret' => env('SES_SECRET'),
		'region' => env('SES_REGION'),  // e.g. us-east-1
	]
]
?>

기존에 파일이 있는 경우에는 'ses' => [] 블럭만 입력.
파일에 직접 ses key, secret, region을 작성할 거라면 4번의 .env 파일 코드추가 절차 생략 가능

 

7. blade 파일 위치로 resources/views 하위에 mailling 폴더 생성
: 메일 양식을 설정 후 필요에 따라 blade 파일을 메일 발송시 지정하는 용도이기 때문에 분류를 구분하지 않을 거라면 생략 가능

 

8. 사용할 메일 양식으로 blade 파일 생성 (경로는 resource/views 또는 7번 과정을 진행한 경우 resource/views/mailling)
: 파일 형식은 (파일 명).blade.php

 

9. 작업을 수행 할 컨트롤러에서 메일 발송 처리

<?php

// 사용 할 컨트롤러 상단에 메일 발송 파사드 추가
use Illuminate\Support\Facades\Mail;

class 컨트롤러 명 extends Controller {

  ...
  
  public function 함수 명(파라미터) {
  
    ... 처리코드
    
    // 암호화 처리 할 key 는 자유롭게 아무렇게나 설정
    // 필자는 각각 asdfasfasfd, 123321231123, sdfsdf112sdf212312 .. 처럼 설정
    $data['enc_tr_cert'] = "cert 암호화 key";
    $data['tr_url'] = "url 암호화 key";
    $data['tr_add'] = "address 암호화 key";

    // Mail::send(blade 파일 명, 암호화데이터, rollback 함수)
    // blade 파일 명은 파일이 resource/views 폴더에 있으면 파일 명만 입력
    // 하위 폴더를 추가했으면 (하위폴더.파일 명)
    Mail::send('mailling.mailTest', $data, function ($m) {
      $m->from('발송자 이메일', 'laravel');
      $m->to("수신인 이메일");
      $m->subject("제목");
    });
    
    ... 처리코드
  }
  
  ...
}
?>

 

메일 발송 설정의 경우 laravel 공식 사이트에 더 간단하게 설명되어 있으므로, 참고

 

라라벨 6.x - 메일

라라벨 한글 메뉴얼 6.x - 메일

laravel.kr