We help IT Professionals succeed at work.

If then statement within HTML email

lepirtle
lepirtle asked
on
I have an if then statement whose results I want to display in the body of an html email. I know the email script is correct and the if then statement displays properly if I run it on its own php page but when I try to place the code within my php html email I receive a syntax error.

Might someone look at the attached code and advise?
Thanks.
<?php
// multiple recipients
$to  = 'me@mydomaine.com' . ', '; // note the comma
$to .= 'you@your.com';

// subject
$subject = 'This is subject line';

// message
$message = '
<html>
<head>
  <title>Telephone number and optional extension</title>
</head>
<body>
  <table>
	<tr>
		<td>'.if(strlen($_POST['extension']) >0)
			{echo $_POST['areacode'].'-'.$_POST['exchange'].'-'.$_POST['telno'].' ext '.$_POST['extension'];}
			else
			{echo $_POST['areacode'].'-'.$_POST['exchange'].'-'.$_POST['telno'];}.'</td>
	</tr>
  </table>		
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: me <me@mydomain.com>, john <johnr@john.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <someplace@example.com>' . "\r\n";
$headers .= 'Cc: $_POST['email']' . "\r\n";
$headers .= 'Bcc: tgifl@domain.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

Open in new window

Comment
Watch Question

// message
$message = '
<html>
<head>
  <title>Telephone number and optional extension</title>
</head>
<body>
  <table>
        <tr>
                <td>';
if(strlen($_POST['extension']) >0){
   $message .= $_POST['areacode'].'-'.$_POST['exchange'].'-'.$_POST['telno'].' ext '.$_POST['extension'];
} else {
   $message .= $_POST['areacode'].'-'.$_POST['exchange'].'-'.$_POST['telno'];
}
$message .= '</td>
        </tr>
  </table>              
</body>
</html>
';

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
You can use a function like this to filter and normalize a telephone number.  Just concatenate the POST fields and feed them to the function.

You might also find HEREDOC notation to be very useful for constructing long strings with variable content, such as HTML email messages.
http://php.net/manual/en/language.types.string.php
<?php // RAY_phone_numbers.php - USING USA PHONE NUMBERS
error_reporting(E_ALL);

// A FUNCTION TO VALIDATE A PHONE NUMBER AND RETURN A NORMALIZED STRING
// MAN PAGE: http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=102667&ixReplies=15
function strtophone($phone, $format=FALSE, $letters=FALSE, $dlm='-')
{
    if ($letters)
    {
        // TRANSLATE INPUT LIKE 1-800-BIG-DOGS
        $phone = strtoupper($phone);
        if (preg_match('/[A-Z]/', $phone))
        {
            $phone = str_replace('A', '2', $phone);
            $phone = str_replace('B', '2', $phone);
            $phone = str_replace('C', '2', $phone);

            $phone = str_replace('D', '3', $phone);
            $phone = str_replace('E', '3', $phone);
            $phone = str_replace('F', '3', $phone);

            $phone = str_replace('G', '4', $phone);
            $phone = str_replace('H', '4', $phone);
            $phone = str_replace('I', '4', $phone);

            $phone = str_replace('J', '5', $phone);
            $phone = str_replace('K', '5', $phone);
            $phone = str_replace('L', '5', $phone);

            $phone = str_replace('M', '6', $phone);
            $phone = str_replace('N', '6', $phone);
            $phone = str_replace('O', '6', $phone);

            $phone = str_replace('P', '7', $phone);
            $phone = str_replace('Q', '7', $phone);
            $phone = str_replace('R', '7', $phone);
            $phone = str_replace('S', '7', $phone);

            $phone = str_replace('T', '8', $phone);
            $phone = str_replace('U', '8', $phone);
            $phone = str_replace('V', '8', $phone);

            $phone = str_replace('W', '9', $phone);
            $phone = str_replace('X', '9', $phone);
            $phone = str_replace('Y', '9', $phone);
            $phone = str_replace('Z', '9', $phone);
        }
    }

    // DISCARD NON-NUMERIC CHARACTERS
    $phone = preg_replace('/[^0-9]/', NULL, $phone);

    // DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
    if (substr($phone,0,1) == '1') $phone = substr($phone,1);

    // IF LESS THAN TEN DIGITS, IT IS INVALID
    if (strlen($phone) < 10) return FALSE;

    // IF IT STARTS WITH '0' OR '1' IT IS INVALID, SECOND DIGIT CANNOT BE '9' (YET)
    if (substr($phone,0,1) == '0') return FALSE;
    if (substr($phone,0,1) == '1') return FALSE;
    if (substr($phone,1,1) == '9') return FALSE;

    // ISOLATE THE COMPONENTS OF THE PHONE NUMBER
    $ac = substr($phone,0,3); // AREA
    $ex = substr($phone,3,3); // EXCHANGE
    $nm = substr($phone,6,4); // NUMBER
    $xt = substr($phone,10);  // EXTENSION

    // ADD OTHER TESTS HERE AS MAY BE NEEDED - THESE ARE FOR LOCAL APPS
    if ($ac == '900') return FALSE;
    if ($ac == '976') return FALSE;
    if ($ex == '555') return FALSE;

    // IF NOT FORMATTED
    if (!$format) return $phone;

    // STANDARDIZE THE PRINTABLE FORMAT OF THE PHONE NUMBER LIKE 212-555-1212-1234
    $formatted_phone = $ac . $dlm . $ex . $dlm . $nm;
    if ($xt != '') $formatted_phone .= $dlm . $xt;
    return $formatted_phone;
}



// DEMONSTRATION OF THE FUNCTION IN ACTION.
if (!empty($_GET["phone"]))
{

    // VALIDATE PHONE USING FUNCTION ABOVE
    if (!$phone = strtophone($_GET["phone"], TRUE))
    {
        // FUNCTION RETURNS FALSE IF PHONE NUMBER IS UNUSABLE
        echo "BOGUS: {$_GET["phone"]} ";
    }
    else
    {
        // SHOW THE FORMATTED PHONE
        echo "VALID: {$_GET["phone"]} == $phone";
    }
}


// END PHP, PUT UP THE FORM TO TEST PHONE NUMBERS
?>
<form>
ENTER A PHONE NUMBER:
<input name="phone" /><br/>
<input type="submit" />
</form>
TRY SOME OF THESE (COPY AND PASTE):
<br/>1-800-5551212
<br/>202-537-7560
<br/>202 537 7560
<br/>1-202-537-7560
<br/>(202) 537-7560
<br/>1.202.537.7560
<br/>123456789
<br/>703-356-5300 x2048
<br/>(212) 555-1212
<br/>1 + (212) 555-1212
<br/>1 (292) 226-7000

Open in new window

Author

Commented:
Thanks so very much. I played with this for more than an hour and you solved it in a couple minutes. Amazing!
Thanks again.

Author

Commented:
Ray Paseur,
Thanks for that information. As you can tell, I am very much a novice with PHP so I will experiment with the information you provided and add it to my 'toolbox'.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Glad to help.   If you want to get a little more depth with PHP, here are two good learning resources that you can trust:
http://us2.php.net/tut.php
http://www.sitepoint.com/books/phpmysql4/

Author

Commented:
Thanks again. I appreciate the links.