Laravel不发送电子邮件而不发送错误

Dan*_*rde 29 php laravel laravel-4

我过去四个月一直在做一个项目,我真的很生气,现在正面对着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,

);
Run Code Online (Sandbox Code Playgroud)

这是我发送电子邮件的PHP代码:

$data["mail_message"] = "Hello!";

    Mail::send('emails.welcome', $data, function($message)
    {
        $message
            ->to('me@mydomain.com')
            ->from('info@otherdomain.com')
            ->subject('TEST');
    });
Run Code Online (Sandbox Code Playgroud)

小智 12

首先,转到app/config/mail.php并将驱动程序更改为"mail".同时将主机置为空白.

  • 任何尝试此方法的人,不要忘记清除缓存,`php artisan config:cache` (2认同)

Jus*_*tin 7

在我的情况下,电子邮件正在排队,这就是为什么我没有输出的原因。我忘记了默认情况下将电子邮件设置为排队。我查看了Jobs表,看到所有消息都在那里等待。我跑去php artisan queue:work让队列运行/发送它们。


Ale*_*lex 7

您可以通过键入以下内容来检查一般电子邮件传送:

php artisan tinker

Mail::getSwiftMailer()->registerPlugin (
    new Swift_Plugins_LoggerPlugin(new Swift_Plugins_Loggers_EchoLogger(false))
);
$to = 'youraddress@example.com';
Mail::raw('Testmail', function ($message) use($to) {  
    $message->to($to)->subject('Testmail'); 
});
Run Code Online (Sandbox Code Playgroud)

你甚至会得到 SMTP 对话框的输出。

确保替换$to为您的邮件地址以检查它是否到达。

编辑如果这有效并且您仍然遇到电子邮件传递问题,则您可能遇到排队问题。

  • 不幸的是,Laravel 8 中不存在 `Swift_Plugins_LoggerPlugin` 和 `Swift_Plugins_Loggers_EchoLogger`。 (3认同)

Brn*_*iya 5

转到 .env 文件并设置

MAIL_DRIVER=smtp
Run Code Online (Sandbox Code Playgroud)

或者您可以从配置文件设置驱动程序。转到文件 Laravel 4:app/config/mail.php Laravel 5:config/mail.php 并设置

'driver' => 'smtp',
Run Code Online (Sandbox Code Playgroud)

您可以使用 SMTP。希望它会有所帮助。


Ale*_*lex 5

这可能是因为您在 Laravel 项目根目录中有一个“.env”文件,其邮件服务器配置如下:

...
MAIL_DRIVER=mail
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Run Code Online (Sandbox Code Playgroud)

看来“.env”文件只是覆盖了“config/mail.php”文件。您只需从“.env”文件中删除相同的行即可使用“config/mail.php”配置选项。

  • 或者我应该更好地写“不应该被触及”,因为它主要是提交的(并且只有在像你这样的极少数情况下才可能被触及)? (2认同)

小智 5

对我有用的修复

  1. 在您的.env文件中更改APP_URL=127.0.01APP_URL=http://localhost
  2. 正确设置您的 SMTP 详细信息,然后执行config:cache,然后重新启动服务器。

之后我的邮件开始发送!

我是如何解决这个问题的

我之前已经正确设置了我的 SMTP 详细信息,但是即使在执行 config:cache config:clear 并重新启动我的服务器之后,电子邮件也没有发送。之后,我将它与我的工作 Laravel 应用程序进行了比较,唯一的区别是 127.0.0.1 -> http://localhost,所以我将其更改为最后一次尝试,并且它起作用了。


小智 5

不通过“邮件”发送通知的解决方案

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 0

将配置文件中的驱动程序从“mail”更改为“smtp”。我想这应该可行。