Jignesh Patel
Jignesh Patel

Reputation: 316

How To make Bulk email Script using php

I am making a bulk email script for 1k user and I want to send email but my script gives me error, timeout.

How to handle page timeout. any suggestion for backend run this script.

If user clicks send button then run this script backend if user close this page but script working.

    $Email_ID="";
    $queryUser=mysql_query("SELECT  `email` From `users`",$conn);
    while($User=mysql_fetch_array($queryUser))
    {
        $to = $User["email"];
        $Email_ID=$User["email"];
        $from = '[email protected]';
        $from_name = 'Test bulk email';
        $senderMail="[email protected]";
        $senderName="abc";
        $message=$_POST["message"];
        $subject = $_POST["subject"]; 
        $html_content = $_POST["message"];
        $from = $senderName." <".$senderMail.">"; 
        $headers = "From: $from";
        $headers = "CC: [email protected]";
        $headers = "BCC: [email protected]";

        $semi_rand = md5(time()); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

        $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

        $upload_file="";
        if(isset($_SESSION["uniqid"]))
        {
            $query=mysql_query("SELECT  * From `bulk_email_attchment` where `unique_id`='".$_SESSION["uniqid"]."' ",$conn);
            while($row=mysql_fetch_array($query))
            {
                        $files = "Attchment/".$row["name"];
                        $message .= "--{$mime_boundary}\n";
                        $fp =    @fopen($files,"rb");
                        $data =  @fread($fp,filesize($files));
                        @fclose($fp);
                        $data = chunk_split(base64_encode($data));
                        $message .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"\n" . 
                        "Content-Description: ".basename($files)."\n" .
                        "Content-Disposition: attachment;\n" . " filename=\"".basename($files)."\"; size=".filesize($files).";\n" . 
                        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                }
        }


        $message .= "--{$mime_boundary}--";
        $returnpath = "-f" . $senderMail;

        //send email
        $mail = mail($to, $subject, $message, $headers, $returnpath); 

    }
}

Upvotes: 0

Views: 428

Answers (3)

Peter
Peter

Reputation: 9143

Option 1

If you want to continue a script if the user aborts you can use ignore_user_abort().

// Ignore if a user aborts
ignore_user_abort(true);
set_time_limit(0);

// Send your emails here

For more information check the PHP ignore_user_abort() (documentation)[http://php.net/manual/fr/function.ignore-user-abort.php]

Option 2

Insert all submited form data into a database. You can then make a cron job that checks if there are new email jobs to submit and submit them. This way the user won't have to wait and you will not have the problem of them closing the browser.

Upvotes: 0

Webeng
Webeng

Reputation: 7143

The Right Way: Create a php Queue, with a Named Pipe, that connects to another php script that would send the emails each time it is invoked. If you are just staring php, it'll take a while before all this is coded properly.

The Moderate but not ideal way: Set up a Cron Job to invoke your script (that would send 20 or so emails each time invoked. If your using a service like Godaddy, just google: "CronJob godaddy setup"

The Hacky Way: Have the script in a page that redirects to itself with a query string updating it on how many emails were sent:

After 20 emails for instance are sent, use the code:

$count = 20;
header('Location http://www.example.com/index.php?count='.$count);

I don't recommend using The Hacky Way because as a programmer, we should try to follow best practices, but I personally can't hold you back :)

Upvotes: 1

Amit
Amit

Reputation: 71

You can set a cron script. So your email script handled by cron and run in background.

Upvotes: 1

Related Questions