Sam Teng Wong
Sam Teng Wong

Reputation: 2439

CI Sending Mail using SMTP

I'm going straight to the point here. I'm trying to send an mail using codeigniter library. However, I'm not receiving any email and there are no errors shown on my code. I don't know why it doesn't work.

I've also followed the 2 step verification and allow access to less secure apps

However when I omit the config, it send me an email which goes directly to the spam section. I don't know why it goes to spam section.

Here's my code:

public function sending_email(){

    // The mail sending protocol.
    $config['protocol'] = 'smtp';
    // SMTP Server Address for Gmail.
    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
    // SMTP Port - the port that you is required
    $config['smtp_port'] = '465';
    // SMTP Username like. ([email protected])
    $config['smtp_user'] = '[email protected]';
    // SMTP Password like (abc***##)
    $config['smtp_pass'] = '****';
    // Load email library and passing configured values to email library
    $this->load->library('email', $config);
    // Sender email address
    $this->email->from('[email protected]', 'sample');
    // Receiver email address.for single email
    $this->email->to('[email protected]');
    //send multiple email
    //$this->email->to([email protected],[email protected],[email protected]);
    // Subject of email
    $this->email->subject('test');
    // Message in email
    $this->email->message('hello world!');
    // It returns boolean TRUE or FALSE based on success or failure
    echo $this->email->send(); 

}

Upvotes: 0

Views: 4321

Answers (1)

Rana Ghosh
Rana Ghosh

Reputation: 4674

You can try this code also if commented link's answer is not working.

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxx',
        'smtp_pass' => 'xxx',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    // Set to, from, message, etc.

    $result = $this->email->send();

Upvotes: 1

Related Questions