0

I have the following to send an email

if($this->form_validation->run())
    {
        $data = $this->input->post();
        $this->load->library('email');
        $this->email->from('me@gmail.com', 'Joe Dev');
        $this->email->reply_to('me@gmail.com', 'Joe Dev');
        $this->email->to('someone@mail.com'); 
        $this->email->cc('friend@mail.com');

        $this->email->subject('Email Test');
        $this->email->message('I used this firstname: '. $data['fname']);   

        $this->email->send();

        $config['charset'] = 'iso-8859-1';
        $config['smtp_host'] = 'mail.google.com';
        $config['smtp_user'] = 'me@gmail.com';
        $config['smtp_pass'] = 'mypassword';

        $this->email->initialize($config);
    }

where $this->email->to() is my webmail account, it delivers the message successfully but it has failed for every gmail account I tried sending it to. What may be the problem?

nanobash
  • 5,419
  • 7
  • 38
  • 56
Joseph Rex
  • 1,540
  • 2
  • 18
  • 20
  • Use `echo $this->email->print_debugger();`, secondly, use `smtp.gmail.com` as the `smtp_host`; thirdly, is it that you are setting the email settings **after** you sent the email? Lastly, do check this link: [**Sending gmail smtp Codeiginter SO Question**](http://stackoverflow.com/questions/1555145/sending-email-with-gmail-smtp-with-codeigniter-email-library?rq=1) – MackieeE Mar 21 '14 at 16:54
  • I was placing the settings after the email->send() but I corrected that now. I also changed the SMTP and it's still the same. It works perfectly for my webmail – Joseph Rex Mar 21 '14 at 17:05
  • Are you sure its not sending at all? I have found alot of my emails form unrecognised sources in my spam / junk folder in gmail. – RaggaMuffin-420 Mar 21 '14 at 17:13

1 Answers1

2

You have to initialize the email library before using $this->email->send(). So the corrected code will be:

    if($this->form_validation->run())
        {
            $data = $this->input->post();
            $this->load->library('email');
            $this->email->from('me@gmail.com', 'Joe Dev');
            $this->email->reply_to('me@gmail.com', 'Joe Dev');
            $this->email->to('someone@mail.com'); 
            $this->email->cc('friend@mail.com');
            $this->email->subject('Email Test');
            $this->email->message('I used this firstname: '. $data['fname']); 
            $config['charset'] = 'iso-8859-1';
            $config['smtp_host'] = 'mail.google.com';
            $config['smtp_user'] = 'me@gmail.com';
            $config['smtp_pass'] = 'mypassword';
            $this->email->initialize($config);
            $this->email->send();
        }

Hope this helps!

Hammad
  • 2,097
  • 3
  • 26
  • 45
  • I get even more errors by doing that. My code was working but it was just not working with gmail accounts. Now I get "Undefined Index: Form" from the email library by using yours – Joseph Rex Mar 22 '14 at 11:24