Windows10 下安装 mailHog 用于邮件测试 (Laravel)

2018-10-30 11:51:43 阅读:15 编辑
1. 下载 MailHog

https://github.com/mailhog/MailHog/releases/download/v1.0.0/MailHog_Windows_amd64.exe

2. 安装 MailHog
  1. 把 MailHog_Windows_amd64.exe 复制到 d:/wamp/sendmail/mailhog.exe, 运行
    mailhog.exe -smtp-bind-addr 127.0.0.1:10250

    端口默认为 1025, 有可能已经被系统暂用。所以使用 10250 端口 ( 接收邮件端口) (相当于服务端,打开 http://127.0.0.1:8025),程序退出,邮件将被全部删除

    3.laravel 配置 (不用修改 env)
    config (["mail" => ["driver" => "smtp",
            "sendmail" => "D:/wamp/sendmail/mailhog.exe sendmail",
            "host" => "127.0.0.1",
            "port" => "10250",
            "from" => ['address' => "admin@linsys.test",
                'name' => "admin",
            ]
        ]]);
    4. 创建邮件
    PHP artisan make:mail Register
    1).E:\edison\linsys\App\Mail\Register.php
<?PHP

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class Register extends Mailable
{
    use Queueable, SerializesModels;

    public $name;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct ($name)
    {$this->name = $name;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build ()
    {return $this->view ('email_test', ['name' => $this->name]);
    }
}
2).E:\edison\linsys\resources\views\email_test.blade.php
{{$name}} 你好,这是一封测试文件。
5. 发邮件
\Mail::to ("14952516@qq.com")
        ->send (new \App\Mail\Register ("linson"));
    if (count (\Mail::failures () == 0)) // 没有失败的
    {echo "ok";} else {echo "fail";}