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

时间:2014-06-01 14:39:11

标签: 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,

);

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

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

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

16 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:4)

在我的方案中,电子邮件正在排队,这就是为什么我没有输出。我忘记了我将电子邮件设置为queue by default。我查看了乔布斯表,我看到所有的消息都在那里等着。我运行php artisan queue:work以使队列运行/发送它们。

答案 2 :(得分:3)

转到.env文件并设置

MAIL_DRIVER=smtp

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

'driver' => 'smtp',

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

答案 3 :(得分:3)

可能是因为你有一个" .env"在Laravel项目根目录中的文件,具有如下邮件服务器配置:

...
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"配置选项。

答案 4 :(得分:2)

您可以通过键入以下内容检查常规的电子邮件传递:

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'); 
});

甚至您将获得SMTP对话框的输出。

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

答案 5 :(得分:1)

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

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;
    }
}

答案 6 :(得分:1)

配置文件:

MAIL_DRIVER=smtp 
MAIL_HOST=smtp.gmail.com 
MAIL_PORT=587 
MAIL_USERNAME=abc@gmail.com 
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

更多详细信息:

答案 7 :(得分:0)

将配置文件中的驱动程序更改为&#39; smtp&#39;来自&#39; mail&#39;。我想这应该有效。

答案 8 :(得分:0)

我终于开始工作了。如果您使用的是mailgun,请将以下信息添加到.env文件

MAILGUN_DOMAIN=your.mailgundomain.com
MAILGUN_SECRET=key-yourmailgunkey

然后运行

php artisan config:clear

答案 9 :(得分:0)

对我有用的修复程序

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

之后,我的邮件开始发送!

我如何达到此目标

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

答案 10 :(得分:0)

对于Laravel 6,如果不再发送验证电子邮件:

在user.php中添加

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail

以及在routes / web.php中

Auth::routes(['verify' => true]);

答案 11 :(得分:0)

首先,请在cmd上转到您的项目根目录并运行此cmd-

php artisan route:cache (clear your routes cache)
php artisan cache:clear (clear your application cache)
php artisan config:cache (clear your config cache)
php artisan view:clear (clear your view (blade) cache)

转到.env文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=YourUserName
MAIL_PASSWORD=YourPassword
MAIL_ENCRYPTION=tls

转到项目根目录并使用此cmd

php artisan make:mail WelcomeUserVerifyMail

之后转到App \ mail \ WelcomeUserVerifyMail 添加这些行-

use Illuminate\Mail\Mailable;
public $mailData;
public function __construct(array $mailData)
{
  $this->mailData = $mailData;
}
public function build()
{
return $this->from(env('MAIL_FROM'))->subject('Test')->view('email-template.email'); //view load
}

在使用邮件功能的控制器中-

use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeUserVerifyMail;

// Send Verify email
$mailData = ['name' => "Shubham Gupta", 'verifyButtonUrl' => $verifyButtonUrl];
Mail::to('shubham@yopmail.com')->send(new WelcomeUserVerifyMail($mailData));

答案 12 :(得分:0)

我遇到了同样的问题。

邮件没有发送,因为我没有设置电子邮件的收件人。

不要忘记设置它。

$mail->to('example@example.com');

答案 13 :(得分:0)

我遇到了同样的问题!然后我注意到我的老板对 config/mail.php 进行了更改。 这是你需要的:

在 .env 文件中设置邮件变量:

MAIL_DRIVER=smtp
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls

在 config/mail.php 中,确保您有 something => env('ENV_VARIABLE_NAME', 'default_value')

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
    'theme' => 'default',

    'paths' => [
        resource_path('views/vendor/mail'),
    ],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),

如果您有 ssl 问题,请添加到 config/mail.php :

'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]

祝大家好运。

答案 14 :(得分:-1)

不是技术性答案,但它解决了我的问题。

如果您使用GMAIL检查收到的邮件,请确保检查您的垃圾邮件文件夹。

答案 15 :(得分:-1)

运行为

Mail::raw( 'mail_content' , function ($message) {
            $message->to(...)
            ->subject('Hi, welcome user!');
        });