CodeIgniter Forums
Send e-mail stored from database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Send e-mail stored from database (/showthread.php?tid=40292)



Send e-mail stored from database - El Forum - 04-05-2011

[eluser]mvn1990[/eluser]
Hi, i need to send e-mail to e-mailadresses stored in a database, i don't know how i have to do this with codeigniter? the table of the database looks like this

Tbl_emails

id
username
email
fk_id_user


Send e-mail stored from database - El Forum - 04-05-2011

[eluser]Frank Rocco[/eluser]
Code:
$query = $this->db->get('Tbl_emails');
$this->load->library('email');
foreach ($query->result() as $row)
{
$this->email->from('youremail@com', 'Your Name');
$this->email->to($row->email);
$this->email->subject('your subject');
$this->email->message('your body');
$this->email->send();
sleep(1);
}



Send e-mail stored from database - El Forum - 04-05-2011

[eluser]InsiteFX[/eluser]
Code:
$query = $this->db->get('Tbl_emails');
$this->load->library('email');
foreach ($query->result() as $row)
{
    $this->email->from('youremail@com', 'Your Name');
    $this->email->to($row->email);
    $this->email->subject('your subject');
    $this->email->message('your body');
    $this->email->send();
    if ( ! $this->email->send())
    {
        // Generate error or handle it!
    }
    else
    {
        $this->email->clear(TRUE);
    }
}

You also need to setup the email conf read the CodeIgniter User Guide Email Class.

InsiteFX


Send e-mail stored from database - El Forum - 04-05-2011

[eluser]Frank Rocco[/eluser]
Thanks for adding the error check.

Don't you have to take out the
Code:
$this->email->send();
to prevent sending twice?


Send e-mail stored from database - El Forum - 04-05-2011

[eluser]InsiteFX[/eluser]
Not if your your doing multiple email sends, which is the way you have it coded.

But be careful some hosting providers will nail you if you send to many in any given time limit.

InsiteFX


Send e-mail stored from database - El Forum - 04-05-2011

[eluser]Frank Rocco[/eluser]
Ok, thanks for the info.