codeigniter email send example
Example 1: codeigniter 3 smtp email send
CodeIgniter Email Configuration
We need to have a central place where we can manage the email settings. CodeIgniter does not come with a config file for emails so we will have to create one ourselves.
Create a file email.php in the directory application/config
Add the following code to email.php
'smtp', // 'mail', 'sendmail', or 'smtp'
'smtp_host' => 'smtp.example.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',
'smtp_pass' => '12345!',
'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example
'mailtype' => 'text', //plaintext 'text' mails or 'html'
'smtp_timeout' => '4', //in seconds
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
?>
HERE,
'protocol' => 'smtp', specifies the protocol that you want to use when sending email. This could be Gmail smtp settings or smtp settings from your host
'smtp_host' => 'smtp.example.com',specifies the smtp host. For example, if you want to use Gmail then you would have something like smtp.gmail.com
'smtp_port' => 465, an open port on the specified smtp host that has been configured for smtp mail
'smtp_user' => '[email protected]', the email address that will be used as the sender when sending emails. This should be a valid email address that exists on the server
'smtp_pass' => '12345!', the password to the specified smtp user email
'smtp_crypto' => 'ssl', specifies the encryption method to be used i.e. ssl, tls etc.
'email type' => 'text', sets the mail type to be used. This can be either plain text or HTML depending on your needs.
'smtp_timeout' => '4', specifies the time in seconds that should elapse when trying to connect to the host before a timeout exception is thrown.
'charset' => 'iso-8859-1', defines the character set to be used when sending emails.
'wordwrap' => TRUE is set to TRUE then word-wrap is enabled. If it is set to FALSE, then word-wrap is not enabled
load->config('email');
$this->load->library('email');
$from = $this->config->item('smtp_user');
$to = $this->input->post('to');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
$this->email->set_newline("\r\n");
$this->email->from($from);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
echo 'Your Email has successfully been sent.';
} else {
show_error($this->email->print_debugger());
}
?>
Example 2: codeigniter send email using smtp gmail
$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';
//$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = 'YOURPASSWORDHERE';
$config['smtp_port'] = 465;
$config['smtp_timeout'] = 5;
$config['wordwrap'] = TRUE;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['validate'] = FALSE;
$config['priority'] = 3;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['bcc_batch_mode'] = FALSE;
$config['bcc_batch_size'] = 200;
$this->load->library('email’, $config);
$this->email->from('[email protected]', '[email protected]');
$this->email->to('[email protected]');
$this->email->subject('Test email from CI and Gmail');
$this->email->message('This is a test.');
$this->email->send();
Example 3: kirim email using codeigniter 4
request->getPost('to');
$subject = $this->request->getPost('subject');
$message = $this->request->getPost('message');
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.googlemail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // silahkan ganti dengan alamat email Anda
$mail->Password = 'password_email'; // silahkan ganti dengan password email Anda
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('[email protected]', 'Ilmu Coding'); // silahkan ganti dengan alamat email Anda
$mail->addAddress($to);
$mail->addReplyTo('[email protected]', 'Ilmu Coding'); // silahkan ganti dengan alamat email Anda
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
session()->setFlashdata('success', 'Send Email successfully');
return redirect()->to('/kirim_email');
} catch (Exception $e) {
session()->setFlashdata('error', "Send Email failed. Error: ".$mail->ErrorInfo);
return redirect()->to('/kirim_email');
}
}
}