Welcome Guest, Not a member yet? Register   Sign In
Send email automatically with Code Igniter
#1

[eluser]jimmy90[/eluser]
Hi,

I'm very newbie with Code Igniter.

I want to create a code for sending email automatically if the car's speed exceeds the speed limit is in the database.

Here is my code,

Code:
<?php
class Send extends Controller{

    function Send(){
        parent::Controller();
        $this->load->model('Send_model', '', TRUE);
    }
    
    function send_email(){
        $getmail = $this->Send_model->get_mail();
        $getname = $this->Send_model->get_name();
        $getspeed = $this->Send_model->get_speed();
        $getspeedlimit = $this->Send_model->get_speedlimit();
        
        if($getspeed > $getspeedlimit){
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
        
            $this->email->from($getmail, $getname);
            $this->email->to('[email protected]');
            
            $this->email->subject('KuLacak');
            $this->email->message('Attention : Speed Alert!!!');
        
            if($this->email->send()){
                echo 'Your email was sent.';
            }
            else{
                $this->email->print_debugger();
            }
        }
    }
}

Code:
<?php
class Send_model extends Model{

    function Send_model(){
        parent::Model();
    }
    
    function get_email(){
        $this->db->select('users.email');
        $this->db->from('users');
        $this->db->where('units.owner = users.id');
        $this->db->where('speed_limit.unit_id = units.id');
        return $this->db->get();
    }
    
    function get_name(){
        $this->db->select('users.username');
        $this->db->from('users');
        $this->db->where('units.owner = users.id');
        $this->db->where('speed_limit.unit_id = units.id');
        return $this->db->get();
    }
    
    function get_speed(){
        $this->db->select('tracks.speed');
        $this->db->from('tracks');
        return $this->db->get();
    }
    
    function get_speedlimit(){
        $this->db->select('speed_limit.batas');
        $this->db->from('speed_limit');
        return $this->db->get();
    }
}

Sorry for my bad english. Thank you.

^^
#2

[eluser]pickupman[/eluser]
Your code is very close to working. I would recommend using the following in your model
Code:
//Change
return $this->db->get();

//To
$query = $this->db->get();
if($query->num_rows() > 0)
  return $query->result();

return FALSE;

Right now, you are only returning the query object, not a field value. The corrected code will return the result object or FALSE if query fails. I can't tell from your query strings if you might be returning multiple rows or not. You will need to iterate through the result object and process accordingly.

Code:
$getmail = $this->Send_model->get_mail();

if($getmail != FALSE)
{
   foreach($getmail as $mail)
   {
     echo $mail->email;
   }
}
#3

[eluser]jimmy90[/eluser]
Hi, thank you for your help.

But for the code that I made, it only works if the user is online or not?

^^
#4

[eluser]pickupman[/eluser]
[quote author="jimmy90" date="1275479945"]Hi, thank you for your help.

But for the code that I made, it only works if the user is online or not?

^^[/quote]

Sorry, what is this supposed to mean?
#5

[eluser]jimmy90[/eluser]
[quote author="pickupman" date="1275502937"]
Sorry, what is this supposed to mean?
[/quote]

I mean the code that I made, can it executes if the user access the page?

^^
#6

[eluser]pickupman[/eluser]
[quote author="jimmy90" date="1275551161"][quote author="pickupman" date="1275502937"]
Sorry, what is this supposed to mean?
[/quote]

I mean the code that I made, can it executes if the user access the page?

^^[/quote]

Yes...just like any other web page. If you are wanting to automatically run the script at a set interval, you will need to setup a crontab.
#7

[eluser]jimmy90[/eluser]
[quote author="pickupman" date="1275552115"]

Yes...just like any other web page. If you are wanting to automatically run the script at a set interval, you will need to setup a crontab.[/quote]

How can I setup my code into a crontab because I don't really know what cron is? and how it work?

^^
#8

[eluser]Simian Studios[/eluser]
[quote author="jimmy90" date="1275556832"][quote author="pickupman" date="1275552115"]

Yes...just like any other web page. If you are wanting to automatically run the script at a set interval, you will need to setup a crontab.[/quote]

How can I setup my code into a crontab because I don't really know what cron is? and how it work?

^^[/quote]

This link should fill you in on the basics of crontab - http://kb.iu.edu/data/afiz.html

You can easily have your cron job execute any script like this:

Code:
* * * * * curl --silent --compressed http://your.site/send/send_email >/dev/null 2>&1

The above would run your send_email method every minute, but you can change the time settings to get whatever frequency you want.
#9

[eluser]jimmy90[/eluser]
[quote author="Simian Studios" date="1275572103"][quote author="jimmy90" date="1275556832"][quote author="pickupman" date="1275552115"]

Yes...just like any other web page. If you are wanting to automatically run the script at a set interval, you will need to setup a crontab.[/quote]

How can I setup my code into a crontab because I don't really know what cron is? and how it work?

^^[/quote]

This link should fill you in on the basics of crontab - http://kb.iu.edu/data/afiz.html

You can easily have your cron job execute any script like this:

Code:
* * * * * curl --silent --compressed http://your.site/send/send_email >/dev/null 2>&1

The above would run your send_email method every minute, but you can change the time settings to get whatever frequency you want.[/quote]

Thank you for your suggestion, it really works.

^^
#10

[eluser]Simian Studios[/eluser]
[quote author="jimmy90" date="1276157673"]

Thank you for your suggestion, it really works.

^^[/quote]

No problems - it can be a bit daunting the first time you need to set up a cron job but it's simpler than it seems!




Theme © iAndrew 2016 - Forum software by © MyBB