这些天我一直在用mailgun工作。它是在Laravel 7上运行的,而且运行得很好。我认为这不会给v8带来问题。
实际上,我已经从控制器发送了电子邮件,但您可以根据需要为您实现相同的功能。
无论如何,我都会分享我的经验。
.env
#MAIL_DRIVER=mailgun
MAIL_MAILER=mailgun
MAIL_HOST="smtp.mailgun.org"
MAIL_PORT=587
MAIL_USERNAME="postmaster@sandbox********************************.mailgun.org"
MAIL_PASSWORD="123456"
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN="sandbox********************************.mailgun.org"
MAILGUN_SECRET="key-********************************"
MAIL_FROM_NAME="ProjectName"
MAIL_FROM_ADDRESS="no-reply@yourfuturesite.com"
MAIL_ENV=test
MAIL_TEST="recipient@yourfuturesite.com"
#MAIL_LOG_CHANNEL
#MAILGUN_ENDPOINT="api.eu.mailgun.net"
config/mail.php
<?php
return [
'default' => env('MAIL_MAILER'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'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,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
'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,
'auth_mode' => null,
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
'from' => [
'address' => env('MAIL_FROM_ADDRESS'),
'name' => env('MAIL_FROM_NAME'),
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
// CUSTOM CONFIGS
'mail_env' => env('MAIL_ENV'), // 'local' for testing via mailgun, 'production' for all mails
'mail_test' => env('MAIL_TEST'), // test email for 'local' testing
// ADDITIONAL UNNECESSARY CONFIGS
// 'sendmail' => '/usr/sbin/sendmail -bs',
// 'sendmail' => '/usr/sbin/sendmail -t-i',
// 'pretend' => false,
// 'log_channel' => env('MAIL_LOG_CHANNEL'),
// 'pretend' => false,
];
config/services.php(与您的相同)
控制器
try {
$name = array_key_exists('first_name', $email_data) ? $email_data['first_name'] : $email_data['name'];
Mail::send('emails.confirm-registration', [
'role' => $email_data['role'],
'name' => $name,
'email' => $email_data['email'],
'confirm_registration' => route('front.auth.confirm_registration', ['registration_token' => $email_data['registration_token']]),
], function ($message) use ($email_data, $name) {
$to = (config('mail.mail_env') == 'prod' || config('mail.mail_env') == 'production') ? $email_data['email'] : config('mail.mail_test');
$message
->subject(config('app.name') . ": Email Confirmation")
->from(config('mail.from.address'), config('mail.from.name'))
->to($to, $name);
});
return true;
}
catch(\Exception $e) {
// TODO: report all the "$e->message"s like this
return false;
}
资源/视图/电子邮件/confirm-regulation.php
<a href="{{ route('front.main') }}" target="_blank">
<img label="logo" alt="{{ config('app.name') }}"
src="{{ $message->embed(public_path('images/logo.svg')) }}" width="128" height="96">
</a>
<p>{{ $name }}</p>
<p>{{ $confirm_registration }}</p>
<p>© {{ date("Y") == 2020 ? '2020' : '2020-' . date("Y") }} {{ config('app.name') }}</p>
这只是我的例子,所以你可能需要随意更换。
只需注意“config/mail.php”文件。我认为你需要设置一些额外的道具。(也不要忘记在这些最后的配置更改后重新加载缓存:“php artisan config:cache”)