Need "else if" Statement For PHP Script

He has: 2 posts

Joined: Nov 2007

I have a tell-a-friend PHP script that I'm already using successfully and I want to tweak it so that visitors can optionally send a notice to multiple email addresses.

I've named and added several new fields to the form. Can anyone tell me how to update the script to accomodate this?

Perhaps it would also help if I post the entire script below.

Thanks

<?php
  $tomail
= $_POST[\"friend_email\"];

  //THE ADDITIONAL EMAIL FIELDS ARE NAMED
  //friend_2, friend_3, friend_4, friend_5

 
$from = $_POST[\"sender_email\"];
 
$copyfrom = $_POST[\"sender_email\"];
 
$notifyMail = \"[email protected]\";

  // Check the email address
 
$replace_str = array (\"&amp;\", \"&gt;\", \"&lt;\", \"&copy;\", \"&nbsp;\",
         \"&quot;\", \"&reg;\", \"&laquo;\", \"&raquo;\",
         \"&iexcl;\", \"&iquest;\", \"&Agrave;\", \"&agrave;\",
         \"&Aacute;\", \"&aacute;\", \"&Acirc;\", \"&acirc;\",
         \"&Atilde;\", \"&atilde;\", \"&Auml;\", \"&auml;\",
         \"&Aring;\", \"&aring;\", \"&AElig;\", \"&aelig;\",
         \"&Ccedil;\", \"&ccedil;\", \"&ETH;\", \"&eth;\", \"&Egrave;\",
         \"&egrave;\", \"&Eacute;\", \"&eacute;\", \"&Ecirc;\",
         \"&ecirc;\", \"&Euml;\", \"&euml;\", \"&Igrave;\", \"&igrave;\",
         \"&Iacute;\", \"&iacute;\", \"&Icirc;\", \"&icirc;\",
         \"&Iuml;\", \"&iuml;\", \"&Ntilde;\", \"&ntilde;\",
         \"&Ograve;\", \"&ograve;\", \"&Oacute;\", \"&oacute;\",
         \"&Ocirc;\", \"&ocirc;\", \"&Otilde;\", \"&otilde;\",
         \"&Ouml;\", \"&ouml;\", \"&Oslash;\", \"&oslash;\",
         \"&Ugrave;\", \"&ugrave;\", \"&Uacute;\", \"&uacute;\",
         \"&Ucirc;\", \"&ucirc;\", \"&Uuml;\", \"&uuml;\", \"&Yacute;\",
         \"&yacute;\", \"&yuml;\", \"&THORN;\", \"&thorn;\", \"&szlig;\",
         \"&sect;\", \"&para;\", \"&micro;\", \"&brvbar;\", \"&plusmn;\",
         \"&middot;\", \"&uml;\", \"&cedil;\", \"&ordf;\", \"&ordm;\",
         \"&not;\", \"&shy;\", \"&macr;\", \"&deg;\", \"&sup1;\",
         \"&sup2;\", \"&sup3;\", \"&frac14;\", \"&frac12;\",
         \"&frac34;\", \"&times;\", \"&cent;\", \"&pound;\",
         \"&curren;\", \"&yen;\", \"http://\", \"ftp://\", \"mail://\",
         \"news://\", \"nntp://\", \"irc://\", \"gopher://\",
         \",\", \";\", \":\", \"!\", \"#\", \"\$\", \"%\", \"\^\", \"&\", \"\*\",
         \"\(\", \"\)\", \"\+\", \"=\", \"\{\", \"\}\", \"\[\", \"]\", \"\|\", \"'\", '\"',
         \"/\", \"\?\", \">\", \"<\", \"~\", \"`\", \" \", \"¡\", \"¢\", \"£\", \"¤\",
         \"¥\", \"¦\", \"§\", \"¨\", \"©\", \"ª\", \"«\", \"¬\", \"*\", \"®\", \"¯\",
         \"°\", \"±\", \"²\", \"³\", \"´\", \"µ\", \"¶\", \"·\", \"¸\", \"¹\", \"º\",
         \"»\", \"¼\", \"½\", \"¾\", \"¿\", \"À\", \"Á\", \"Â\", \"Ã\", \"Ä\", \"Å\",
         \"Æ\", \"Ç\", \"È\", \"É\", \"Ê\", \"Ë\", \"Ì\", \"Í\", \"Î\", \"Ï\", \"Ð\",
         \"Ñ\", \"Ò\", \"Ó\", \"Ô\", \"Õ\", \"Ö\", \"×\", \"Ø\", \"Ù\", \"Ú\", \"Û\",
         \"Ü\", \"Ý\", \"Þ\", \"ß\", \"à\", \"á\", \"â\", \"ã\", \"ä\", \"å\", \"æ\",
         \"ç\", \"è\", \"é\", \"ê\", \"ë\", \"ì\", \"í\", \"î\", \"ï\", \"ð\", \"ñ\",
         \"ò\", \"ó\", \"ô\", \"õ\", \"ö\", \"÷\", \"ø\", \"ù\", \"ú\", \"û\", \"ü\",
         \"ý\", \"þ\", \"ÿ\");

  //check customer email address
  for (
$i = 0; $i < sizeof ($replace_str) - 1; $i++)
   
$tomail = ereg_replace ($replace_str[$i], \"\", $tomail);
 
$pattern = '^([._a-zA-Z0-9-]){2,255}@([._a-zA-Z0-9-]){2,255}\.([a-zA-Z]){2,3}$';

  if (!ereg (
$pattern, $tomail) && !empty($tomail))
  {
     
$errorMessage .= \"Your Friend's Email Address is not valid<BR>\n\";
  }

    if (!empty(
$_POST[\"sender_email\"]))
    {
     
$from = \"From: \" . $your_email . \"\nContent-type: text/html\n\";
    } else {
     
$from = \"From: [email protected]\nContent-type: text/html\n\";
     
$your_email = \"[email protected]\";
  }

  //if a field wasnt filled out calls an error page.
  //if (!empty(
$errorMessage))
  //{
  //  PrintError(
$errorMessage,\"refer\");
  //  exit;
  //}

$notifyMail = \"[email protected]\";
$notifySubject = \"TV Webcasts Was Shared from mywebsite.com\";
$notifyMessage = \"The send video script was just used for:\r\n\r\n\";
$notifyMessage .= \"Live Streaming TV Webcasts\r\n\";
$notifyMessage .= \"http://www.mywebsite.com/live-webcasts/\r\n\r\n\";
$notifyMessage .= \"Recipient Email: \" . $tomail . \"\r\n\r\n\";
$notifyMessage .= \"Sender: \" . $_POST[\"sender_name\"] . \"\r\n\";
$notifyMessage .= \"Sender Email: \" . $_POST[\"sender_email\"] . \"\r\n\";
$notifyMessage .= \"Message: \" . $_POST[\"extra\"] . \"\r\n\r\n\r\n\";
$notifyMessage .= \"* Complete strangers are promoting mywebsite.com.\";

  //copy to sender
$copyfrom = \"From: \" . $_POST[\"sender_email\"] . \"\";
$copyMail = $_POST[\"sender_email\"];
$copySubject = \"You sent TV webcasts from mywebsite.com\";
$copyMessage = \"This is a copy of the email you sent to:\r\n\";
$copyMessage .= $_POST[\"friend_email\"] . \"\r\n\r\n\";
$copyMessage .= \"When you have a moment, check out these online TV webcasts.\r\n\r\n\";
$copyMessage .= \"Live Streaming TV Webcasts\r\n\r\n\";
$copyMessage .= \"http://www.mywebsite.com/live-webcasts/\r\n\r\n\r\n\";
$copyMessage .= $_POST[\"extra\"] . \"\r\n\r\n\r\n\";
$copyMessage .= $_POST[\"sender_name\"] . \"\r\n\r\n\r\n\r\n\";
$copyMessage .= \"________________________________________\r\n\";
$copyMessage .= \"Sent courtesy of: http://www.mywebsite.com/\r\n\r\n\";
//end of copy email

  //main message
$from = \"From: \" . $_POST[\"sender_email\"] . \"\";
$subject = \"Check out these online TV webcasts...\";

//email format - plain jane text
$inputMessage .= \"Hi,\r\n\r\n\";
$inputMessage .= \"When you have a moment, check out these online TV webcasts.\r\n\r\n\";
$inputMessage .= \"Live Streaming TV Webcasts\r\n\r\n\";
$inputMessage .= \"http://www.mywebsite.com/live-webcasts/\r\n\r\n\r\n\";
$inputMessage .= $_POST[\"extra\"] . \"\r\n\r\n\r\n\";
$inputMessage .= $_POST[\"sender_name\"] . \"\r\n\r\n\r\n\r\n\";
$inputMessage .= \"________________________________________\r\n\";
$inputMessage .= \"Sent courtesy of: http://www.mywebsite.com/\r\n\r\n\";
//end of email

  //send the email
  mail (
$tomail, $subject, $inputMessage,$from);
  //mail (
$notifyMail, $notifySubject, $notifyMessage);
  if (
$_POST[\"copyMail\"]) {
      mail (
$copyMail, $copySubject, $copyMessage, $copyfrom);
    }
  ereg_replace(array(\"\n\",\"\r\"),'',
$from)
  //sends them off to the thank you page below

<html>
<head>
<title>thank you page</title>

</head>
<body>
thank you page text
</body>
</html>
?>

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Just quick suggestion.

Name all the TO fields the same (say sender_email[]

Now you will have an array in PHP, , ex. $_POST['sender_email'][0], $_POST['sender_email'][1], $_POST['sender_email'][2]

Break you code up so it does all the checking of the info that will be the same on every e-mail (sender's e-mail, setting up the actual message and headers, etc). Then do a loop, simliar to the following:

foreach ($_POST['sender_email'] as $thisEmail)
{
    // VALIDATE THE E_MAIL ADDRESS HERE, the current e-mail is in $thisEmail

    // SEND THE E_MAIL HERE
}
'
You will probably want to ad checking on each one to make sure each of them actually validate good.

Just a rough solution for you, normally I'd et more detailed, but kinda tired today.

-Greg

calculator's picture

They have: 40 posts

Joined: Nov 2007

Your email checking function is a bit too 'messy'. Why would you try and replace invalid characters? If someone enters one, simply throw an error at them and ask them to modify their it.

I would use something like the following regular expression to test email validity (inspired by the book essential PHP security from Chris Shifflet):

if(preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $thisEmail)){
  //email is ok
}else{
  //email invalid
}
'

He has: 2 posts

Joined: Nov 2007

A buddy of mine from Indiana resolved this issue in about ten minutes. In case anyone's interested, the below code works perfectly.

Thanks

<?php
$tomail
= $_POST[\"friend_1\"];

$from = $_POST[\"sender_email\"];
$copyfrom = $_POST[\"sender_email\"];
$notifyMail = \"[email protected]\";

// Check the email address
if(!preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',
$from)) {
$errormessage = \"Your e-mail address is invalid!\";
}

if(!preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',
$_POST[\"friend_1\"])) {
$errormessage .= \" Your 1st friend's e-mail address is invalid!\";
}
if(!preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',
$_POST[\"friend_2\"]) && $_POST[\"friend_2\"]) {
$errormessage .= \" Your 2nd friend's e-mail address is invalid!\";
}
if(!preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',
$_POST[\"friend_3\"]) && $_POST[\"friend_3\"]) {
$errormessage .= \" Your 3rd friend's e-mail address is invalid!\";
}
if(!preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',
$_POST[\"friend_4\"]) && $_POST[\"friend_4\"]) {
$errormessage .= \" Your 4th friend's e-mail address is invalid!\";
}
if(!preg_match('/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',
$_POST[\"friend_5\"]) && $_POST[\"friend_5\"]) {
$errormessage .= \" Your 5th friend's e-mail address is invalid!\";
}

if (!
$errormessage) {

if (!empty(
$_POST[\"sender_email\"]))
{
$from = \"From: \" . $your_email . \"\nContent-type: text/html\n\";
} else {
$from = \"From: [email protected]\nContent-type: text/html\n\";
$your_email = \"[email protected]\";
}

$notifyMail = \"[email protected]\";
$notifySubject = \"TV webcasts Was Shared from mywebsite.com\";
$notifyMessage = \"The send video script was just used for:\r\n\r\n\";
$notifyMessage .= \"Live Streaming TV Webcasts\r\n\";
$notifyMessage .= \"http://www.mywebsite.com/live-webcasts/\r\n\r\n\";
$notifyMessage .= \"Recipient Email: \" . $tomail . \"\r\n\r\n\";
$notifyMessage .= \"Sender: \" . $_POST[\"sender_name\"] . \"\r\n\";
$notifyMessage .= \"Sender Email: \" . $_POST[\"sender_email\"] . \"\r\n\";
$notifyMessage .= \"Message: \" . $_POST[\"extra\"] . \"\r\n\r\n\r\n\";
$notifyMessage .= \"* Complete strangers are promoting mywebsite.com.\";

//copy to sender
$copyfrom = \"From: \" . $_POST[\"sender_email\"] . \"\";
$copyMail = $_POST[\"sender_email\"];
$copySubject = \"You sent TV webcasts from mywebsite.com\";
$copyMessage = \"*Below is a copy of the email(s) you sent:\r\n\r\n\r\n\";
$copyMessage .= \"When you have a moment, check out these online TV webcasts.\r\n\r\n\";
$copyMessage .= \"Live Streaming TV Webcasts\r\n\r\n\";
$copyMessage .= \"http://www.mywebsite.com/live-webcasts/\r\n\r\n\r\n\";
$copyMessage .= $_POST[\"extra\"] . \"\r\n\r\n\r\n\";
$copyMessage .= $_POST[\"sender_name\"] . \"\r\n\r\n\r\n\r\n\";
$copyMessage .= \"________________________________________\r\n\";
$copyMessage .= \"Sent courtesy of: http://www.mywebsite.com/\r\n\r\n\";
//end of copy email

//main message
$from = \"From: \" . $_POST[\"sender_email\"] . \"\";
$subject = \"Check out these online TV webcasts...\";

//email format - plain jane text
$inputMessage .= \"Hi,\r\n\r\n\";
$inputMessage .= \"When you have a moment, check out these online TV webcasts.\r\n\r\n\";
$inputMessage .= \"Live Streaming TV Webcasts\r\n\r\n\";
$inputMessage .= \"http://www.mywebsite.com/live-webcasts/\r\n\r\n\r\n\";
$inputMessage .= $_POST[\"extra\"] . \"\r\n\r\n\r\n\";
$inputMessage .= $_POST[\"sender_name\"] . \"\r\n\r\n\r\n\r\n\";
$inputMessage .= \"________________________________________\r\n\";
$inputMessage .= \"Sent courtesy of: http://www.mywebsite.com/\r\n\r\n\";
//end of email

//send the email
for (
$x = 1; $x <= 5; $x++)
mail (
$_POST[\"friend_$x\"], $subject, $inputMessage,$from);

//mail (
$notifyMail, $notifySubject, $notifyMessage);
if (
$_POST[\"copyMail\"]) {
mail (
$copyMail, $copySubject, $copyMessage, $copyfrom);
}
}

<html>
<head><title>thank you page</title></head>
<body>
thank you page text
</body>
</html>
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.