0

I'm trying to send a mail whit PHPmailer but get errors and don't know how to attack this problem. I hope somebody can help me whit this. (in the test phase this set-up works strangely enough)

I got this class which i call. :

class Mailer{

protected $mailer;
public function __construct($mailer) {
    $this->mailer = $mailer;
}

public function send($template, $data, $callback){
    require_once 'core/init.php';
    $message = new Message($this->mailer);

    extract($data);

    ob_start();
    require $template;
    $template = ob_get_clean();
    ob_end_clean();

    $message->body($template);

    call_user_func($callback, $message);//callback message

    $this->mailer->send();
}
}

Here is the point where i try to mail, and as said before, in the test fase this works whit ob_end_clean.

require 'classes/PHPMailer/PHPMailerAutoload.php';
    $mailer = new PHPMailer;
    require 'core/mailsettings.php';
    $mail = new Mailer($mailer);

    $mail->send('mailtemplate/new_bid.php',['name'=>Input::get('name'), 'activation_code' => $randomString1,'servername' => $_SERVER["SERVER_NAME"],'bid' => Input::get('bid'),'bidid' => $bidid, 'email' => Input::get('email')],function($m) {
                    $m->to(Input::get('email'));
                    $m->subject('Your input is in progress, please check your mail to activate.');
                });Redirect::to('index.php');

But when i try to send the mail I got an error from ob_end_clean.

Notice: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete

even if I try

 if (ob_get_length() > 0) { ob_end_clean(); }

as suggested here (failed to delete buffer. No buffer to delete).

Is there something I'm doing wrong in the class Mailer?? Or is there a better way to construct the public function send????

please advice, thank in advanced.

Community
  • 1
  • 1
hexedecimal
  • 279
  • 1
  • 2
  • 9

1 Answers1

2

You are cleaning the buffer twice:

$template = ob_get_clean();
ob_end_clean();

The second call will throw your error because the first already deleted the buffer.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • Almost right, the ob_end_clean() fails because ob_get_clean() also *deletes the buffer* – symcbean Dec 07 '15 at 10:52
  • Thanks, this seems to be usefull and maybe the isseu, unfortenaly i can only try if this works tonight.I'll give it a try tonight. @symcbean: I'm sorry but what do you mean? isn't your answer the same as from Gerald? – hexedecimal Dec 07 '15 at 11:04
  • --> So what do you suggest?? remove ob_end_clean sins it has no valeu or do something totaly diffrent?? – hexedecimal Dec 07 '15 at 11:07
  • 1
    Quote [from the documentation](http://php.net/manual/en/function.ob-get-clean.php): `ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean()`. Remove the second function, or replace the first with `ob_get_contents()`. It's up to you, the result is the same. – Gerald Schneider Dec 07 '15 at 11:08