Wai Yan Hein
Wai Yan Hein

Reputation: 14791

Why sending email is not working in CodeIgniter?

I am sending an email using CodeIgniter Version 2.2.1. But it is giving me error. Here is my PHP code:

       $config = array(
            'protocol'=>'smtp',
            'smtp_host'=>'ssl://smtp.googlemail.com',
            'smtp_port'=>465,
            'smtp_user'=>'[email protected]',
            'smtp_pass'=>'xxx'
        );

        $this->load->library('email',$config);

        $this->email->set_newline("\r\n");
        $this->email->from('[email protected]', "My Name");
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();
        return $this->email->print_debugger();

But when I send, it is giving me this error. I tried many ways. Why is this happening?

   220 smtp.gmail.com ESMTP ci2sm13227458pbc.66 - gsmtp

hello: 250-smtp.gmail.com at your service, [61.4.76.240]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8

Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

from: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

to: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

data: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp

The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. ci2sm13227458pbc.66 - gsmtp
502 5.5.1 Unrecognized command. ci2sm13227458pbc.66 - gsmtp
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. ci2sm13227458pbc.66 - gsmtp
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

User-Agent: CodeIgniter
Date: Thu, 15 Oct 2015 08:45:28 +0200
From: "War" <[email protected]>
Return-Path: <[email protected]>
To: [email protected]
Subject: =?utf-8?Q?subject?=
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=utf-8
Content-Transfer-Encoding: 8bit

Every find bro ? I am here for u .

I am using localhost by xampp. Do I need to configure something in my localhost?

Upvotes: 1

Views: 4356

Answers (5)

Wai Yan Hein
Wai Yan Hein

Reputation: 14791

Thank you all for helping me. Finally I got the answer . The problem was from google. It is blocking the less secure apps . So I just navigated to this url https://www.google.com/settings/security/lesssecureapps after sign in to google and turn on the access for less secure apps . You can find out more details at there .

Upvotes: 0

Suraj
Suraj

Reputation: 2277

Do check you code with following snippets

$config = array( 
    'protocol' => 'smtp', 
    'smtp_host' => 'mail.google.com', 
    //'smtp_port' => '465', 
    'smtp_timeout' => '7', 
    'smtp_user' => '[email protected]', 
    'smtp_pass' => 'password', 
    'charset' => 'utf-8', 
    'newline' => "rn", 
    'mailtype' => 'html', // or html 
    'wordwrap' => FALSE, 
    'validation' => TRUE // bool whether to validate email or not 
); 

$this->load->library('email'); 
$this->email->initialize($config); 
$this->email->set_newline("rn"); 
$this->email->from($email, 'Subject'); 
$this->email->to('[email protected],[email protected],[email protected]'); 
$this->email->subject('Contact us form by: ' . $name); 
$this->email->message($message . '<br>website:' . $website); 
$this->email->send(); 

//echo $this->email->print_debugger(); `enter code here`

Please check your code with the above snippet. Debug your code for mail using //echo $this->email->print_debugger();. If still problem persist then post here.

Upvotes: 0

B.Nadesh kumar
B.Nadesh kumar

Reputation: 226

Try this:

public function some_name() {

    $email = $this->input->post('email');

    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '*****@gmail.com',
        'smtp_pass' => '*****',
        'mailtype'  => 'html', 
        'charset' => 'utf-8',
        'wordwrap' => TRUE
    );            

    /* 
    *
    * Send email with #temp_pass as a link
    *
    */

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('****@gmail.com', "****");
    $this->email->to($email);
    $this->email->subject("***********");

    $message = "<p>Your Account ************</p>";
    $this->email->message($message);
    $result = $this->email->send();

}

Upvotes: 1

Ricky
Ricky

Reputation: 568

Try this

 public function sendEmail(){
    //now copy and paste the below code
    $this->load->library('email');  
    $html = "Test email";
     $email_setting = array('mailtype'=>'html');
     $this->email->initialize($email_setting);
     $this->email->from('Test');
     $this->email->to('the email address');
     $this->email->subject('Title': 'New email');
     $this->email->message($html);
     $this->email->send();
}

Let me know if this work. you can modify your code later on. For more how to send email in codeigniter try this link http://w3code.in/2015/09/how-to-send-email-using-codeigniter/

Upvotes: 0

kross
kross

Reputation: 475

This is a fully working email function that I'm currently using.

public function send_email($to, $subject, $message)
{
    // Initilize email configuration
    $config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'intsmtp.abc.com.sg',
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('ABCMaster');
    $this->email->to($to);
    $this->email->subject($subject);
    $this->email->message($message);

    return ($this->email->send()); // returns TRUE or FALSE
}

Here's how I used it:

$to = "[email protected]";
$subject = "Learning ABCs";
$message = "ABCs are very simple. I am using them now.<br>";
$message .= "If you would like to learn more, please contact me.";

// Send email
if ( $this->send_email( $to, $subject, $message ) )
{
    // Sending email success
}
else
{
    // Sending Email has failed
}

Upvotes: 0

Related Questions