Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 using mail function to send emails

View previous topic View next topic Go down 
AuthorMessage
Death Note
Member
Member
Death Note

Posts : 3
Join date : 2011-04-18

using mail function to send emails Empty
PostSubject: using mail function to send emails   using mail function to send emails EmptyFri Jun 17, 2011 2:09 am

i have this simple script to send emails to my subscribers. i've broken the sending into 20 emails per batch to get around getting a timeout error in my browser. without bias from using PEAR in sending multiple emails, any comments to improve the code will be appreciated. thank you.

Code:

<?php
include("user_functions.php");
include("connection.php");    // connects to MySQL database
$allsent = false;            // flag to signal that message has been sent to all subscribers
$subject = "email subject";
$message = "Content of the Email message.";
$headers = "From: name@domain.com";

// send email 20 at a time
$query = "SELECT * FROM email_database WHERE sent != 1 LIMIT 20";
$result = mysql_query($query, $conn);
if (mysql_num_rows($result) > 0) {
    while ($rows = mysql_fetch_assoc($result)) {
        mail ($rows['email'], $subject, $message, $headers); 
        $query2 = "UPDATE email_database SET sent = 1";
        $result2 = mysql_query($query2, $conn);
    }
} else {
    $allsent = true;
}
include("close_connection.php");    // close the connection
for ($i=0; $i<=1000000; $i++) {
//delay counter
}
if !$allsent {
    redirect_to( self );    // user defined function (header function)
}
?>
Back to top Go down
Skilletrockz
Super Moderator
Super Moderator
Skilletrockz

Posts : 45
Join date : 2011-05-11

using mail function to send emails Empty
PostSubject: Re: using mail function to send emails   using mail function to send emails EmptyFri Jun 17, 2011 2:12 am

It seems like you might be repeating this code indefinitely, sending 20 emails at a time, until you have sent an email to every email address in the database.

If that is the case, then you are executing multiple database queries, when you really only need to execute one (that returns all emails - no LIMIT clause). This is less taxing on the database.

After performing your single db query, you can loop through the result set and implement sleep() commands periodically to temporarily stall execution (to accommodate email send limits).
Back to top Go down
FamilyGuy
Member
Member
FamilyGuy

Posts : 1
Join date : 2011-05-10

using mail function to send emails Empty
PostSubject: Re: using mail function to send emails   using mail function to send emails EmptyFri Jun 17, 2011 2:20 am

What sim_pack is saying is that he doesn't want the script to time out. Therefore he is sending 20 emails at a time and then redirecting the browser back to its own page to start the process of sending again.

@sim_pack: Look at using the functions ignore_user_abort(true) and set_time_limit(0).

The former will ignore the browser connection being close (the stop button, the window being closed etc) will mean the script continues to run instead of stopping as normal.

The latter will mean that the script can continue to run for as long as it needs.

With those two functions you can call your script from the browser and then hit the stop button. It will continue to run and you won't need to keep refreshing/redirecting the page. Just be sure to include some code in the loop that will look for a stop indicator (a file, a marker in a DB etc) so that the script will exit when instructed to.
__________________
Please wrap your code in [php] tags. It is a sticky topic at the top of the main forum and yes it applies to you too ;)
TIP: Coding styles and $end errors

Code:

//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
Back to top Go down
Death Note
Member
Member
Death Note

Posts : 3
Join date : 2011-04-18

using mail function to send emails Empty
PostSubject: Re: using mail function to send emails   using mail function to send emails EmptyFri Jun 17, 2011 2:21 am

thanks familyguy, you pretty much nailed what i wanted to do. i will try to incorporate your suggestions to improve the code.

thank you also to everyone who contributed their ideas. i appreciate it very much.

off topic, I love family guy. :)
Back to top Go down
Sponsored content




using mail function to send emails Empty
PostSubject: Re: using mail function to send emails   using mail function to send emails Empty

Back to top Go down
 

using mail function to send emails

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» sending mail by jsp servlet

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: using mail function to send emails Edit-trash Useless :: Trash-