Attach a file to an email in codeigniter - codeigniter

This is my code and I want to attach multiple files to the email.
This code is working and sending the email, but I cannot attach an attachment to the email. Email title and the message are user inputs and the sending email address is also a user input.In here I used check boxes to select the email addresses that sending the email.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sendemail extends CI_Controller {
public function __construct()
{
/*call CodeIgniter's default Constructor*/
parent::__construct();
/*load database libray manually*/
$this->load->database();
$this->load->library('session');
/*load Model*/
$this->load->helper('url');
}
public function index() {
$this->load->view('Admin/dashboard');
}
public function form_validation()
{
//echo 'OK';
$this->load->library('form_validation');
$this->form_validation->set_rules("title", "title", 'required');
$this->form_validation->set_rules("files[]", "files[]");
$this->form_validation->set_rules("message", "message", 'required');
$this->form_validation->set_rules("single_select[]", "single_select[]", 'required');
if ($this->form_validation->run()){
if(isset($_POST['submit'])){
$checkbox1=$_POST['single_select'];
foreach($checkbox1 as $chk1){
$this->load->library('email');
$this->email->to($chk1);
$this->email->from('xxxxxxxxxx#gmail.com');
$this->email->subject($this->input->post("title"));
$this->email->message($this->input->post("message"));
$this->email->send();
}
$this->session->set_flashdata('message','Email sent.');
redirect('Admin/index');
}
else{
$this->session->set_flashdata('message','Something went wrong!');
redirect('Admin/index');
}
}
else{
$this->session->set_flashdata('message','Something went wrong!');
redirect('Admin/index');
}
}
}```

You can attach multiple files like this
$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');

Related

Redirect is not working in Codeigniter on real server. Error reporting in on but not showing any error

I have Codeigniter 2 installed on my server. redirect is not working on the login page. I can provide the URL.
This is my controller file of login. I have set the error reporting on but not displaying any error. Kindly help me with this. The script is taken from truebus.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_admin extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('AdminLogin_model');
$this->load->model('Settings_model');
$this->load->library('session');
if($this->session->userdata('logged_in_admin')) {
redirect(site_url('bus_details/view_busdetails'));
}
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
// redirect(base_url().'Bus_details/view_busdetails');
redirect(site_url('bus_details/view_busdetails'));
}
}
$this->load->view('login-form');
}
I think you didn't include URL helper
$this->load->helper('url');
you should include in controller
$this->load->helper('url);
redirect()

Dashboard Failes to load after sign in

After right login details,form redirects to same place
This is my controller for the login
The login should be redirected to the dashboard after validation and database check
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
if($this->session->userdata('logged_in')) {
redirect(base_url().'dashboard');
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
redirect(base_url().'dashboard');
}
}
$this->load->view('Templates/header', $template);
$this->load->view('Login/login_form');
$this->load->view('Templates/footer');
}
}
code for Dashboard
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('dashboard_model');
if(!$this->session->userdata('logged_in')) {
redirect(base_url());
}
}
Try This Method
redirect('dashboard');
Try site_url() as well. Make sure there are proper code(error free) of view in dashboard controller.
redirect(site_url()."/dashboard");

CodeIgniter Call to a member function on a non-object in [library]

I'am trying call custom library function witch should return inputed values back to form, but I'am geting error.
enter image description here
Controler:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Guest extends CI_Controller {
public function __construct(){
parent::__construct();
error_reporting(E_ALL ^ (E_NOTICE));
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation', 'uri');
$this->load->model('dayData');
$this->load->library('session');
$this->load->library('guest');
}
public function index(){
$this->login();
}
public function dataToView($data, $viewFile){
$this->load->view('template/header');
if($viewFile != ''){
$this->load->view('user/' . $viewFile, $data);
}
$this->load->view('template/footer');
}
public function login(){
$this->dataToView($data, 'guest/login');
}
public function registration(){
if($this->input->post('registrationSubmit') !== null) {
//$this->form_validation->set_error_delimiters('<div class="alert alert-warning" role="alert">', '</div>');
$this->config->load('form_validation');
$this->form_validation->set_rules($this->config->item('registrationValidation'));
if($this->form_validation->run() == FALSE){
var_dump($this->guest->registrationPost());
$this->dataToView($data, 'guest/registration');
} else {
echo "string";
}
} else {
$this->dataToView($data, 'guest/registration');
}
}
}
Library:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Guest {
protected $CI;
public function __construct(){
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
//$this->CI->load->model('Guest');
$this->CI->lang->load('error', 'english');
}
public function registrationPost(){
$result = array('name' => $this->CI->input->post('name'),
'nickName' => $this->CI->input->post('nickName'),
'email' => $this->CI->input->post('email'));
return $result;
}
}
There is a naming conflict between the controller and the custom class. They should not have the same name.
You have duplicate class names. The controller class name is Guest and the library class name is also Guest.
When CodeIgniter goes to load the Guest library, it will skip over it and log a debug message. https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Loader.php#L1032
I suggest renaming your library to something else, Registration perhaps?

codeigniter: call Index Method from another method

I am new in CodeIgniter. I am getting a problem. My procedure seems logical but it would not work!
I have a controller: User
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index($slug = FALSE) {
$data['title'] = "User List";
if($slug === FALSE) {
$data['user'] = $this->user_model->get_user();
} else {
$data['user'] = $this->user_model->get_user($slug);
}
if (empty($data['user'])){
show_404();
}
$this->load->library('table');
$this->load->view('user_view', $data);
}
public function authen() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('phone', 'phone', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login_form');
} else {
if($this->user_model->do_login()===TRUE) {
$this->index();
}
$this->authen();
}
}
}
The "authen" method is not working properly with its last conditions. Page does not redirect to controller.
Can anybody help?
Thanks
You can use: redirect('user') it will redirect u to the controller user class
Instead of
$this->index();
try this
header('Location:http://myipaddress/mysite/user');
You are try to load the login page in case there is any authentication error.
For for this you will need to bring user to the login page again.
use redirect('user/login'); and store the error message in session flash.
or display validation errors..
you cannot call controller from a controller. as $this->index();

CodeIgniter put email in a global function

I can send an email from within a controller with the example in the CodeIgniter documentation. I was wondering how I would put the email code on a global functions page so I have access to it all over with a simple function call.
// in controller
emailTest($to, $subject, $message);
// on a global functions page
function emailTest($to, $subject, $message) {
$this->load->library('email');
$this->email->from('my#example.com', 'My Name');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
You can create a helper and just load it up when you need it.
$this->load->helper('email');
send_email($to, $subject, $message);
Edit: Since you want to use the inbuilt email functionality, libraries would be a better bet:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Library {
public function __construct()
{
$this->CI =& get_instance();
}
public function send_email($to, $subject, $msg)
{
$this->CI->load->library('email');
$this->CI->email->from('my#example.com', 'My Name');
$this->CI->email->to($to);
$this->CI->email->subject($subject);
$this->CI->email->message($message);
$this->CI->email->send();
}
}
Then you can call it like so:
$this->load->library('my_library');
$this->my_library->send_email('test#example.com', 'RE: test message','cool message');

Resources