cyberrspiritt
cyberrspiritt

Reputation: 926

Formatting HTML in codeigniter emails

I have to send an email to the user but the email must be HTML formatted. I've read other posts here but that didnt help.

Here's my code from controller:

public function reply_post(){

    $to = $this->input->post('contact_email');
    $subject = $this->input->post('contact_subject');
    $header_message = "<html><head><title>".$subject."</title></head><body>";
    $footer_message = "</body></html>";
    $input_msg = $this->input->post('contact_reply');
    $msg = $header_message.$input_msg.$footer_message;
    $from = "[email protected]";

    $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'mail.XXXXXX.com',
      'smtp_port' => XXX,
      'smtp_user' => '[email protected]', // change it to yours
      'smtp_pass' => 'XXXXX', // change it to yours
      'mailtype' => 'html',
      'charset' => 'iso-8859-1',
      'wordwrap' => TRUE
    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from($from, "Admin"); // change it to yours
    $this->email->to($to);// change it to yours
    $this->email->subject($subject);
    $this->email->message($msg);

    if($this->email->send()){
      $msg = 'Email sent.';
    } else {
        log_message('error','Email did not send');
        $msg = "Email Did not send";
    }
    $this->finish($msg);
}

The message that i get from the form is via Summernote WYSIWYG editor so it is already HTML formatted. However, when i receive the email, It doesnt apply HTML tags to it and all of the tags are visible. Its like somehow the mailtype is set to 'text', even after setting it to HTML in the config.

Upvotes: 3

Views: 2350

Answers (1)

Linus
Linus

Reputation: 909

Setting config options doesn't work unless you initialize them. Add this after your $config array:

$this->email->initialize($config);

So you didn't intealize it So it didn't work further adding

 adding $this->email->set_mailtype("html");

may solve your problem.

Load library then initialize

Upvotes: 3

Related Questions