使用 lumen 发送电子邮件,不在 env 文件中设置 smtp 凭据
send email with lumen, without set smtp credential in env file
我想用 lumen 发送电子邮件,
我有许多不同的 smtp 凭据,我必须使用 laravel 发送电子邮件,它超过 1000
所以我无法从 env 文件设置它。
每次发送电子邮件之前我都必须定义新的凭据。
然后如何在发送电子邮件之前将其传递给 lumen
因为在 lumen sendMail() 方法中我们可以传递参数数组然后如何管理它。
根据建议,我已经覆盖了如下代码,
我在 composer.json 文件中使用了以下包,
"illuminate/mail": "^5.2"
然后在命令提示符下启动命令composer update
在bootstrap/app.php下面的文件寄存器class,
$app->register('\Illuminate\Mail\MailServiceProvider');
在 cofig 文件夹中创建 mail.php 文件,如下所示,
<?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", "log", "array"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', ''),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| 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.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', ''),
'name' => env('MAIL_FROM_NAME', ''),
],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME', ''),
/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/
'password' => env('MAIL_PASSWORD', ''),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/
'pretend' => false,
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
];
为下面更新我的 .env 文件,
MAIL_DRIVER=
MAIL_HOST=
MAIL_PORT=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
MAIL_USERNAME=
MAIL_PASSWORD=
在bootstrap/app.php文件中添加mail.php文件如下,
$app->configure('mail');
创建提供程序以覆盖下面文件中的方法 createSmtpDriver
,
vendor\illuminate\mail\TransportManager.php
我创建了如下供应商,
<?php
namespace App\Providers;
use Illuminate\Mail\TransportManager;
use Session;
class EmailServiceProvider extends TransportManager
{
/**
* Create an instance of the SMTP Swift Transport driver.
*
* @return \Swift_SmtpTransport
*/
protected function createSmtpDriver()
{
$config = $this->app['config']['mail'];
// The Swift SMTP transport instance will allow us to use any SMTP backend
// for delivering mail such as Sendgrid, Amazon SES, or a custom server
// a developer has available. We will just pass this configured host.
$transport = SmtpTransport::newInstance(
Session::get('smtp_server'), $config['port']
);
if (isset($config['encryption'])) {
$transport->setEncryption($config['encryption']);
}
// Once we have the transport we will check for the presence of a username
// and password. If we have it we will set the credentials on the Swift
// transporter instance so that we'll properly authenticate delivery.
if (Session::has('smtp_user') && Session::get('smtp_user')) {
$transport->setUsername(Session::get('smtp_user'));
$transport->setPassword(Session::get('smtp_password'));
}
if (isset($config['stream'])) {
$transport->setStreamOptions($config['stream']);
}
return $transport;
}
}
并在 bootstrap/app.php 文件中添加该提供商,如下所示,
$app->register(App\Providers\EmailServiceProvider::class);
从现在开始,在所有方法中,它都会给我如下错误,
ErrorException in Manager.php line 77:
Missing argument 1 for Illuminate\Support\Manager::createDriver(), called in E:\xampp563\htdocs\delrentals-dashboard-api\vendor\illuminate\support\Manager.php on line 87 and defined
谁能给我建议,我应该怎么做才能解决。
您将需要覆盖命名空间 Illuminate\Mail\TransportManager
中的方法 createSmtpDriver
有一种使用多个 smtps 的简单方法。
public function sendEmails()
{
$smtps = Stmp::all();
foreach ($smtps as $smtp)
{
config('MAIL_DRIVER', 'smtp');
config('MAIL_USERNAME', $smtp->username);
config('MAIL_HOST', $smtp->host);
config('MAIL_PASSWORD', $smtp->password);
config('MAIL_PORT', $smtp->port);
config('MAIL_ENCRYPTION', $smtp->enc);
//now send mail
Mail::send('emails.mail', ['data' => $data], function ($message) use($smtp) {
$message->to('xyz@gmail.com', 'Tutorials Point')->subject('Laravel');
$message->from($smtp->from_name, 'Name');
});
}
}
我想用 lumen 发送电子邮件,
我有许多不同的 smtp 凭据,我必须使用 laravel 发送电子邮件,它超过 1000
所以我无法从 env 文件设置它。
每次发送电子邮件之前我都必须定义新的凭据。
然后如何在发送电子邮件之前将其传递给 lumen
因为在 lumen sendMail() 方法中我们可以传递参数数组然后如何管理它。
根据建议,我已经覆盖了如下代码,
我在 composer.json 文件中使用了以下包,
"illuminate/mail": "^5.2"
然后在命令提示符下启动命令composer update
在bootstrap/app.php下面的文件寄存器class,
$app->register('\Illuminate\Mail\MailServiceProvider');
在 cofig 文件夹中创建 mail.php 文件,如下所示,
<?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", "log", "array"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', ''),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| 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.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', ''),
'name' => env('MAIL_FROM_NAME', ''),
],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME', ''),
/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/
'password' => env('MAIL_PASSWORD', ''),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/
'pretend' => false,
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
];
为下面更新我的 .env 文件,
MAIL_DRIVER=
MAIL_HOST=
MAIL_PORT=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
MAIL_USERNAME=
MAIL_PASSWORD=
在bootstrap/app.php文件中添加mail.php文件如下,
$app->configure('mail');
创建提供程序以覆盖下面文件中的方法 createSmtpDriver
,
vendor\illuminate\mail\TransportManager.php
我创建了如下供应商,
<?php
namespace App\Providers;
use Illuminate\Mail\TransportManager;
use Session;
class EmailServiceProvider extends TransportManager
{
/**
* Create an instance of the SMTP Swift Transport driver.
*
* @return \Swift_SmtpTransport
*/
protected function createSmtpDriver()
{
$config = $this->app['config']['mail'];
// The Swift SMTP transport instance will allow us to use any SMTP backend
// for delivering mail such as Sendgrid, Amazon SES, or a custom server
// a developer has available. We will just pass this configured host.
$transport = SmtpTransport::newInstance(
Session::get('smtp_server'), $config['port']
);
if (isset($config['encryption'])) {
$transport->setEncryption($config['encryption']);
}
// Once we have the transport we will check for the presence of a username
// and password. If we have it we will set the credentials on the Swift
// transporter instance so that we'll properly authenticate delivery.
if (Session::has('smtp_user') && Session::get('smtp_user')) {
$transport->setUsername(Session::get('smtp_user'));
$transport->setPassword(Session::get('smtp_password'));
}
if (isset($config['stream'])) {
$transport->setStreamOptions($config['stream']);
}
return $transport;
}
}
并在 bootstrap/app.php 文件中添加该提供商,如下所示,
$app->register(App\Providers\EmailServiceProvider::class);
从现在开始,在所有方法中,它都会给我如下错误,
ErrorException in Manager.php line 77:
Missing argument 1 for Illuminate\Support\Manager::createDriver(), called in E:\xampp563\htdocs\delrentals-dashboard-api\vendor\illuminate\support\Manager.php on line 87 and defined
谁能给我建议,我应该怎么做才能解决。
您将需要覆盖命名空间 Illuminate\Mail\TransportManager
中的方法 createSmtpDriver有一种使用多个 smtps 的简单方法。
public function sendEmails()
{
$smtps = Stmp::all();
foreach ($smtps as $smtp)
{
config('MAIL_DRIVER', 'smtp');
config('MAIL_USERNAME', $smtp->username);
config('MAIL_HOST', $smtp->host);
config('MAIL_PASSWORD', $smtp->password);
config('MAIL_PORT', $smtp->port);
config('MAIL_ENCRYPTION', $smtp->enc);
//now send mail
Mail::send('emails.mail', ['data' => $data], function ($message) use($smtp) {
$message->to('xyz@gmail.com', 'Tutorials Point')->subject('Laravel');
$message->from($smtp->from_name, 'Name');
});
}
}