mass email script

Discussion in 'PHP' started by nomirock, Jul 31, 2007.

  1. #1
    i need code for making a mass mailing script with which i can send more than 1 email to an email address.
     
    nomirock, Jul 31, 2007 IP
  2. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #2
    uniqueasitis, Jul 31, 2007 IP
  3. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Where are the emails stored?

    If they are in mysql you can loop the results and send the mails out like that.

    Someting like.

    HTML Form
    
    <h4>Mass mail form</h4>
    <form action="" name="mass_email" method="post">
    <p>Subject of Mail: <input type="text" name="subject"></p>
    <p>Message to Send: <input type="text" name="message"></p>
    <p><input type="submit" name="send" value="Send the mass mail"></p>
    </form>
    
    HTML:
    
    <?php
    if($_POST['send'])
    {
    /***
    Connections Information here
    ***/
    
    $result = mysql_query("SELECT * FROM email");
    // gets all the emails from the db
    $total = @mysql_num_rows($result);
    // total emails in db
    if($total==0)
    echo "No emails where found";
    }else{
    $i=0;
    // loops the emails
    while($i<$total)
    {
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $to = mysql_result($result, $i, 'email');
    mail($to, $subject, $message) or die ("Mail failed");
    echo "Sent Mail to $to<br />";
    $i++;
    		}
    	}
    echo "Sent $i emails in total";
    }
    ?>
    
    PHP:
    I just coded that in the quick reply havent tested it but it should work fine, this is just to show you how to use mysql and php to make a simple mass mailer, you would need to make a database have the fields I made etc to get it to work.
     
    HuggyCT2, Jul 31, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    ^ The problem with that is, that your server's DNS is going to get blocked quick, if you have a lot of users or send a lot of emails. You should use a queue. Send a few mails... wait a while,... send a few more, and so on...
     
    nico_swd, Jul 31, 2007 IP
  5. HuggyCT2

    HuggyCT2 Guest

    Messages:
    222
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It was to show how you would go about doing a mass mail, I think setting stages and making it more complicated is not wise for a novice.
     
    HuggyCT2, Jul 31, 2007 IP
  6. nomirock

    nomirock Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanx for your help people i have understood it now
     
    nomirock, Jul 31, 2007 IP
  7. postcd

    postcd Well-Known Member

    Messages:
    1,043
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #7
    How to do it??please
     
    postcd, Dec 16, 2007 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    easiest way is to use the sleep(); function, if you sleep too much you might die :p

    
    sleep(1800); // delay the script for 30 minutes
    
    PHP:
    The harder and more efficient way is to use the sendmail or postfix server to queue the emails for you. Another way is to split up the database table into parts using
    
    SELECT * FROM emails LIMIT 0,100
    SELECT * FROM emails LIMIT 100,100
    SELECT * FROM emails LIMIT 200,100 ... and so on...
    
    Code (markup):
     
    Kaizoku, Dec 16, 2007 IP