CodeIgniter Forums
Email Class: Send dynamic text from file? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Email Class: Send dynamic text from file? (/showthread.php?tid=9101)



Email Class: Send dynamic text from file? - El Forum - 06-11-2008

[eluser]kabbink[/eluser]
Hello all!

This might be a fairly simple question but I've been working all day so maybe I've just lost it!

All I want to do is be able to send an email (preferably using the built in email library)
which is stored in a separate file but also has some dynamic variables in it.

Here is some pseudo code of what I am trying to do:

I would have a file called email.php containing something similar to:

Code:
Dear {$customer_name},

We appreciate your business!

Then send the email with something like:

$data['customer_name'] = 'Joe';
$this->email->message(email.php, $data);

Hope that made sense!
Thanks for all the help!
Kevin


Email Class: Send dynamic text from file? - El Forum - 06-12-2008

[eluser]freshface[/eluser]
Why not use a view?

$data[’customer_name’] = ‘Joe’;
$content = $his->load->view("email/template1",$data);
$this->email->message($content);


Email Class: Send dynamic text from file? - El Forum - 06-12-2008

[eluser]kabbink[/eluser]
Great! That is exactly what I wanted I just wasnt sure I could do it that way. I am an MVC & OO noob!

Thanks!


Email Class: Send dynamic text from file? - El Forum - 06-12-2008

[eluser]sophistry[/eluser]
freshface's code example won't work ( not least because of one obvious typo using the variable $his instead of $this). you need to put TRUE as the third parameter to the this->load->view() call to get it to return the result as a string.


Email Class: Send dynamic text from file? - El Forum - 06-12-2008

[eluser]Popcorn[/eluser]
I use something similar in my authentication library to find "{key}" and then replace this with the real value.

The first parameter is the string you are looking for, the 2nd is what you are replacing it with and the 3rd is the message you are finding "{key}" in. You can assign this new message into your email script.

Code:
$message = str_replace('{key}', $key, $this->forgotten_password_message);

Kind Regards,
-Mathew