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');
Related
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');
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()
I'm writing a CodeIgniter helper for e-mail functions, and need currently in each function I'm writing:
$CI = &get_instance();
$CI
->email
//etc
It's not a big problem, but I was wondering if there was a way of loading the CI instance once for all functions? Something like a constructor method?
If you are absolutely set on defining your functions inside of a helper rather than extending CodeIgniter's email library then you can do this:
email_helper.php
<?php
// Assuming $CI has not been set before this point
$CI = &get_instance();
function some_email_function()
{
$GLOBALS['CI']->email;
}
unset($CI);
You can actually do this.
Here's an example
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Example {
public function __construct()
{
$this->CI =& get_instance();
}
public function functionExample(){
$this->CI->load->module('fullpage');
if($this->CI->fullpage->my_function() ){
echo 'It works!';
}
}
}
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?
I have an app using CI 1.7.2.
In controller i have $data['title'] = 'Sample Title';
In view
<?php echo $title; ?>
Should print it.
But not working.
I have another app with latest CI, in that passing like this works fine.
Anybody know reason for this strange issue?
You should pass the data to view like,
$this->load->view('view_file', $data);
There is another way to pass data to view use the library parser
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('parser');
}
public function index() {
$this->sssssssss();
}
function sssssssss($data = '') {
$header = $this->parser->parse('interface', array(), TRUE);
$data['interface'] = $header;
//bind all together and parse to template view
$this->parser->parse('template_view', $data);
}
}