Cấu hình và gửi thư điện tử (Email) với Gmail trong Laravel

Laravel

Trong quá trình xây dựng Website, việc xác thực người dùng khi đăng ký tài khoản, xác thực địa chỉ Email khi quên mật khẩu hay gửi Email cho thành viên mỗi khi có tin bài mới là một công việc quan trọng. Trong bài viết này, tôi sẽ hướng dẫn các bạn xây dựng cấu hình để gửi Email trong Laravel

Bạn cần một địa chỉ Gmail không có xác thực bảo mật qua 2 lớp (không khuyên dùng) vì nếu Email của bạn đã được xác thực bảo mật qua hai lớp thì không thể gửi được Email (đăng nhập bằng mật khẩu và code điện thoại di động).

Tiếp đó, bạn cần cấu hình file .env như sau:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:hLwSXW0+cgSKYh5Ti42pjBRMlZGwDWbyan4PhCFAINA=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://dothanhspyb.com // Địa chỉ này là chạy trên Server, nếu là Localhost thì bạn cần cấu hình http://127.0.0.1:8000 hoặc tên miền ảo. 

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=blog_sample
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Địa chỉ email của bạn. VD: dothanhspyb@gmail.com
MAIL_PASSWORD=mật khẩu đăng nhập Gmail
MAIL_ENCRYPTION=tls

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

Tiếp đó, bạn mở file mail.php theo đường dẫn: app\config\mail.php và cấu hình như sau:

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.gmail.com'),

    /*
    |--------------------------------------------------------------------------
    | 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', 'email của bạn@gmail.com'),
        'name' => env('MAIL_FROM_NAME', 'Thành Đỗ. Đào tạo Tin học trực tuyến'),
    ],

    /*
    |--------------------------------------------------------------------------
    | 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'),

    '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',

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'), // Cái này không quan trọng
        ],
    ],

    // Cấu hình chạy Email:
    'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ],

];

Có thể bạn cứ copy hết toàn bộ code và thay thế địa chỉ Email của bạn vào là Ok. Như vậy, chúng ta đã cấu hình xong Email, chúc bạn thành công!

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *