CodeIgniter Forums
how do you do a newsletter (batches) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: how do you do a newsletter (batches) (/thread-20492.html)



how do you do a newsletter (batches) - El Forum - 07-11-2009

[eluser]tomdelonge[/eluser]
I don't want to use something like mailchimp. I want to make something myself. Say you have 100 subscriptions. Can you think of a way maybe to have each page visit do 5 or something? Until the list is empty? I don't know if it would be slow or not to check the database every page view to see if any subscriptions are pending.

Any ideas?


how do you do a newsletter (batches) - El Forum - 07-11-2009

[eluser]richthegeek[/eluser]
A simple SQL (MySQL 5+, due to subquery) statement splits the list into groups of 5:
Code:
SELECT GROUP_CONCAT(email,',') AS recipients FROM emails GROUP BY MOD(id,(SELECT ROUND(COUNT(*) / 6) FROM emails));

You can add a WHERE statement between the FROM and GROUP, or a HAVING afterwards (remembering to add the appropriate column in the fieldlist) to limit by unsent.

Hope that helps.


how do you do a newsletter (batches) - El Forum - 07-11-2009

[eluser]tomdelonge[/eluser]
Do you guys think this is a smart way to accomplish this?
@richthegeek that looks good, thanks.


how do you do a newsletter (batches) - El Forum - 10-05-2009

[eluser]sophistry[/eluser]
i suggest using

$this->db->limit(5);

and (if you have enough of them visiting your site) tell the robots to do your work:

Code:
$this->load->library('user_agent');
if ($this->agent->is_robot()) run_subscriptions();



how do you do a newsletter (batches) - El Forum - 10-05-2009

[eluser]renownedmedia[/eluser]
You're all crazy, use cron!


how do you do a newsletter (batches) - El Forum - 10-05-2009

[eluser]n0xie[/eluser]
indeed use cron:

Code:
function sendqueue()
{
$where = "blnSend='0' AND datPlanned <= '".date('Y-m-d')."'";
$this->db->where($where,'',FALSE);
$this->db->limit(5);
$list = $this->db->get('tblemail')->result();

        foreach ($list as $email)
        {
            $this->email->clear();

            $this->email->to($email->to);
            $this->email->from($email->from);
            $this->email->subject($email->subject);
            $this->email->message($email->message);
            
            if($this->email->send())
            {
                $this->db->set('blnSend',1);
                $this->db->set('datSend',date('Y-m-d'));
                $this->db->where('ID_Email', $email->ID_Email);
                $this->db->update('tblemail');
            }
            else
            {
                $this->db->set('blnSend',0);
                $this->db->set('blnError',1);
                $this->db->set('msgError',$this->email->print_debugger());
                $this->db->where('ID_Email', $email->ID_Email);
                $this->db->update('tblemail');
            }
        }
    }
}



how do you do a newsletter (batches) - El Forum - 10-05-2009

[eluser]sophistry[/eluser]
[quote author="Thomas Hunter" date="1254771604"]You're all crazy, use cron![/quote]

ha ha ha! that's the best compliment i can get from someone. all crazy!

cron it is. or maybe robots... really i prefer enlisting robots - it makes me feel superior to them.

.insert big sig here.