Welcome Guest, Not a member yet? Register   Sign In
sending email using code igniter
#1

I am trying to send email using code igniter and I am not having any luck. I am hosting on a go daddy server. Here is my code. Do I need to create another view? Could there be issues associated with the config file?

PHP Code:
<?php

class Email extends CI_Controller
{
    function 
__construct()
    {
        
parent::Controller();  
        
    
}
    
    function 
index()
    {
        
$config = array( //"Array" changed to "array" 1/15/15
        
            
'protocol' => 'smtp',
            
'smtp_host' => 'ssl://smtp.googlemail.com',
            
'smtp_port' => 465//465
            
'smtp_user' => '[email protected]',
            
'smtp_pass' => 'gmailfb38'
            'mailtype' 
=> 'text',
 
            'charset' => 'iso-8859-1',
 
            'wordwrap' => TRUE
        
);
        
        
$this->load->library('email'$config); //$config
        
        
$this->email->set_newline("\r\n");
        
        
$this->email->from('[email protected]','Ben Abrams');
        
        
$this->email->to('[email protected]');
        
        
$this->email->subject('This is an email test.');
        
        
$this->email->message('It is working.');
        
        
// $this->email->send();
        
        //echo $this->email->print_debugger();
        
        
if($this->email->send())
        {
            echo 
'Your email was sent';
        }
        else
        {
            
show_error($this->email->print_debugger());
        }
        
    }

Reply
#2

(This post was last modified: 01-15-2015, 11:48 PM by Avenirer.)

Did you just put your email password inside the code? Smile)
Reply
#3

(This post was last modified: 01-16-2015, 12:01 AM by Avenirer.)

What's this?
Code:
parent::Controller();
Some sort of new constructor?
Reply
#4

Have you loaded the email library? And you should use your constructor like that:

PHP Code:
parent::__construct(); 

Reply
#5

(01-15-2015, 08:35 PM)Ben Wrote: I am trying to send email using code igniter and I am not having any luck. I am hosting on a go daddy server. Here is my code. Do I need to create another view? Could there be issues associated with the config file?


PHP Code:
<?php

class Email extends CI_Controller
{
 function 
__construct()
 {
 
parent::Controller();  
 
 
}
 
 function 
index()
 {
 
$config = array( //"Array" changed to "array" 1/15/15
 
 
'protocol' => 'smtp',
 
'smtp_host' => 'ssl://smtp.googlemail.com',
 
'smtp_port' => 465//465
 
'smtp_user' => '[email protected]',
 
'smtp_pass' => 'gmailfb38'
 'mailtype' 
=> 'text',
 
    'charset' => 'iso-8859-1',
 
    'wordwrap' => TRUE
 
);
 
 
$this->load->library('email'$config); //$config
 
 
$this->email->set_newline("\r\n");
 
 
$this->email->from('[email protected]','Ben Abrams');
 
 
$this->email->to('[email protected]');
 
 
$this->email->subject('This is an email test.');
 
 
$this->email->message('It is working.');
 
 
// $this->email->send();
 
 //echo $this->email->print_debugger();
 
 
if($this->email->send())
 {
 echo 
'Your email was sent';
 }
 else
 {
 
show_error($this->email->print_debugger());
 }
 
 }


Okay, first of all, you need to initialise the email class with the new config, not when loading the library: $this->email->initialize($config);

And also you have // $this->email->send(); which sends the email commented out; remove the // and it should work.
Reply
#6

(This post was last modified: 01-16-2015, 09:20 AM by Rufnex. Edit Reason: Format code )

I updated the code with everyone's suggestions. Nothing seems any different. Do I need to create a view to "see" the result of the call to this controller? This is the URL to where this is hosted:
http://www.designtimedesignworks.com/ci/index.php/email .

PHP Code:
<?php

class Email extends CI_Controller
{
    function 
__construct()
    {
        
parent::__construct(); // was parent::Controller(); 
        
    
}
    
    function 
index()
    {
        
$config = array( //"Array" changed to "array" 1/15/15
        
            
'protocol' => 'smtp',
            
'smtp_host' => 'ssl://smtp.googlemail.com',
            
'smtp_port' => 465//465
            
'smtp_user' => '[email protected]',
            
'smtp_pass' => 'pass'
            'mailtype' 
=> 'text',
             
'charset' => 'iso-8859-1',
             
'wordwrap' => TRUE
        
);
        
        
$this->email->initialize($config); // added on 1/16
        
        
$this->load->library($config); //$config, 'email' removed
        
        
$this->email->set_newline("\r\n");
        
        
$this->email->from('[email protected]','Ben Abrams');
        
        
$this->email->to('[email protected]');
        
        
$this->email->subject('This is an email test.');
        
        
$this->email->message('It is working.');
        
        
$this->email->send(); // uncommented 1/16/15
        
        //echo $this->email->print_debugger();
        
        
if($this->email->send())
        {
            echo 
'Your email was sent';
        }
        else
        {
            
show_error($this->email->print_debugger());
        }
        
    }

Reply
#7

Try this

PHP Code:
<?php

class Email extends CI_Controller
{
    function 
__construct()
    {
        
parent::__construct();
        
    }
    
    function 
index()
    {
        
$this->load->library('email');
    
        
$config = array(
            
'protocol' => 'smtp',
            
'smtp_host' => 'ssl://smtp.googlemail.com',
            
'smtp_port' => 465//465
            
'smtp_user' => '[email protected]',
            
'smtp_pass' => 'pass'
            'mailtype' 
=> 'text',
            
'charset' => 'iso-8859-1',
            
'wordwrap' => TRUE
        
);
        
        
$this->email->initialize($config);

        
$this->email->set_newline("\r\n");
        
$this->email->from('[email protected]','Ben Abrams');
        
$this->email->to('[email protected]');
        
$this->email->subject('This is an email test.');
        
$this->email->message('It is working.');
        
$this->email->send();
        
//echo $this->email->print_debugger();
        
        
if($this->email->send())
        {
            echo 
'Your email was sent';
        }
        else
        {
            
show_error($this->email->print_debugger());
        }
        
    }


Reply
#8

I tried your update. I'm not getting different results with what I have hosted on go daddy, but I got some interesting results when I tried to access the file in my mamp. I don't think I can send email from my mamp, which is why I was testing this on a web server. Should I look at any other files on the server?

This was the output from my mamp (if that has any relevance, I don't think my mamp is really capable of sending email):

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ci/application/config/config.php:1)

Filename: core/Common.php

Line Number: 440

An Error Was Encountered

220 mx.google.com ESMTP cl9sm1841955igb.0 - gsmtp

hello: 250-mx.google.com at your service, [71.57.41.94]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
Failed to authenticate password. Error: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/bin/answ...swer=78754 cl9sm1841955igb.0 - gsmtp
from: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 cl9sm1841955igb.0 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 cl9sm1841955igb.0 - gsmtp
to: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 cl9sm1841955igb.0 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 cl9sm1841955igb.0 - gsmtp
data: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 cl9sm1841955igb.0 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 cl9sm1841955igb.0 - gsmtp
502 5.5.1 Unrecognized command. cl9sm1841955igb.0 - gsmtp
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. cl9sm1841955igb.0 - gsmtp
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
220 mx.google.com ESMTP fr4sm1835034igd.4 - gsmtp
hello: 250-mx.google.com at your service, [71.57.41.94]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
Failed to authenticate password. Error: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/bin/answ...swer=78754 fr4sm1835034igd.4 - gsmtp
from: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 fr4sm1835034igd.4 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 fr4sm1835034igd.4 - gsmtp
to: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 fr4sm1835034igd.4 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 fr4sm1835034igd.4 - gsmtp
data: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 fr4sm1835034igd.4 - gsmtp
The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answe...swer=14257 fr4sm1835034igd.4 - gsmtp
502 5.5.1 Unrecognized command. fr4sm1835034igd.4 - gsmtp
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. fr4sm1835034igd.4 - gsmtp
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Fri, 16 Jan 2015 19:47:53 +0100
From: "Ben Abrams" <[email protected]>
Return-Path: <[email protected]>
To: [email protected]
Subject: =?iso-8859-1?Q?This_is_an_email_test.?=
Reply-To: "[email protected]" <[email protected]>
X-Sender: [email protected]
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0


Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

It is working.
Reply
#9

@Ben

If you are out of options, try this:

http://forum.codeigniter.com/thread-28.html

Very carefully read README.md, install, compare and re-check configuration and try.
Reply
#10

The error tells you that you have the wrong credentials for your smtp login. check this.

Reply




Theme © iAndrew 2016 - Forum software by © MyBB