0

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. (abc@gmail.com)
    $config['smtp_user'] = 'sample@gmail.com';
    // 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('sample@gmail.com', 'sample');
    // Receiver email address.for single email
    $this->email->to('receiver@gmail.com');
    //send multiple email
    //$this->email->to(abc@gmail.com,xyz@gmail.com,jkl@gmail.com);
    // 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(); 

}
active92
  • 644
  • 12
  • 24
Sam Teng Wong
  • 2,379
  • 5
  • 34
  • 56
  • I think the smtp server should be `smtp.gmail.com` https://support.google.com/a/answer/176600?hl=en and if you are using 2 step verification you should be using an app specific password https://support.google.com/accounts/answer/185833?hl=en. Let me know if that works. – Morfinismo Dec 08 '16 at 04:13
  • @Morfinismo does not work at all. tried what you suggested.. – Sam Teng Wong Dec 08 '16 at 04:17
  • I recommend looking into the answers provided on this question which seems like a duplicate of yours http://stackoverflow.com/questions/1555145/sending-email-with-gmail-smtp-with-codeigniter-email-library – Morfinismo Dec 08 '16 at 04:21

1 Answers1

1

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();
Rana Ghosh
  • 4,514
  • 5
  • 23
  • 40