0

I am trying to send mail to all users that have no created reports in one month, also trying to send mail to this users with foreach loop and with mail function, when i refresh one by one then it sends mail one by one, then it works. i want to send mail to all this users in one time.

        $from = "xxx@xxx.com";
        $subject = "";
        $headers = "";
        $inc = 0;
        foreach($query->result_array() as $row)
        {
            $inc++;
            $to = $row['us_email'];
            $to_date = $row['report_date'];
            if($to_date != "0000-00-00")
            {
                $subject = "Hello! (Reports Are Not Created).";

                //begin of HTML message 
                $message = "1 month above you should create reports.";
            }
            else if($to_date == "0000-00-00")
            {
                $subject = "Hello! (Generate Reports).";

                //begin of HTML message 
                $message ="generate reports to get more.";
            }
            $headers  = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
            $headers .= "To: User <".$to.">, Admin <".$from.">" . "\r\n";
            $headers .= "From: Complete Online Marketing <".$from.">" . "\r\n";
            $headers .= "Reply-To: Recipient Name <".$from.">";
            $headers .= "Cc: [email]".$from."[/email]" . "\r\n";
            $headers .= "Bcc: [email]".$from."[/email]" . "\r\n";


            // now lets send the email.
            if(mail($to, $subject, $message, $headers))
            {
                echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Success...\n";
                $ins = array('report_mail'=>'0');
                $this->db->update('se_user', $ins, array('us_id' => $row['us_id']));
            }
            else
            {
                echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Fail...\n";
            }
        }
Rahul K
  • 413
  • 3
  • 11
  • When you send mail in bulk like this it probably gets flagged as spam – John Conde Sep 16 '14 at 13:52
  • 1
    Sending mail can be a pain. Since you're using codeigniter why not use the MailManager class ? https://ellislab.com/codeigniter/user-guide/libraries/email.html – MaxouMask Sep 16 '14 at 13:53
  • its a cron job @JohnConde i want to send mails in one time calling this php file. and yes this also going to spam why ? – Rahul K Sep 16 '14 at 14:04
  • some headers problem so i am trying to use it directly beacase i am sending two at a time, mail to user and also to the admin. @Maskime – Rahul K Sep 16 '14 at 14:06
  • Bulk emails, especially those sent from PHP scripts like this one, look very spammy. – John Conde Sep 16 '14 at 14:06
  • thanks @john Conde can help to improve this php scripts? – Rahul K Sep 16 '14 at 14:13
  • 1
    I'd suggest to have a look in the `sendmail` logs to see what is happening then your SMTP might send an error message that you don't display => http://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function a look at this should tell you how to get the message – MaxouMask Sep 16 '14 at 14:26

0 Answers0