Welcome Guest, Not a member yet? Register   Sign In
Having trouble getting Email library to load
#1

[eluser]ihateregisteringforastupidforum[/eluser]
Using CI 1.7.2.

So I'm trying to make OpenVBX use CI's Email library, rather than php's own mail(), because on some hosts you cannot use that. No mail() function, just SMTP access.

I don't get why, but every time I try to add $this->load->library('email'); inside the function that is supposed to handle sending emails, I get a blank page. PHP just refuses to parse past that point. What am I missing?

Original Code:
Code:
function openvbx_mail($recipient, $subject, $template, $maildata = array())
{
    error_log('mailing');
    $path = APPPATH . 'views/emails/' . $template . '.php';
    $ci = &get;_instance();
    $domain = $_SERVER['HTTP_HOST'];
    $from_email = $ci->settings->get('from_email', $ci->tenant->id);
    if(empty($from_email))
    {
        $from_email = "$from <do-not-reply@$domain>";
    }
    
    $headers = "From: $from_email";

    /* Render the mail template */
    ob_start();
    extract($maildata);
    include($path);
    $message = ob_get_contents();
    ob_end_clean();

    if($ci->config->item('log_threshold') > 2)
    {
        error_log($message);
    }

    return mail($recipient, "[OpenVBX] " . $subject, $message, $headers);
}

So I can see that mail() can be replaced with $this->email->send();, or better off, to keep OpenVBX happy, I can do a if(!$this->email->send()Wink { return FALSE; } and include return TRUE; after.

Modified code:
Code:
function openvbx_mail($recipient, $subject, $template, $maildata = array())
{
    error_log('mailing');
    $path = APPPATH . 'views/emails/' . $template . '.php';
    $ci = &get;_instance();
    $domain = $_SERVER['HTTP_HOST'];
    $from_email = $ci->settings->get('from_email', $ci->tenant->id);
    if(empty($from_email))
    {
        $from_email = "$from <do-not-reply@$domain>";
    }
    
    $headers = "From: $from_email";

    /* Render the mail template */
    ob_start();
    extract($maildata);
    include($path);
    $message = ob_get_contents();
    ob_end_clean();

    if($ci->config->item('log_threshold') > 2)
    {
        error_log($message);
    }
    $this->load->library('email');
    $this->email->from($from_email, 'Company name here');
    $this->email->to($recipient);
    $this->email->subject($subject);
    $this->email->message($message);    
    if(!$this->email->send()) { return false; } else { return true; }
    //return mail($recipient, "[OpenVBX] " . $subject, $message, $headers);
}

Problem is, like I said, if I try to get the Email library loaded, I get a white page, php stops parsing.

I don't know why but I can't even get PHP to output all messages (and errors). I have the proper php.ini params set... beats me!

Any ideas would be appreciated - including helping me get to see the error messages at least. But trying to fix it blindly gets frustrating quick.
EDIT: I found config.php and noticed that I can turn debugging on there. It was overriding my php.ini.
#2

[eluser]dudeami0[/eluser]
Code:
$this->load->library('email');
    $this->email->from($from_email, 'Company name here');
    $this->email->to($recipient);
    $this->email->subject($subject);
    $this->email->message($message);    
    $return = $this->email->send();
    $this->email->print_debugger();
    return $return;
See what the debugger says, if it says anything.
#3

[eluser]ihateregisteringforastupidforum[/eluser]
I think I get it now. OpenVBX devs did it completely backwards or I'm trying to do this backwards.

So apparently, what I'm editing is a helper. Helpers cannot use $this. Controllers can. Which can call upon helpers. But helpers can be extended, yet not in the way that would allow for libraries to be loaded.

Pffft. God help me.
#4

[eluser]ihateregisteringforastupidforum[/eluser]
[quote author="dudeami0" date="1290577238"]
Code:
$this->load->library('email');
    $this->email->from($from_email, 'Company name here');
    $this->email->to($recipient);
    $this->email->subject($subject);
    $this->email->message($message);    
    $return = $this->email->send();
    $this->email->print_debugger();
    return $return;
See what the debugger says, if it says anything.[/quote]

Debugger says
Code:
Fatal error: Using $this when not in object context in /OpenVBX/helpers/mail_helper.php on line 104

I'm trying to do it backwards. I need to probably load the email library in the controller that calls upon this helper. I hope the helper can then use $this->email->send();.
No, I can't do that either.
Code:
Fatal error: Call to a member function library() on a non-object in /OpenVBX/models/vbx_user.php on line 202
#5

[eluser]dudeami0[/eluser]
Code:
$ci =& get_instance(); // Place at the top of helper

then replace all the $this in the helper with $ci.
#6

[eluser]ihateregisteringforastupidforum[/eluser]
[quote author="dudeami0" date="1290578222"]
Code:
$ci =& get_instance(); // Place at the top of helper

then replace all the $this in the helper with $ci.[/quote]

That actually makes sense, since $ci is already instantiated in this particular function (by OpenVBX devs).

However, it still complains about overloading ..bla bla bla. I hate it that I don't know what the heck that means.
Code:
Fatal error: Cannot assign by reference to overloaded object in /system/libraries/Model.php on line 69
#7

[eluser]dudeami0[/eluser]
Try

Code:
$ci = get_instance(); // Place at top of helper

That should remove the overloaded object error.
#8

[eluser]ihateregisteringforastupidforum[/eluser]
[quote author="dudeami0" date="1290579026"]Try

Code:
$ci = get_instance(); // Place at top of helper

That should remove the overloaded object error.[/quote]

It won't make a difference, it seems.

Code:
function openvbx_mail($recipient, $subject, $template, $maildata = array())
{
    error_log('mailing');
    $path = APPPATH . 'views/emails/' . $template . '.php';



// $ci is instantiated here, by default with &. If removed, I get the same error.
    $ci = &get;_instance();



    $domain = $_SERVER['HTTP_HOST'];
    $from_email = $ci->settings->get('from_email', $ci->tenant->id);
    if(empty($from_email))
    {
        $from_email = "$from <do-not-reply@$domain>";
    }
    
    $headers = "From: $from_email";

    /* Render the mail template */
    ob_start();
    extract($maildata);
    include($path);
    $message = ob_get_contents();
    ob_end_clean();

    if($ci->config->item('log_threshold') > 2)
    {
        error_log($message);
    }   //this is my way of retrieving my newly created password. else I can't login...
        $myFile = "loggingmail.log";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $message);
        fclose($fh);



// if I were to get another $ci here, without &, it would still complain about overloading the object.



    $ci->load->library('email');        
    $ci->email->from($from_email, 'Company name here');
    $ci->email->to($recipient);
    $ci->email->subject($subject);
    $ci->email->message($message);    
    $return = $this->email->send();
    $this->email->print_debugger();
    return $return;

}

The error stays the same.
Code:
Fatal error: Cannot assign by reference to overloaded object in /system/libraries/Model.php on line 69
#9

[eluser]ihateregisteringforastupidforum[/eluser]
[quote author="dudeami0" date="1290579026"]Try

Code:
$ci = get_instance(); // Place at top of helper

That should remove the overloaded object error.[/quote]

I am very appreciative of your help and time. I will try to employ a Pear::Mail to send my emails. That I know how to work with.

Have a wonderful night.
#10

[eluser]ihateregisteringforastupidforum[/eluser]
A few questions, for coders/admins stumbling upon this post:

1. Why aren't you supposed to load and use a library within a helper?
2. Why aren't helpers getting the vars of the controller/model that called it in the first place? That seems to me like it could be very useful.




Theme © iAndrew 2016 - Forum software by © MyBB