CodeIgniter Forums

Full Version: Storing email copy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]minoflow[/eluser]
I'm building a site where I have a few functions in my controller that send out emails. One for example will send an email if someone forgets their password. The email will contain their password as well as information about their account pulled from the database.

Everything works great, my only issue is that I don't like having all of the email's subject copy in my controller. Is there a way to take the copy and store it in a separate file? I've tried creating a config file, the problem was that the variables in the email subject were not being recognized and were throwing errors.

It seems like a language file would make the most sense but I might run into the same problems with the variables.

Any thoughts or ideas?

El Forum

[eluser]Derek Jones[/eluser]
I'd use a language file. You'll be loading the language file and accessing the variables via $this->lang->line(), so you won't have any undefined variable issues.

El Forum

[eluser]minoflow[/eluser]
Awesome, I'll give that a try. Thanks for the quick advise

El Forum

[eluser]minoflow[/eluser]
So I made my language file and defined a lang line with the following text

language/english/emails_lang.php
Code:
$lang['email_resetpassword'] = 'Hey '.$email_vars->name.',

The password to your portfolio has been reset:

------------------------------------

Password: '.$new_password.'

------------------------------------

Login at http://'.$email_vars->url.'.portfolio.site/admin

Be sure to login and change it to something more memorable at:
http://'.$email_vars->url.'.portfolio.site/admin/settings'

controllers/admin.php
Code:
$this->load->library('email');
$this->lang->load('emails', 'english');
$this->email->from('[email protected]', 'Folio.com');
$this->email->to($email_vars->email);            
$this->email->subject('Password Reset For ('.$email_vars->url.')');
$this->email->message($this->lang->line('email_resetpassword'));
$this->email->send();

I'm still getting errors that the variables within the lang line are undefined. The function is defining this within my controller. I was hoping that the language file could store the reference to the variables then my function could pick it up and define them. Looks like it can't?

El Forum

[eluser]Derek Jones[/eluser]
That wouldn't work unless you defined $email_vars as a global object, which seems wasteful. Why not just use pseudo variables and let your controller replace it?

Code:
$lang['email_resetpassword'] = 'Hey {name},

The password to your portfolio has been reset:

------------------------------------

Password: {new_password}

------------------------------------

Login at http://{url}portfolio.site/admin

Be sure to login and change it to something more memorable at:
http://{url}portfolio.site/admin/settings';

And replace the {variables} with the actual data in the controller.

El Forum

[eluser]minoflow[/eluser]
Rad, that worked... I used the code below to do a replace. Is this the best way to go about doing this? Is there a helper or something built into CI to do pseudo code replacements?

Code:
$subject = $this->lang->line('email_resetpassword');
$pseudo_vars = array("{name}", "{url}", "{password}");
$real_vars = array($email_vars->name, $email_vars->url, $new_password);
$new_subject = str_replace($pseudo_vars, $real_vars, $subject);

Thanks for your help!

El Forum

[eluser]Derek Jones[/eluser]
There's a Parser class that does something similar, but it's intended to operate on View files. So instead of a Language file solution, you'd have a view file for each email template.

Code:
$vars = array(
    'name' => $email_vars->name,
    'url' => $email_vars->url,
    'password' => $new_password
);

$message = $this->parser->parse('email_resetpassword', $vars, TRUE);