How to send email to all users?

Discussion in 'PHP' started by moxet, Feb 28, 2011.

  1. #1
    Hey all
    any one suggest me how can i send an email from <textarea> as content to the table users.:(

    User table having.
    * Username
    * Full Name
    * Email
     
    moxet, Feb 28, 2011 IP
  2. webcodez

    webcodez Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like this?

    
    $get_users = mysql_query("SELECT username, full_name, email FROM users"); //get all users
    $first = 1;
    while($user = mysql_fetch_assoc($get_users)) { //loop through all of them
      if(!$first) {
        $users .= ", ".$user['username'];
      }else{
        $users = $user['username'];
        $first = 0;
      }  
    }
    $subject = "Some subject";
    $message = "Some message"; //<-- e.g. $_POST['textarea_name']
    $mail = mail($to, $subject, $message);
    
    PHP:
     
    Last edited: Feb 28, 2011
    webcodez, Feb 28, 2011 IP
  3. moxet

    moxet Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ^ Thank You Very Much webcodez
     
    moxet, Mar 2, 2011 IP
  4. webcodez

    webcodez Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're very welcome, I'm glad I could help :)

    Let me know if you can get it to work or if you have any other questions!

    Cheers,
    Webcodez.
     
    webcodez, Mar 2, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i believe you would need the mail function within the while loop in order to sesend the message to each user
     
    srisen2, Mar 2, 2011 IP
  6. happyhosting

    happyhosting Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, the mail function should be inside the loop. Also, consider sending around 10 emails at a time. Process the remaining later.
     
    happyhosting, Mar 2, 2011 IP
  7. cheesee

    cheesee Peon

    Messages:
    621
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Why only send 10 at a time? Can sending too many emails at once cause some sort of an error?
     
    cheesee, Mar 2, 2011 IP
  8. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Sending more than 500 emails / per hour / per domain is not considered a good practice and thanks there is a high possibility of server ip getting blacklisted as a spam server :)
     
    eleetgeek, Mar 5, 2011 IP
  9. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Moxet

    Keep a wait timer for each loop. This will take up bit of CPU but, its necessary.
     
    eleetgeek, Mar 5, 2011 IP
  10. ferostive

    ferostive Active Member

    Messages:
    162
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    1- Page which contain your form (<form><textarea> ....)
    .... <body> .... <form action="sendmessage.php" method="get" name="msgform" id="msgform">
      <p>Subject:
        <input type="text" name="msg_textfield" id="msg_textfield" />
      </p>
      <p>
        Message
        <textarea name="msg_textarea" id="msg_textarea" cols="45" rows="5"></textarea>
      </p>
      <p>
        <input type="submit" name="snd_msg" id="snd_msg" value="Send" />
      </p>
    </form>....
    </body>
    PHP:
    2-Page which contain php script to send email (php page ex:sendmessage.php)
    $get_users = mysql_query("SELECT username, full_name, email FROM users")or die(mysql_error());
    $first = 1;
    while($user = mysql_fetch_assoc($get_users)) {	
    $to = $user['email'];    
    $subject = $_POST['msg_textfield'];
    $message = $_POST['msg_textarea'];; 
    $mail = mail($to, $subject, $message);}
    PHP:
     
    ferostive, Mar 5, 2011 IP
  11. moxet

    moxet Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks Dear...
    Try the new one..
     
    moxet, Mar 8, 2011 IP
  12. moxet

    moxet Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Great Working 100% ferostive..
    Thank You Very Much..
     
    moxet, Mar 8, 2011 IP
  13. ferostive

    ferostive Active Member

    Messages:
    162
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #13
    never mind
     
    ferostive, Mar 8, 2011 IP
  14. benims

    benims Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    it will be useful for me too, thanks :)
     
    benims, Mar 9, 2011 IP