1

I am using codeigniter mail function. Its sends mail perfectly. But all the time mail going to the spam folder. How can I overcome this.

Function

function msg_now(){
        $this->load->library('email');
        $this->load->library('parser');

        $config['protocol'] = 'sendmail';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $this->email->initialize($config);
        $email_id='test@test.com';
        $name=$this->'test';
        $this->email->from('test@gmail.com');
        $this->email->to($email_id);

        $this->email->subject('Test subject');

        $this->email->message("<p>Lorem ipsum dummy content</p>");
        $this->email->send();
}
JAL
  • 41,701
  • 23
  • 172
  • 300
user5060801
  • 67
  • 1
  • 2
  • 13

2 Answers2

2
    $this->load->library('parser');

    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'google email id',
        'smtp_pass' => 'password',
        'mailtype'  => 'html',
        'charset'   => 'iso-8859-1'
    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('google email id','Title'); // change it to yours
    $this->email->to(your send email id);// change it to yours
    $this->email->subject('you subject');
    $this->email->message('your message');
    if($this->email->send())
    {
        return true;
    }
    else
    {
        show_error($this->email->print_debugger());
    }
1

There are many reasons for mail to go in spam, but easy solution for it is to set the headers of the mail before sending and giving it priority.

Here's how to do that in CodeIgniter. The function is set_header():

$this->email->set_header($header, $value);

also check this link for reference.

It has always worked for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
Marmik Bhatt
  • 607
  • 4
  • 16