Kirim Email Di Lumen 5.2 Menggunakan Mandrill
Halo Lumener π
Sabtu malam gagal kencan gara-gara ujan (curhat) yasudah dibikin nulis artikel aja, semoga bermanfaat dan berguna. Kali ini yg dibahas adalah cara kirim email di framework Lumen 5.2 menggunakan protokol SMTP di Mandrill. Kebetulan kemarin ada mainan yg perlu dibikin dari Lumen, kenapa Lumen? 1. karena sangat ringan (tentunya) 2. untuk web skala ringan (harusnya sih framework ini cocoknya untuk API) 3. mudah dipelajari.
Ok, langsung saja, berikut yang perlua dipersiapkan:
- API key Mandrill, kalau belum punya coba registrasi disini
- Framework Lumen (veris terbaru 5.2) yg sudah terinstall
- Sedikit paham tentang cara install via composer
- Kopi dan roti goreng dari istri yg solehah
(yg jomblo abaikan)
Baiklah, mari kita mulai,
Buka file di app.php di bootstrap/app.php dan uncomment baris berikut:
//$app->withFacades(); .... //$app->register(App\Providers\AppServiceProvider::class);
menjadi:
$app->withFacades(); .... $app->register(App\Providers\AppServiceProvider::class);
Untuk mengaktifkan ServiceProvider, tambahkan kode berikut pada file app/Providers/AppServiceProvider.php di dalam function register()
public function register() { $this->app->singleton('mailer', function ($app) { $app->configure('services'); return $app->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer'); }); }
Kemudian kita install paket via composer: illuminate/mail and guzzlehttp/guzzle
$ composer require illuminate/mail $ composer require guzzlehttp/guzzle
Lalu buat folder Config setelah itu buat file services.php di dalamnya rootweb/config/services.php, tuliskan berikut:
<?php return [ 'mandrill' => [ 'secret' => env('MAIL_KEY') ], ];
Kemudian isikan konfigurasi mandrill di file .env
MAIL_KEY=<mandrill_api_key> MAIL_USERNAME=<mandrill_username> MAIL_PASSWORD=<mandrill_password>
terkahir, buat file mail.php di dalam folder config yg telah dibuat tadi dan isikan berikut:
<?php return [ /* |-------------------------------------------------------------------------- | 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", "mailgun", "mandrill", | "ses", "sparkpost", "log" | */ 'driver' => env('MAIL_DRIVER', 'mandrill'), /* |-------------------------------------------------------------------------- | 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.mandrillapp.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' => "email@domainkamu.com", 'name' => "Nama Kamu"], /* |-------------------------------------------------------------------------- | 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'), /* |-------------------------------------------------------------------------- | 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' => 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', ];
Ok, konfigurasi selesai, tinggal eksekusi di controller dan membuat views untuk email, berikut contoh (hanya contoh, selebihnya silahkan sesuaikan dengan keperluan masing-masing) mengirim email, pertama panggil Illuminate\Support\Facades\Mail; di controller seperti ini:
use Illuminate\Support\Facades\Mail;
selanjutnya panggil Mail::send() , ada beberapa fungsi dan parameter yg bisa digunakan untuk mengirim email, detailnya bisa dilihat disini
Mail::send('layouts.email', ['data'=>$data], function($send) use ($email){ $send->to($data['email'], $data['nama'])->subject('Contoh Kirim Email via Mandrill!'); $send->from('email@domainkamu.com', 'Nama Kamu'); });
Terakhir, buat file view email (terserah) taruh mana, contohnya taruh di resources/views/layouts/email.blade.php
<!DOCTYPE html> <html lang="en" class="app" itemscope itemtype="http://schema.org/Article"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> </head> <body> <span style="font-family:'comic sans ms','sans-serif'"> Hello, <br> <br> Ini adalah contoh konten yg dikirim dari Lumen menggunakan Mandrill <br> <br> Terima Kasih </span> </body> </html>
Begitulah kisah gagal malam mingguanku, semoga bermanfaat dan ditularkan kepada yg lain, kalau ada error, kurang jelas atau cara lain yg lebih baik mari kita diskusi, master. π
~ Sekian
kok punya ky error gini ya mas
Class ‘Mail’ not found
composer require illuminate/mail”:”5.2.* . ganti pake itu coba pak,
saya sudah mengikuti dengan baik, tetapi masih error
Missing argument 1 for Illuminate\Support\Manager::createDriver()
itu error kenapa ya bang?