PHP check a email.txt file or MySQL if exists before sending a message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxtran2005
    New Member
    • Nov 2006
    • 3

    PHP check a email.txt file or MySQL if exists before sending a message

    Hi guys,

    I've downloaded couple email list scripts at hotscripts but none of them do what I wanted. Can you help me?

    Basically, I have an emaillist.txt (code from this site http://www.phptutorial .info/scripts/mailinglist/) which allow user to subscribe and unsubscribe.

    If user selects subscribe, enter an email and that email is already in the emaillist.txt, a message is sent to that email saying thank you you've already subscribed.

    If user selects subscribe, and email is not in the emailist.txt, a message is sent saying something like thank you for your interest, your email will be added to our emaillist as soon as possible. (*I will add the email into the emaillist.txt manually)

    If user selects the unsubscribe, the message will say... sorry you have to leave etc.

    The reason I do this is because I don't want to send out newsletter to those who aren't interested, or that they're not in group or friends.

    Any suggestion would be greatly appreciated.
  • TheMadMidget
    New Member
    • Oct 2006
    • 98

    #2
    [PHP]
    $str = strtoupper(file _get_contents(" emaillist.txt") );
    if (strpos($str, strtoupper($Inp utedEmail)))
    {//Already in file
    }
    else
    {//New to file
    }
    [/PHP]
    $InputedEmail will have to be declared yourself
    strtoupper() lets makes all the characters uppercase to prevent captialization differences.

    Comment

    • TheMadMidget
      New Member
      • Oct 2006
      • 98

      #3
      I just looked at that URL in your post and depending on how much of that you used, you may be able to take out the strtoupper() s and might need one strtolower(). It's up to you, but the code above will work regardless, though it might have redundancies.

      Comment

      • maxtran2005
        New Member
        • Nov 2006
        • 3

        #4
        So... is this correct? I don't have php to test it... please help.

        Code:
        <?php
        
        $str = strtoupper(file_get_contents("emaillist.txt"));
        if (strpos($str, strtoupper($email)))
            {
        
          	if ($_SERVER['REQUEST_METHOD'] == "POST") {
        
            		$realname = strip_tags($realname);
            		$email = strip_tags($email);
            		$feedback = "You already subscribed."
        
            		$subject = "Send Mail Feedback";
            		$message = "$realname, $email\n\n$feedback";
        
            	mail($email, $subject, $message);
        
            }
        }
        
        else
            {
        	//New to file
          	if ($_SERVER['REQUEST_METHOD'] == "POST") {
        
            		$realname = strip_tags($realname);
            		$email = strip_tags($email);
            		$feedback = "You haven't subscribed yet.  We will add you into our mailing list."
        
            		$subject = "Sorry";
            		$message = "$realname, $email\n\n$feedback";
        
            	mail($email, $subject, $message);
        
            } 
        
        
        ?>
        
        <html>
        <head>
          <title></title>
        </head>
        <body>
        
        
        <?php 
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            
            $realname = $_POST['realname']; 
            $email = $_POST['email']; 
            $feedback = $_POST['feedback']; 
            
            echo("<p><b>The following message was sent to $email\n");
            echo("$feedback");
        
            }
            else {
        ?>
        
        
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">
            Name: <input type="text" name="realname" size="25"></td></tr>
            Email: <input type="text" name="email" size="25"></td></tr>
            Feedback: <textarea name="feedback" rows="4" cols="40" wrap="physical"></textarea>
          <input type="submit" value="Send Feedback">
          </table>
          </form>
        
        
        <?php } ?>
        
        </body>
        </html>

        Comment

        • maxtran2005
          New Member
          • Nov 2006
          • 3

          #5
          Hey, I tested the script, however it doesn't work correctly.

          It went to the else statement. Any idea why?

          the emaillist.txt file contains

          myemail1@yahoo. com,
          myemail2@yahoo. com,
          myemail3@abc.co m

          I'm not sure if it matter with breakline, or a comma added after each one

          Thanks

          Comment

          Working...