简体   繁体   中英

How to send html page as message when sending email in codeigniter?

I want to send a html page as message when sending email in codeigniter..

$this->email->set_mailtype("html");
$this->email->from('xyz@gmail.com', 'ABC');
$this->email->to($this->input->post('emailid'));            
$this->email->subject('New Subject');   

$message = //HERE I WANT TO INCLUDE A FILE TO END AS MESSAGE    

$this->email->message($message);        
$this->email->send();

Could it be possible ?? Please help...

You can simply use the method file_get_contents()

$message = file_get_contents("/path/to/htmlfile");

Also you can use the codeigniter way

$template = $this->load->view(APPATH.'email/file', $your_data, true);

What I have been using in my project, I created a function in a common_helper named as sendMail

function sendMail($subject, $mailContent, $mailTo, $mailFromId, $mailFromName)
{
    $CI =& get_instance();
    $CI->load->library('email');
    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $CI->email->clear(TRUE);
    $CI->email->initialize($config);
    $CI->email->from($mailFromId, $mailFromName);
    $CI->email->to($mailTo);
    $CI->email->subject($subject);
    $CI->email->message($mailContent);
    $CI->email->send();
}

You can call this function anywhere from your controller to send mail. Regarding HTML content of email should be loaded as Agam Banga mentioned above:

$mailContent = $this->load->view('email/template', $data, true);

This varibale can be passed simply calling like

sendMail($subject, $mailContent, $mailTo, $mailFromId, $mailFromName);

In your controller.

Let me know if you face any issue.

Here the code of Model and sending mail Using Email Library. And auto load library use it on your config folder -> autoload file $autoload['libraries'] = array('email');

public function mail_send($mdata){

          $this->load->library('email');
          $this->email->set_mailtype('html');
         //$this->email->set_newline("\r\n");
          $this->email->from($mdata['email']); // change it to yours
          $this->email->to('xyz@gmail.com');// change it to yours
          $this->email->cc($mdata['email']);
          $this->email->subject('Hello');
          //$this->email->message($mdata['address']);

          $body= $this->load->view('pages/mail', $mdata, true);
          $this->email->message($body);
          // echo '<pre>';
          // print_r($mdata);
          // exit();

          $this->email->send();


          $this->email->clear();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM