1

I've never had issues sending emails before through CodeIgniter, but all of a sudden it's an issue on my latest project.

I'm using this mail server tool to send mail on my local-host, which has never given me any issues before, and then the CodeIgniter email library.

I can get one of 2 results: either the email sends, but all the raw HTML source code is displayed, or the email sends and has a subject line, but the entire body is blank.

This is my email_helper.php

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

function send_email($to, $subject, $template)
{
    $ci = &get_instance();
    $ci->load->library('email');

    $config = array(
      'mailtype'  => 'html',
      'newline' => '\r\n',
      'crlf'  => '\r\n'
    );

    //If I comment out this line, it sends raw HTML, otherwise it sends a blank body.
    $ci->email->initialize($config);

    $ci->email->from($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
    $ci->email->reply_to($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
    $ci->email->to($to);

    $ci->email->subject($subject);
    $ci->email->message($ci->load->view('email/' . $template . '_html', $data, TRUE));
    $ci->email->send();
}

this is my test_html.php

<!DOCTYPE html>
<html>
  <head><title>Test</title></head>
  <body>
      <div style="max-width: 800px; margin: 0; padding: 30px 0;">
       TEST!
      </div>
  </body>
</html>

And then I'm calling the email helper from my controller with this:

$this->load->helper('email_helper');
send_email($this->input->post('email'), 'Test Subject', 'test');
Pradeep
  • 9,667
  • 13
  • 27
  • 34
hyphen
  • 2,368
  • 5
  • 28
  • 59

1 Answers1

1

Hope this will help you :

You r missing $data there in your load view section and also try with $ci->load->library('email', $config); instead of $ci->email->initialize($config);

send_email should be like this :

function send_email($to, $subject, $template)
{
    $ci = & get_instance();

    $config = array(
      'mailtype'  => 'html',
      'charset' => 'iso-8859-1',
      'newline' => '\r\n',
      'crlf'  => '\r\n'
    );

    $data = '';

    $body = $ci->load->view('email/' . $template . '_html', $data, TRUE);
    echo $body; die;

    $ci->load->library('email', $config);
    $ci->email->set_mailtype("html");
    $ci->email->from($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
    $ci->email->reply_to($ci->config->item('from_email_address'), $ci->config->item('from_email_name'));
    if ($to) 
    {
      $ci->email->to($to);

      $ci->email->subject($subject);
      $ci->email->message($body);
      if ($ci->email->send())
      {
        return TRUE;
      }
      else
      {
        echo $ci->email->print_debugger();die;
      }
    }
}

For more : https://www.codeigniter.com/user_guide/libraries/email.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34