Codeigniter how to html template for emails

2019-02-01 19:24发布

问题:

I'm using below code to send email from my codeigniter based website's contact form, to do that i'm using below codes in my controller,

    $entrydata['name']= $this->input->post('name');
    $entrydata['email']= $this->input->post('email');
    $entrydata['phone']= $this->input->post('phone');
    $entrydata['message']= $this->input->post('message');$msg = 'Email has sent successfully';

            $data['reset'] = TRUE;
            $this->load->library('email');
            $this->email->from(set_value('email'), set_value('name'));
            $this->email->to('my@webs.com');
            $this->email->subject("Get a quote enquiry");
            $all = 'Name:' . set_value('name') ."\n". 'Email :' .' '. set_value('email') ."\n".'Phone :' .' '. set_value('phone') ."\n".'Message :' .' '. set_value('message'); $this->email->message($all);
            $s=$this->email->send();
            $data['message'] = $msg;

Did anyone know how to add custom email template that'll hold my conact form informations?

回答1:

This is an example which i have done

 $data['map_to']=$this->input->post('map_to');
        $event=$this->db->query("query");
        if($event->num_rows()>0)
        {
            $data['event']=$event->row();
            $data['map_from']=$event->row()->address2;
        }
        else
        {
        $data['event']=NULL;    
        }
      $data['sender_mail'] = 'xx@xx.org';

        $this->load->library('email');
        $config = array (
                  'mailtype' => 'html',
                  'charset'  => 'utf-8',
                  'priority' => '1'
                   );
        $this->email->initialize($config);
        $this->email->from($data['sender_mail'], 'xxxx');
        $this->email->to($mail);
        $this->email->subject('Map Location');
        $message=$this->load->view('map_mail_format',$data,TRUE);
        $this->email->message($message);
        $this->email->send();       

Here i am loading a view page called map_mail_format and passing values to that view page ($data) then assign that view page to a variable then send mail with that message



回答2:

first you need to initialize config as

$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);

after you load email library and then you can store your email view to a variable and mail it

$mailbody = $this->load->view('myfile', $data, true);


回答3:

You may try this

$this->load->helper(array('email'));
$this->load->library(array('email'));
$this->email->set_mailtype("html");