0

I have problem with email feature in codeigniter, i can't send email with codeigniter in some hosting, hosting provider said if their hosting can't support for phpmailer, and i tried to different hosting it work correctly.

Here my email source code :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class app_admin_site extends CI_Controller {

    public function index()
    {
        $cek = $this->session->userdata('logged_in');
        if(empty($cek))
        {
            header('location:'.base_url().'app_admin');
        }
        else
        {
            $config['protocol'] = 'smtp';
            $config['smtp_host'] = 'mail.com';
            $config['smtp_port'] = '25';
            $config['smtp_timeout'] = '7';
            $config['smtp_user'] = 'admin@mail.com';
            $config['smtp_pass'] = 'abcdefgh';
            $config['charset'] = 'utf-8';
            $config['newline'] = "\r\n";
            $config['mailtype'] = 'text'; // or html
            $config['validation'] = FALSE; // bool whether to validate email or not

            $page=$this->uri->segment(3);
            $limit=$this->config->item('limit_data');
            if(!$page):
            $offset = 0;
            else:
            $offset = $page;
            endif;

            $d['tot'] = $offset;
            $tot_hal = $this->db->query("select * from tbl_site order by id_site ASC");
            $config['base_url'] = base_url() . 'app_admin_site/index/';
            $config['total_rows'] = $tot_hal->num_rows();
            $config['per_page'] = $limit;
            $config['uri_segment'] = 3;
            $config['first_link'] = 'Awal';
            $config['last_link'] = 'Akhir';
            $config['next_link'] = 'Selanjutnya';
            $config['prev_link'] = 'Sebelumnya';
            $this->pagination->initialize($config);
            $d["paginator"] =$this->pagination->create_links();
            $d['data_get'] = $this->db->query("select * from tbl_site order by id_site ASC
            LIMIT ".$offset.",".$limit."");
            $this->load->view("app_admin/global/header",$d);
            $this->load->view("app_admin/site/home");
            $this->load->view("app_admin/global/footer");
        }
    }

    public function simpan()
    {
        if($this->session->userdata('logged_in')!="")
        {
            $this->form_validation->set_rules('siteid', 'Site ID', 'trim|required');
            $this->form_validation->set_rules('sitename', 'Site Name', 'trim|required');
            $id['id_site'] = $this->input->post("id_param");

            if ($this->form_validation->run() == FALSE)
            {
                $st = $this->input->post('st');
                if($st=="edit")
                {
                    $q = $this->db->get_where("tbl_site",$id);
                    $d = array();
                    foreach($q->result() as $dt)
                    {                   
                        $d['id_param'] = $dt->id_site;
                        $d['siteid'] = $dt->siteid; 
                        $d['sitename'] = $dt->sitename; 
                        $d['remark'] = $dt->remark; 
                    }
                    $d['data_site'] = $this->db->get("tbl_site");   
                    $d['st'] = $st;
                    $this->load->view('app_admin/site/input',$d);
                }
                else if($st=="tambah")
                {
                    $d['siteid'] = "";      
                    $d['sitename'] = "";    
                    $d['remark'] = "";  
                    $d['id_param'] = "";
                    $d['st'] = $st;
                    $d['data_site'] = $this->db->get("tbl_site");
                    $this->load->view('app_admin/site/input',$d);
                }
            }
            else
            {
                $st = $this->input->post('st');
                if($st=="edit")
                {
                    $upd['siteid'] = $this->input->post("siteid");
                    $upd['sitename'] = $this->input->post("sitename");
                    $upd['remark'] = $this->input->post("remark");
                    $this->db->update("tbl_site",$upd,$id);
                    ?>
                        <script>
                            window.parent.location.reload(true);
                        </script>
                    <?php
                }
                else if($st=="tambah")
                {           
                $this->load->library('email');  
                $datapesan['siteid'] = mysql_real_escape_string($this->input->post('siteid'));
                $datapesan['sitename'] = mysql_real_escape_string($this->input->post('sitename'));
                $datapesan['remark'] = mysql_real_escape_string($this->input->post('remark'));
                $isi_psn = 'Berikut data<br><br>                    
                            Detail Data<br>
                            Data Site ID : '.$datapesan['siteid'].'<br>
                            Site Name : '.$datapesan['sitename'].'<br>
                            Remark : '.$datapesan['remark'].'<br><br>
                            ';
                    $isi_psn .='Salam, Admin';
                    $this->email->set_mailtype('html');
                    $this->email->from("admin@mail.com", "Admin");
                    $this->email->to("xxx@mail.com");
                    $this->email->subject('Detail Data Input');
                    $this->email->message($isi_psn);    
                    $hsl = $this->email->send();
                    $this->email->clear();

                    $in['siteid'] = $this->input->post("siteid");
                    $in['sitename'] = $this->input->post("sitename");
                    $in['remark'] = $this->input->post("remark");
                    $this->db->insert("tbl_site",$in);
                    ?>
                        <script>
                            window.parent.location.reload(true);
                        </script>
                    <?php
                }

            }
        }
        else
        {
            header('location:'.base_url().'');
        }
    }


}

Hosting provider said if can used mail service with another methods, but they didn't give me a sample for another solution, is there any ideas to solve my problem in codeigniter?

nb: i already tried with different hosting my code work correctly, my web can sent email. But it can't work in my own hosting, so i need another solution

Please inform, thanks...

karisma
  • 156
  • 5
  • 18
  • Codeigniter provides email library try it [click here](https://www.codeigniter.com/user_guide/libraries/email.html) – Suneel Kumar Jul 20 '17 at 10:49
  • First, make one small demo of single PHP file to check if SMTP is working on your server or not. If it will not work then you need to change the SMTP to Gmail or any other good emails service provider. – Sahil Patel Jul 20 '17 at 10:52
  • i code above already worked in another hosting, but in my own hosting it can't work, they said it because my email method can't support it, if there any advice or another method with source code it will helpfull to understand... – karisma Jul 20 '17 at 10:57
  • change `$config['smtp_host'] = 'mail.com';` to `$config['smtp_host'] = 'mail.smtp.com';` – RJParikh Jul 20 '17 at 11:33

2 Answers2

0

Check out this link for creating an Email model which takes advantage of CI email library

https://github.com/ddell003/Email_model

Parker Dell
  • 472
  • 4
  • 11
0

Follow this code it can help you.

        $this->email->set_mailtype('html'); 
        $this->email->from(webmail_from, 'sender mail id');
        $this->email-
        >reply_to(webmail_from,'youth_career_applicants_registration');
        $this->email->to('receiver mail id'); 

        $data = array(
             'name'=> $this->input->post('txtFirstName'),
             'mail'=> $this->input->post('txtEmail'),
             'pass'=> $otp
        );
        $this->email->subject('Youth subject');
        $body = $this->load-
        >view('mail_templete/otp_details.php',$data,TRUE);          
        $this->email->message($body);
        if($this->email->send()){                                   
        }   
        else
        {           
        $data['message'] = $this->email->print_debugger();
        echo($this->email->print_debugger());
        }
dipti
  • 71
  • 7