Search code examples
phpcodeigniterwiki

Read Email or Not | in CodeIgniter


I want to know if the person read the mail or not.. It works well if person click on a link inside the mail. But I want to make it work without a link, so I have to add a picture <img>. But in this case does not work.

$message = '<p> confirm </p> <img src="'. base_url().'Email/Track_Open_Email/'.$toEmail.'" width="80" height="80" />';
//Email{Controller}/Track_Open_Email{Method}/toEmail{First Param}

Track_Open_Email function in Email Controller

public function Track_Open_Email ($email)
{  
  $this->Email_model->Set_Track_Open_Email($email , $data);
}// from here will go to Email model

Solution

  • Passing a parameter as an URI element in CI doesn't automatically convert it into the variable the function expects.

    I suggest this:

    public function Track_Open_Email ($email)
    {  
       // this will take https://domain.tld/controller/track_open_email/email_address and 
       assign email_address to $email:
       $email = $this->uri->segment(3);
    
       $this->Email_model->Set_Track_Open_Email($email , $data);
    }
    

    Be advised that this exact code requires the url helper to be loaded beforehand and has a small caveat: since you are only checking the email address, if you have multiple different emails sent to the same recipient, reading one will mark all of them as read.

    My suggestion would be to pass another parameter (for instance <img src="'. base_url().'Email/Track_Open_Email/'.$toEmail.'/.$email_id.'" width="80" height="80" /> ) on each different mailing so that by parsing $this->uri->segment(3) and $this->uri->segment(4) and assign each to a separate value you can mark only that specific mail as read:

    public function Track_Open_Email ($email)
    {  
       // this will take https://domain.tld/controller/track_open_email/email_address/email_id and 
       assign email_address to $email:
    
       $email = $this->uri->segment(3);
       $email_id = $this->uri->segment(4);
    
       // now pass both params to the Set_Track_Open_Email model:
       $params = array('email'=>$email, 'email_id'=>$email_id);
       $this->Email_model->Set_Track_Open_Email($params , $data);
    }
    

    Finally, in the model run an update query using both params (you'll access them as $params['email'] and $params['email_id'] and mark only the specific email was read by that specific user.

    For clarity: $email_id should have a value that uniquely identifies the "campaign" for lack of a better word. So, if today you send an email to all your 10,000 users informing them of something, those 10,000 emails sent would have the same email_id (it could be anything: a nonce, a hash with the sending date, or whatever as long as no other email has the same). If you send another email to your users tomorrow, that email would have a different email_id. What do you accomplish with that? If I read your first mail, I only get marked as having read the first mail, not both. A second benefit is that you could track open rates for each mail separately.