Sobatcoding.com - Tutorial Kirim Email Menggunakan Email Hosting Via SMPT Protocol
Buatlah sebuah controller untuk mengirim email bernama EmailController.php. Masukkan kode seperti berikut :
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
class EmailController extends BaseController
{
public function index()
{
$email = \Config\Services::email();
$config['protocol'] = 'smpt';
$config['SMTPHost'] = 'mail.**.com';
$config['SMTPUser'] = 'admin@***.com';
$config['SMTPPass'] = '****';
$config['SMTPPort'] = 465;
$config['SMTPCrypto'] = 'ssl';
$config['SMTPTimeout'] = 30;
$config['charset'] = 'utf-8';
$config['wordWrap'] = true;
$email->initialize($config);
$email->setFrom('admin@****.com', 'Admin Sobatcoding');
$email->setTo('***@gmail.com');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
//menambahkan attachement
$email->attach( ROOTPATH . 'public/attachment.pdf');
//menambahkan debuging error
if (! $email->send())
{
// Generate error
dd($email->printDebugger(['headers', 'subject', 'body']));
}else{
dd('Email terkirim');
}
}
}
Kita perlu tambahkan beberapa konfigurasi seperti :
$config['protocol'] = 'smpt'; //mail, sendmail, atau smtp
$config['SMTPHost'] = 'mail.**.com'; //SMTP Server Address.
$config['SMTPUser'] = 'admin@***.com'; //SMTP Username.
$config['SMTPPass'] = '****'; //SMTP Password.
$config['SMTPPort'] = 465; //SMTP Port. (If set to 465, TLS will be used for the connection
regardless of SMTPCrypto setting.)
$config['SMTPCrypto'] = 'ssl'; //tls atau ssl
$config['SMTPTimeout'] = 30;
Sesuaikan konfigurasi di atas dengan email hosting kalian masing-masing.
Selamat mencoba.
Komentar 0