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();
}
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;
}
I'm guessing you want to do something with the debug log? class.smtp.php (lines 114-126) reads:
/** * How to handle debug output. * Options: * * `echo` Output plain-text as-is, appropriate for CLI * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output * * `error_log` Output to error log as configured in php.ini * * Alternatively, you can provide a callable expecting two params: a message string and the debug level: * <code> * $smtp->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; * </code> * @type string|callable */
So in your code, simply
$mail->Debugoutput = function($str, $level) { do_something_with($str); };