我过去四个月一直在做一个项目,我真的很生气,现在正面对Laravel。它没有发送电子邮件;我将其设置为使用邮件驱动程序并输入正确的代码,但似乎根本不起作用。除了不工作,它甚至没有给我一个错误!
这是我的配置:
return array(
/*
|--------------------------------------------------------------------------
| 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", "mail", "sendmail"
|
*/
'driver' => 'mail',
/*
|--------------------------------------------------------------------------
| 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 Postmark mail service, which will provide reliable delivery.
|
*/
'host' => 'smtp.mailgun.org',
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to delivery e-mails to
| users of your application. Like the host we have set this value to
| stay compatible with the Postmark e-mail application by default.
|
*/
'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' => array('address' => null, 'name' => null),
/*
|--------------------------------------------------------------------------
| 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' => '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' => null,
/*
|--------------------------------------------------------------------------
| 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' => null,
/*
|--------------------------------------------------------------------------
| 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,
);
这是我发送电子邮件的PHP代码:
$data["mail_message"] = "Hello!";
Mail::send('emails.welcome', $data, function($message)
{
$message
->to('[email protected]')
->from('[email protected]')
->subject('TEST');
});
首先,转到app / config / mail.php并将驱动程序更改为“mail”。同时将主机置为空白。
不是技术问题,但它解决了我的问题。
如果您使用GMAIL检查收到的邮件,请确保检查您的垃圾邮件文件夹。
在我的方案中,电子邮件正在排队,这就是为什么我没有输出。我忘记了我给queue by default发了电子邮件。我查看了乔布斯表,我看到所有的消息都在那里等着。我运行php artisan queue:work
以使队列运行/发送它们。
这可能是因为您在Laravel项目根目录中有一个“.env”文件,其邮件服务器配置如下:
...
MAIL_DRIVER=mail
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
似乎“.env”文件只是覆盖“config / mail.php”文件。您可以从“.env”文件中删除相同的行,以使用“config / mail.php”配置选项。
转到.env文件并设置
MAIL_DRIVER=smtp
或者您可以从配置文件中设置驱动程序。转到文件Laravel 4:app / config / mail.php Laravel 5:config / mail.php并设置
'driver' => 'smtp',
您可以使用SMTP。希望它会有所帮助。
未通过“邮件”发送的通知解决方案
https://laravel.com/docs/5.7/notifications#customizing-the-recipient
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* Route notifications for the mail channel.
*
* @param \Illuminate\Notifications\Notification $notification
* @return string
*/
public function routeNotificationForMail($notification)
{
return $this->email_address;
}
}
配置文件:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=xxxxxxxxxx
MAIL_ENCRYPTION=tls
然后运行make:mail
命令:
public function build(Request $request)
{
return $this->view('mail['variable'=>$request->message])->to($request->to);
}
路线:
Route::post('send', 'mailController@send');
Route::get('email', 'mailController@email');
控制器:
public function send()
{
Mail::send(new name());
}
public function email()
{
return view('email');
}
视图:
<body><br><h1>Send Mail</h1><form method="post" action="send">to:<input type="text" name="to">message: <input type="text" name="message" cols="30" rows="10"></input><input type="submit" name="submit" value="send"></form></body>
邮件:
<body><h1>Welcome, to this mail system, </h1>{!! $variable !!}</body>
要记住:message
变量是一个内置变量,会产生错误,所以使用任何其他变量而不是message
。
更多细节:
将配置文件中的驱动程序更改为“邮件”中的“smtp”。我想这应该有效。
我终于开始工作了。如果您使用的是mailgun,请将以下信息添加到.env
文件中
MAILGUN_DOMAIN=your.mailgundomain.com
MAILGUN_SECRET=key-yourmailgunkey
然后跑
php artisan config:clear
为我工作的修复
.env
文件中将APP_URL=127.0.01
更改为APP_URL=http://localhost
config:cache
,然后重新启动服务器。之后我的邮件开始发送!
我如何到达那个修复
我之前已经正确设置了我的SMTP详细信息,但即使在执行config:cache config:clear并重新启动我的服务器之后,电子邮件也没有发送。之后我将它与我工作的Laravel应用程序进行了比较,唯一的区别是127.0.0.1 - > http://localhost,所以我将其改为最后一次尝试,并且它有效。