user1125551
user1125551

Reputation:

Conditional Causes Code to Fail

I am writing a small email-to-text script, and it seems that every time I wrap the "doMail()" function in an if-statement, it fails, or within the function, if I wrap the mail() function with an if-statement, it also fails.

However, if I remove the conditionals, it works like a charm? What should I do and what could be the cause of this?

EDIT: By "...it fails," I mean it echoes "Something went wrong!" just as it should according to the code.

Here is the code:

<?php
error_reporting(E_ALL);
session_start();
if (!$_SESSION['doesExist'] == true) 
    {
        echo "Session doesn't exist!";
        die();
    }

if (!isset($_POST['num']) || !isset($_POST['carrier']) || !isset($_POST['msg'])) 
    {
        echo "Failed!";
        die();
    }

$num = $_POST['num'];
$car = $_POST['carrier'];
$msg = $_POST['msg'];
$subject = '';
$head = 'From: [email protected]' . "\r\n";

switch ($car)
    {
        case "att":
            $num .= "@txt.att.net";
            break;
        case "verizon":
            $num .= "@vtext.com";
            break;
        case "tmobile":
            $num .= "@tmomail.net";
            break;
    }

function doMail($toNum, $sub, $message, $headers)
    {
        if(mail($toNum, $sub, $message, $headers))
            {
                echo "Done!";
            }
        else
            {
                echo "Something went wrong!";
            }
    }

doMail($num, $subject, $msg, $head);

?>

Upvotes: 2

Views: 84

Answers (1)

kittycat
kittycat

Reputation: 15044

Apparently according to this comment the mail() function may return an empty string instead of a boolean value of TRUE and as such will be evaluated as FALSE in a conditional statement as an empty value is considered FALSE.

Upvotes: 1

Related Questions