Codeigniter Mail function sends mail to spam folde

2019-08-13 00:27发布

问题:

This question already has an answer here:

  • How do you make sure email you send programmatically is not automatically marked as spam? 21 answers

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();
}

回答1:

    $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());
    }


回答2:

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.