Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need "else if" Statement For PHP Script

    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?

    I believe these are the attributes in the script which need configuration.

    PHP Code:
    $tomail $_POST["friend_email"];

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

    //send the email
    mail ($tomail$subject$inputMessage,$from); 

    Thanks

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Call all email fields "friends[]":
    Code:
    <input type="text" name="friends[]" value="" />
    It will then be passed as an array to your PHP script:
    PHP Code:
    <?php

    // email pseudo-validation
    function is_email$email )
    {
        if ( 
    /* is a real email */ )
        {
            return 
    $email;
        }
        return 
    '';
    }

    /* ... after collecting $subject, $inputMessage, $from... */

    // grab friends
    $friends = & $_POST['friends'];
    $friends array_map'is_email'$friends );
    $friends array_diff$friends, array( '' ) );

    // send email if we have any valid emails
    if ( !empty( $friends ) )
    {
        
    mailimplode', '$friends ), $subject$inputMessage$from );
    }

    ?>

  • #3
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Thanks for your reply.

    That may, or may not work. Perhaps it would help if I post the entire script below.

    PHP Code:
    <?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 "contact@mywebsite.com";

      
    // 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: contact@mywebsite.com\nContent-type: text/html\n";
          
    $your_email "contact@mywebsite.com";
      }

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

    $notifyMail "contact@mywebsite.com";
    $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>

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by Mr. J View Post
    That may, or may not work.
    That's true... maybe you should try it and let us know.

  • #5
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd like to, but if you notice, the original script doesn't appear to accomodate kbluhm's suggested code.

  • #6
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Is there supposed to be something in here instead of /* is a real email */?

    PHP Code:
    if ( /* is a real email */ 

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Yes, some sort of regular expression to check the e-mail address.

  • #8
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Yes, some sort of regular expression to check the e-mail address.
    Well so you'll know, I just finished testing the suggested code.

    It didn't work - in fact it stopped me from sending any emails.

    The form & script currently works okay - I only want to tweak it in order to enable multiple sends.

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    It will do just what you'd like... but you will need to modify your form's HTML code as I've already expressed.

    Let's see what kind of a programmer you are. It's a fairly straight-forward solution.

  • #10
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi kbluhm,

    Yep I modified the form with the friends[] value as you suggested -- it'd be pointless to try the script without doing that.

    Have you looked at the entire script I posted?

  • #11
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I find it hard to believe that the script you where given broke your current script, it might not work but it on;ly adds functionality and doesn't change any important variables at any point.

    try adding an else{} to the if statement you already have with the new code you where given inside it.

  • #12
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A programmer pal of mine from Indiana resolved this issue in about ten minutes. In case anyone's interested, the below code works perfectly.

    Thanks

    PHP Code:
    <?php

    $tomail 
    $_POST["friend_1"];

    $from $_POST["sender_email"];
    $copyfrom $_POST["sender_email"];
    $notifyMail "contact@mywebsite.com";

    // 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: contact@mywebsite.com\nContent-type: text/html\n";
    $your_email "contact@mywebsite.com";
    }

    $notifyMail "contact@mywebsite.com";
    $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>

  • #13
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    glad it works, the only thing I would have done different is store the friends email addresses in an array and use a while loop to loop the preg match bit over and over, but thats just my style of doing things


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •