Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use $mail->SMTPDebug as a string?

Tags:

php

I want to echo "Fail" if strlen $mail->SMTPDebug is more than 10. But I don't know how to use $mail->SMTPDebug as a string. The below line in the function enables the debugging.

$mail->SMTPDebug  = 1;     

But can't use my if statement on it in my function. How can I do that ?

function mailsender($val,$yollayan,$sifresi,$name,$subject,$message) {

$mail = new PHPMailer();  
$mail->IsSMTP();               
$mail->SMTPDebug  = 1;          
$mail->SMTPAuth = true;         
$mail->SMTPSecure = "tls";      

$mail->Username   = $yollayan;
$mail->Password   = $sifresi;

$mail->Host = "smtp.live.com";  
$mail->Port = "587";  


$mail->From = $yollayan;
$mail->Fromname = $name;
$mail->name = $name;


$mail->Subject = $subject;  
$mail->Body = $message;  
$mail->AddAddress($val);   
$mail->send();

}
like image 378
user198989 Avatar asked Mar 23 '26 09:03

user198989


1 Answers

You have to make child class of PHPMailer and redefine edebug method to store output in variable:

class MyPHPMailer extends PHPMailer {

  public $DbgOut = '';

  private function edebug($str) {
    $this->DbgOut .= $str;
  }

}

And you call it like this:

function mailsender($val, $yollayan, $sifresi, $name, $subject, $message) {

  $mail = new MyPHPMailer();  
  $mail->IsSMTP();               
  $mail->SMTPDebug  = 1;          
  $mail->SMTPAuth = true;         
  $mail->SMTPSecure = "tls";      

  $mail->Username   = $yollayan;
  $mail->Password   = $sifresi;

  $mail->Host = "smtp.live.com";  
  $mail->Port = "587";  


  $mail->From = $yollayan;
  $mail->Fromname = $name;
  $mail->name = $name;


  $mail->Subject = $subject;  
  $mail->Body = $message;  
  $mail->AddAddress($val); 
  $mail->send();

  if(strlen($mail->DbgOut) > 10)
    echo 'Failed'.PHP_EOL;

}
like image 53
Roman Newaza Avatar answered Mar 24 '26 22:03

Roman Newaza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!