Welcome Guest, Not a member yet? Register   Sign In
Sending HTML Emails from Model
#1

[eluser]jdav3579[/eluser]
Hi,
I would normally send emails from my controllers, but unfortunately in this instance its not possible for me to refactor the code base in order to do this.
The message sending part of system is located in the model, it can send plain text type emails but not html as this is a preference which I appear not to be able to set from within the model.

Can anyone suggest a remedy for this??
#2

[eluser]mddd[/eluser]
Why would you not be able to set this from a model? I don't believe that is correct.
You should be able to set it to html email using
Code:
$config['mailtype'] = 'html';
$this->email->initialize($config);
Maybe you can show us your code for sending the email?
#3

[eluser]jdav3579[/eluser]
Hi,
Thanks for the fast reply!,
I did try that, but got this message below! Then I tried it different ways, but I kept getting other error messages instead, so assumed it was to do with being in the model!

The class is called messaging, so the function is the constructor!

Code:
function messaging(){
        parent::Model();
        $this->load->library('email');
        $config['mailtype'] = 'html';
        $this->email->initialize($config);
    }

Results in:

code]
Severity: Notice

Message: Undefined property: messaging::$email

Filename: models/messaging.php

Line Number: 10
[/code]
#4

[eluser]mddd[/eluser]
Check out this post.
If you can't access a loaded library right away, you can force CI to check again for all loaded libraries: $this->_assign_libraries();
I think this may have to do with the fact that you are loading and accessing from the constructor.
You could also try to load the email library in the constructor, and then initialize it later (in the function before you start sending email).
Or you could put the email config in a config file so it is loaded automatically.
#5

[eluser]jdav3579[/eluser]
Thanks for that!
I did try both of those, tried this too:
Code:
function messageProfiles($message,$subject,$profiles){
        $this->email->from('mailer@'.$this->config->item('site_name'), 'mailer@'.$this->config->item('site_name'));
        $config['mailtype'] = 'html';
        $this->email->initialize($config);

        //code here
        }
but this causes it not to recognise the 'from' setter! and throws PHP mail errors!
#6

[eluser]mddd[/eluser]
I'm afraid I'm out of ideas then.. I know it is not that you can't use the email library from a model because I have done so on different occasions without problems.
Maybe someone else has ideas?
One last stupid question: you're not using another variable called $email in that class?
#7

[eluser]jdav3579[/eluser]
Thanks for all your help anyway! Much appreciated. Not using variable called email, I cant see anything wrong with it all ! I reckon I'm probably going to have to refactor it!




Theme © iAndrew 2016 - Forum software by © MyBB