Welcome Guest, Not a member yet? Register   Sign In
RESOLVED: include() Heredoc in Controller
#1

[eluser]tkaw220[/eluser]
Hi,

I ran into below situation, and hopefully I could get some helps here.

I have a controller to send out newsletter. I used PHP heredoc to avoid the need to escape string:

Code:
class Newsletter extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

        public function send()
    {

             $message = <<<"MESSAGE"

<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;/body&gt;

&lt;/html&gt;

MESSAGE;

         $config['mailtype'] = 'html';
        
         $this->email->initialize($config);
        }

        public function another_func()
        {
             another function here ...
        }

} // end controller

1) My first headache: I dislike the indentation of my controller (at the end of the delimiter), how could I make the line indent consistent?

2) Is it possible to exclude the HTML newsletter (the HTML code) from my controller and include it by using PHP include() before calling the CI send email class?

For example:

Code:
private function _get_file()
    {
        $message = include('../../newsletter.php');
        
        return $message;
    }

        public function send()
    {
                $message = $this->_get_file(); // include the HTML code before send

                $config['mailtype'] = 'html';
        
                $this->email->initialize($config);

                $this->email->from( ...........
        }

I include the external markup before the send mail function, but the outcome is an empty email.

I tried to describe my issue as detail as I can. Hopefully I could get some help here.

Thank you.

Regards,
Edwin
#2

[eluser]CroNiX[/eluser]
Why not store your email html content as a view? Then just use the third parameter to return the view as a variable instead of outputting it to the browser?

View:
email_view.php
Code:
<!DOCTYPE HTML>
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php echo $content; ?&gt;
&lt;/body&gt;

&lt;/html&gt;

Then in your controller:
Code:
public function send($title, $content)
{
    $email['title'] = $title;
    $email['content'] = $content;
    $message = $this->load->view('email_view', $email, TRUE); // include the HTML code before send

    $config['mailtype'] = 'html';
        
    $this->email->initialize($config);

    $this->email->from( ...........
}
#3

[eluser]tkaw220[/eluser]
Hi CroNix,

Your codes work perfectly. Thank you very much. Now my controller is clean and neat.

Have a nice weekend.

Regards,
Edwin




Theme © iAndrew 2016 - Forum software by © MyBB