1

My apologies if I misstate something or use an incorrect term. But I was trying to figure out how to use an if statement on an object.

This works just fine and as I understand it, the @ suppresses errors.

if (@mail("$receiver_email", "$subject", "$message", "$headers")) { }

However If I do this it fails:

if (@$mail->send('email@gmail.com', "$receiver_email", "$subject", "$message")) {}

The irony is that the email goes through but the IF fails. How can I fix the IF statement so it works properly?

Maelish
  • 1,600
  • 4
  • 18
  • 25

2 Answers2

0

First: try to avoid @, it's generally a bad practice..

Why the if fails does not depend wether the sending was succesful. Only if the mail() function in whatever class $mail is actually returns something (like true or false) you can do something with this.

Since this is not a built-in PHP class, you should ask the authors of your class (or yourself, if you wrote it) to return true on success.

In short: this is not something you can fix, the person who wrote the send function can fix this.

Evert
  • 93,428
  • 18
  • 118
  • 189
0

First of all, don't use @. It's bad practice and better dealt with using error handling routines; see also: Error logging, in a smooth way

From your code I can only guess that your code is like this:

public function send($from, $to, $subject, $message)
{
    // your code here
}

If the function doesn't have a return value, it defaults to null and that evaluates to false which is why the IF "fails".

You should return a success value from your send() method.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309