diff --git a/.env.example b/.env.example index c0660ea..0d99927 100644 --- a/.env.example +++ b/.env.example @@ -47,12 +47,12 @@ REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 -MAIL_MAILER=log -MAIL_SCHEME=null -MAIL_HOST=127.0.0.1 -MAIL_PORT=2525 +MAIL_MAILER=smtp +MAIL_HOST=mailpit +MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null +MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS="hello@example.com" MAIL_FROM_NAME="${APP_NAME}" diff --git a/app/Mail/BaseMailerObj.php b/app/Mail/BaseMailerObj.php new file mode 100644 index 0000000..b004402 --- /dev/null +++ b/app/Mail/BaseMailerObj.php @@ -0,0 +1,51 @@ +mailData['to'] = $this->addDomain($mailerObject->to); + //$this->mailData['to'] = $to; + $this->mailData['copy'] = !empty($copy) ? $this->addDomain($mailerObject->copy) : []; + $this->mailData['subject'] = $mailerObject->subject; + $this->mailData['body'] = $mailerObject->body; + $this->mailData['header'] = $mailerObject->header; + $this->mailData['footer'] = $mailerObject->footer; + $this->checkTestEnv(); + $this->appName = $mailerObject->appName; + $this->mailLayout = $mailerObject->mailLayout; + } + + + /** + * Метод корректирует свойства отправляемого письма, если среда разработки тестовая + * + * @return void + */ + private function checkTestEnv() + { + //Если тестировщик аутентифицирован, берется его логин из сессии, в противном случае берется закрепленный в глобальных конфигах тестовый адрес (который разработчик указывает для своего локального тестирования сам). Можно в будущем улучшить логику с определением тестового емейла, например, передавая к api ендпоинту заголовок с тестовым емейлом, но на старте пока так + // if (auth()->user()->login) { + if (session()->has('_auth_login')) { + $userLogin = session()->get('_auth_login'); + } else { + $userLogin = config('app.mail_test_addressee'); + } + // echo '
'; var_dump($userLogin); echo'
'; + // $test = config('app.test_env'); + // $test2 = config('app.mail_test_addressee'); + // echo '
'; var_dump($test); echo'
'; + // echo '
'; var_dump($test2); echo'
'; + //} + // $userLogin = auth()->user()->login ? auth()->user()->login : (session()->has('_auth_login') ? session()->get('_auth_login')) : config('MAIL_TEST_ADRESSEE'); + //Если работаем из под тестовой среды + if (config('app.test_env')) { + //Информация для тестирования будет добавлена в футер сообщения. В нее включаем информацию об адресатах и копии, если бы письмо отправлялось на проде + $testInfo = "На проде письмо отправится: " . implode(", ", $this->mailData['to']) . ". В копии: " . implode(", ", $this->mailData['copy']); + $this->mailData['footer'] .= $testInfo; + //Копию очищаем + $this->cc(''); + //echo '
'; var_dump(['dgavrilov@rencredit.ru']); echo'
'; + //echo '
'; var_dump($this->addDomain([$userLogin])); echo'
'; + $this->to($this->addDomain([$userLogin])); + //$this->to(['dgavrilov@rencredit.ru']); + } else { + //ГАВРИЛОВ. ДОБАВЬ ДОМЕН К ЛОГИНАМ + $this->to($this->mailData['to']); + $this->to($this->mailData['copy']); + } + } + + + /** + * Хэлпер преобразует всех адресатов, добавляя им почтовый домен + * + * @param array $addresseesArr адресаты + * @return array + */ + private function addDomain(array $addresseesArr): array + { + $domain = config('mail.mail_domain'); + + return array_map(function($el) use($domain) {return $el . "@" . $domain;}, $addresseesArr); + } + + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: $this->mailData['subject'], + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + // view: 'view.name', + view: $this->mailLayout, + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/config/app.php b/config/app.php index 423eed5..ae191a2 100644 --- a/config/app.php +++ b/config/app.php @@ -119,8 +119,5 @@ return [ */ 'maintenance' => [ - 'driver' => env('APP_MAINTENANCE_DRIVER', 'file'), - 'store' => env('APP_MAINTENANCE_STORE', 'database'), - ], - + 'mail_test_addressee' => env('MAIL_TEST_ADDRESSEE'), ]; diff --git a/config/mail.php b/config/mail.php index 522b284..7b5ba08 100644 --- a/config/mail.php +++ b/config/mail.php @@ -7,14 +7,13 @@ return [ | Default Mailer |-------------------------------------------------------------------------- | - | This option controls the default mailer that is used to send all email - | messages unless another mailer is explicitly specified when sending - | the message. All additional mailers can be configured within the - | "mailers" array. Examples of each type of mailer are provided. + | This option controls the default mailer that is used to send any email + | messages sent by your application. Alternative mailers may be setup + | and used as needed; however, this mailer will be used by default. | */ - 'default' => env('MAIL_MAILER', 'log'), + 'default' => env('MAIL_MAILER', 'smtp'), /* |-------------------------------------------------------------------------- @@ -25,28 +24,28 @@ return [ | their respective settings. Several examples have been configured for | you and you are free to add your own as your application requires. | - | Laravel supports a variety of mail "transport" drivers that can be used - | when delivering an email. You may specify which one you're using for - | your mailers below. You may also add additional mailers if needed. + | Laravel supports a variety of mail "transport" drivers to be used while + | sending an e-mail. You will specify which one you are using for your + | mailers below. You are free to add additional mailers as required. | | Supported: "smtp", "sendmail", "mailgun", "ses", "ses-v2", - | "postmark", "resend", "log", "array", - | "failover", "roundrobin" + | "postmark", "log", "array", "failover", "roundrobin" | */ 'mailers' => [ - 'smtp' => [ 'transport' => 'smtp', - 'scheme' => env('MAIL_SCHEME'), 'url' => env('MAIL_URL'), - 'host' => env('MAIL_HOST', '127.0.0.1'), - 'port' => env('MAIL_PORT', 2525), + 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), + 'port' => env('MAIL_PORT', 587), + // 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, - 'local_domain' => env('MAIL_EHLO_DOMAIN', parse_url((string) env('APP_URL', 'http://localhost'), PHP_URL_HOST)), + //'local_domain' => env('MAIL_EHLO_DOMAIN'), + 'auth_mode' => null, + 'verify_peer' => false, ], 'ses' => [ @@ -55,14 +54,17 @@ return [ 'postmark' => [ 'transport' => 'postmark', - // 'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'), + // 'message_stream_id' => null, // 'client' => [ // 'timeout' => 5, // ], ], - 'resend' => [ - 'transport' => 'resend', + 'mailgun' => [ + 'transport' => 'mailgun', + // 'client' => [ + // 'timeout' => 5, + // ], ], 'sendmail' => [ @@ -85,7 +87,6 @@ return [ 'smtp', 'log', ], - 'retry_after' => 60, ], 'roundrobin' => [ @@ -94,9 +95,7 @@ return [ 'ses', 'postmark', ], - 'retry_after' => 60, ], - ], /* @@ -104,9 +103,9 @@ return [ | Global "From" Address |-------------------------------------------------------------------------- | - | You may wish for all emails sent by your application to be sent from - | the same address. Here you may specify a name and address that is - | used globally for all emails that are sent by your application. + | You may wish for all e-mails sent by your application to be sent from + | the same address. Here, you may specify a name and address that is + | used globally for all e-mails that are sent by your application. | */ @@ -115,4 +114,25 @@ return [ 'name' => env('MAIL_FROM_NAME', 'Example'), ], + /* + |-------------------------------------------------------------------------- + | Markdown Mail Settings + |-------------------------------------------------------------------------- + | + | If you are using Markdown based email rendering, you may configure your + | theme and component paths here, allowing you to customize the design + | of the emails. Or, you may simply stick with the Laravel defaults! + | + */ + + 'markdown' => [ + 'theme' => 'default', + + 'paths' => [ + resource_path('views/vendor/mail'), + ], + ], + + 'mail_domain' => env('MAIL_DOMAIN'), + ]; diff --git a/config/services.php b/config/services.php index 6a90eb8..0ace530 100644 --- a/config/services.php +++ b/config/services.php @@ -14,12 +14,15 @@ return [ | */ - 'postmark' => [ - 'key' => env('POSTMARK_API_KEY'), + 'mailgun' => [ + 'domain' => env('MAILGUN_DOMAIN'), + 'secret' => env('MAILGUN_SECRET'), + 'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'), + 'scheme' => 'https', ], - 'resend' => [ - 'key' => env('RESEND_API_KEY'), + 'postmark' => [ + 'token' => env('POSTMARK_TOKEN'), ], 'ses' => [ @@ -28,11 +31,4 @@ return [ 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], - 'slack' => [ - 'notifications' => [ - 'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'), - 'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'), - ], - ], - ]; diff --git a/resources/views/mail/mailer_default.blade.php b/resources/views/mail/mailer_default.blade.php new file mode 100644 index 0000000..37d9f25 --- /dev/null +++ b/resources/views/mail/mailer_default.blade.php @@ -0,0 +1,12 @@ + + + + <h3>{!! $mailData['header'] !!}</h3> + + +

{!! $mailData['body'] !!}

+

{!! $mailData['footer'] !!}

+

+

{{ $appName }}

+ + \ No newline at end of file