Question

I wrote simple PHP (codeigniter) script to send newsletter with Amazon SES. I have ~8000 emails in MySQL table and script which pulls all rows (email addresses), splits it in smaller packages (because of SES limits) and send email via SMTP. I'm using sleep()* and a cronjob which runs every minute. I know that isn’t the best solution but as a concept it works fine.

To allow sending mails with bcc, I have to make sure there is at last one recipient in to field or Amazon SES will not send it. So, my address (news@baulink.rs) is always in to and the other addresses are in bcc. Every iteration puts 8 different addresses in bcc (BTW, is there any better solution for this?). I also have a simple log file which writes every address operated by my application.

As I see, some recipients don’t get the newsletter. Amazon SES returns bounced emails and that works fine, but some addresses doesn't get it at all and there is no any feedback from Amazon SES. These addresses are in my log, which means they are processed by PHP script. Then I just delete that address and insert again in MySQL table and - it works! Some e-mails are parsed from CSV file, some of were inserted manually. I really don't know and can't check with all recipients if they got the newsletter. Do you have any idea what could cause this strange behavior?

I can't figure out what is the problem.

I'm using CodeIgniter and PHPMailer.

 public function cronSendMail(){
    $newsletter = $this->baumodel->getNewsletter();

    if(is_array($newsletter)){
        echo "No job for me!";    
        return;
    }elseif(is_object($newsletter)){ //means there is a newsletter ready for sending                        
        $limit = 104;
        $newsOfset = $newsletter->news_slanje_ofset;                        
        $noviOfset = $newsOfset+$limit;

        $adrese = $this->baumodel->getAdresar($limit, $newsOfset); 
        $brojAdresa = count($this->baumodel->getSveAdrese()); //number of recipients in whole MySQL table

        if($brojAdresa < $newsOfset){ 
            echo "Adrese: ".$brojAdresa;
            echo " Ofset: " .$newsOfset;
            die('Job is finished! ');
        }

        $this->baumodel->setNewsletterOfset($newsletter->news_id, $noviOfset);                      
        $emailArray = array();

        foreach($adrese as $adr){
            array_push($emailArray, trim($adr['adr_email']));
        }

        $newsletterContent = $newsletter->news_sadrzaj;                                                                                     
        $this->load->library('email');
        $this->email->clear();                

        $maliNiz = array();
        for($i=0; $i<13; $i++){
            //13 times with 2 seconds sleep is ~30-35 seconds of execution time.
            //CronJob runs every minute
            $j=0;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;                           
            $maliNiz[$j] = array_pop($emailArray);
            $j++;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;
            $maliNiz[$j] = array_pop($emailArray);
            $j++;                           

            $this->email->to('news@baulink.rs');
            $this->email->bcc($maliNiz); //This is my BCC array of 8 addresses
            $this->email->from('office@baulink.rs', 'Baulink Portal');
            $this->email->subject('Gradjevinski portal Baulink - Novo na portalu');
            $this->email->message($newsletterContent);
            $this->email->send();
            //echo $this->email->print_debugger();

            sleep(2);               

            //log file start
            $filepath = APPPATH . 'logs/mail-log-' . date('Y-m-d') . '.php';
            $handle = fopen($filepath, "a+");                                                       
            $currentDateTime = date('d.M.Y H:i:s');
            foreach($maliNiz as $emailAdresa){
                    $infoLog = $emailAdresa . " // ".$currentDateTime . " \n";
                    fwrite($handle, $infoLog);
            }
            fclose($handle);
            //log file end
        }
    }
}
Was it helpful?

Solution

As per amazon docs, this could be the issue.

Important When you send an email to multiple recipients (recipients are "To", "CC", and "BCC" addresses) and the call to Amazon SES fails, the entire email is rejected and none of the recipients will receive the intended email. We therefore recommend that you send an email to one recipient at a time.

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-email.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top