CodeIgniter Forums

Full Version: Emailer class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]panos konstantinides[/eluser]
Hello this is more of a design question. In our web application we need to send an e-mail when a user subscribes to our web site. This e-mail is a simple text with three placeholders that get replaced with different values every time a new user subscribes.

So basically what I need to do is:

a) set the appropriate e-mail properties (to, subject, body, from e-mail address)
b) replace the placeholders in the text with the actual values
c) send the e-mail

How can I implement this e-mail functionality? Should everything go into a model, a library, a helper file or a custom class? Or maybe inside the controller itself?

I'd appreciate any help.

Regards

Panos

El Forum

[eluser]Flemming[/eluser]
Here's one suggestion. Create your email text, complete with placeholders (eg. %%NAME%% ) in a view.

email_content_view.php
Code:
Hello %%NAME%%, welcome to our site.

Load that view into a variable inside your controller:

Code:
$email_content = $this->load->view('email_content_view',TRUE)

then str_replace your placeholders

Code:
$email_content = str_replace('%%NAME%%', $some_variable, $email_content);

then send out your email, using $email_content as the $message

Code:
$this->email->message($email_content);

Hope that helps?

El Forum

[eluser]panos konstantinides[/eluser]
Hello Flemming, thanks for your prompt reply. So you actually suggest putting everything in the controller, except the e-mail text itself?

Regards

Panos

El Forum

[eluser]Flemming[/eluser]
Yep absolutely! Can't think of any reason why you shouldn't ... can you?

It makes sense, the controller being the place to do your logic. And I would consider email content to be display data so stick it into a view.

El Forum

[eluser]panos konstantinides[/eluser]
No I can't actually. My question was asked because in a previous discussion in here I was told to put all of my logic within my models. And with discussions I had with my colleague he suggested putting it in the models even if there isn't any database interaction involved. I am reluctant to do so because I believe this will break the notion of the models. What I did instead was to write my own class (I named it Emailer) that does all the email stuff and then instantiate it with the "new" keyword and then just call the sendEmail function that does all the work.

But again we have discussions about the way this works, since all of our code is codeigniter specific, and we think that this "new" instantiation, as well as an "include_once" statement (which includes the Emailer class) breaks the concept of codeigniter since CI gives you the ability to work with classes without having to worry about initialisation or inclusion. So I was wondering what other people think, hence my question.

Regards

Panos

El Forum

[eluser]uptime[/eluser]
Why not using the parser class instead?

It's quicker to code, already exist, has docs and most important CI-style code/architecture :-)


---


In general, I prefer to always write code as a library and not a modal.

I don't like the fact that the modal's variables become $CI's global variables (can you imagine having multiple variables called 'id'?).

Also, it makes it -a little- easier to migrate to other projects (in case they're not powered by CI).

El Forum

[eluser]panos konstantinides[/eluser]
[quote author="uptime" date="1259277492"]Why not using the parser class instead?

It's quicker to code, already exist, has docs and most important CI-style code/architecture :-)
[/quote]

I guess you mean to use the parser class to parse the text and populate it with data. Is it better than str_replace (basically I am concerned if it's faster).

[quote author="uptime" date="1259277492"]
In general, I prefer to always write code as a library and not a modal.

I don't like the fact that the modal's variables become $CI's global variables (can you imagine having multiple variables called 'id'?).

Also, it makes it -a little- easier to migrate to other projects (in case they're not powered by CI).[/quote]

We don't really think we will be migrating any time soon, but this code we write now will probably be used for other projects, which are also based on CI as well.

El Forum

[eluser]Flemming[/eluser]
Quote:in a previous discussion in here I was told to put all of my logic within my models. And with discussions I had with my colleague he suggested putting it in the models even if there isn’t any database interaction involved.

I think you should rethink where you're putting your logic!

El Forum

[eluser]uptime[/eluser]
[quote author="panos konstantinides" date="1259277995"]
We don't really think we will be migrating any time soon, but this code we write now will probably be used for other projects, which are also based on CI as well.
[/quote]

I'd probably never migrate away from CI but some old projects do not use it (though, I've slowly migrated nearly all of them).


[quote author="panos konstantinides" date="1259277995"]
I guess you mean to use the parser class to parse the text and populate it with data. Is it better than str_replace (basically I am concerned if it's faster).
[/quote]

Yeah, the built-in (CI) template parser library.

I care a lot about performance but I'm pretty sure what's going to slow it down is the mail, not the str_replace vs. parser class.

[edit]
By the way, nothing against str_replace()... just like to use production code than something that was just made.

If you're gonna use str_replace(), you should know that you could pass an array for the first two parameters, it may save you some time (just wasn't sure if you knew it or not).

El Forum

[eluser]garymardell[/eluser]
The parser class uses str_replace anyways.
Pages: 1 2