1
votes

I am trying to send an email through email function in email_model, but email is not being sent

class email_model extends CI_Model {
public function __construct() {
    parent::__construct();
}    
public function email($name) {
        $this->db->select('*');
        $this->db->from('dna_crm_emails');
        $this->db->where('email_name',$name);
        $query = $this->db->get();
        return $query->result();
    }
}

Any reference or help will be much appreciated.

Regards

1
your model only fetching data from your database.Is there any reason it will send email? - Shaiful Islam

1 Answers

0
votes

The email function in email_model just retrieve data upon given email_name what do you need is to send mail through mail class as Mudshark mentioned

you need to create a function at your controller , it'll be like this :

function sendMail($sender_email, $sender_name, $target_email, $subject, $message_data) {
    $this -> load -> library('email');
    // whatever email you want it to be as a sender 
    //just make sure it end with your domain name to not get blocked
    // ex : '[email protected]','Support'
    $this -> email -> from($email, $sender); 
    $this -> email -> to($target_email);//the email you're targeting
    $this -> email -> subject($subject); // message subject
    $message = "This is message data : ". $message_data;
    $this -> email -> message($message); // message body as plain text
    $this -> email -> send();
}

you could use more advanced options though like sending mail as html , by creating a view message.php with designed html for your email message and parsing an array of data to it.

your function would be like this :

function sendMail($sender_email, $sender_name, $target_email, $subject, $message_data) {
    $this -> load -> library('email');
    $config['mailtype'] = 'html';
    $this -> email -> initialize($config); // to define email as html type
    $this -> email -> from($email, $sender); 
    $this -> email -> to($target_email);//the email you're targeting
    $this -> email -> subject($subject); // message subject
    $this->email->message( $this->load->view( 'message', $message_data, true )); //html message
    $this -> email -> send();
}