2
votes

Im try to send email using gmail with codeIgniter. here is my code :

function sendemail($name,$key, $email) {

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

    $econfig['protocol'] = "smtp";
    $econfig['smtp_host'] = "ssl://smtp.googlemail.com";
    $econfig['smtp_port'] = 465;
    $econfig['smtp_user'] = "*******@gmail.com"; 
    $econfig['smtp_pass'] = "******";
    $econfig['charset'] = "utf-8";
    $econfig['mailtype'] = "html";

    $this -> email -> initialize($econfig);
    $this->email->set_newline("\r\n");

    $this -> email -> from('*********@gmail.com', 'Asoft Administration');
    $this -> email -> to($email);

    $this -> email -> subject('Subject');
    $this -> email -> message("HTML MESSAGE");

    if (!$this -> email -> send()) {
        echo $this -> email -> print_debugger();
        return false;
    } else {            
        return true;
    }
}

My Environment is ubuntu server. Im never use postfix or sendmail. Is it required to send gmail mails ?. i double checked user name and password.!

After try send email im get this error.

hello: 250-mx.google.com at your service, [2607:5300:100:200::377] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 250-ENHANCEDSTATUSCODES 250 CHUNKING

Failed to authenticate password. Error: 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14

from: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1

The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 - gsmtp to: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1

The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 data: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1

The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 502 5.5.1 Unrecognized command. j4sm48430630qaf.31 - gsmtp The following SMTP error was encountered: 502 5.5.1 Unrecognized command. j4sm48430630qaf.31 - 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, 3 Jul 2014 11:20:14 +0200

From: "Asoft Administration" <**@gmail.com>

Return-Path: <**@gmail.com>

To: **@gmail.com

Subject: =?utf-8?Q?Qsi_Member_confirmation_mail?=

Reply-To: "**@gmail.com" <**.com>

X-Sender: **@gmail.com

X-Mailer: CodeIgniter

X-Priority: 3 (Normal)

Message-ID: <[email protected]>

Mime-Version: 1.0

Anyone can help me ... Thanks lot.

2
Try loading the email library below the config settings, like so: $this->load->library('email', $econfig); (not using initialise) - Mudshark
I tried your instruction and then it was give this error.. "Unable to send email using PHP mail(). Your server might not be configured to send mail using this method." - chamaraherath
It's not two factor authentication or anything like that is it? - Sam
Have you logged in on the corresponding gmail account? Sometimes it will complain about "suspicious" activity when you first try using its SMTP. If you then discard it as "non suspicious", gmail will allow future mails being sent through its service. - Mudshark
Yeah Mudshark its the problem. thanks a lot. I logged in server corresponding mail after no problem at all work fine. - chamaraherath

2 Answers

0
votes

I had same problem, here is my working code:

$config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => '[email protected]',
    'smtp_pass' => 'xxx',
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);

$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]', 'Niraj');
$this->email->to($email);
$this->email->subject('SOME SUBJECT');
$this->email->message('<p>Some Content</p>');
$this->email->send();
0
votes

Google want to authorize when it trying to send first time. Google send message to corresponding email whether you want to enable use this email via unsecured app.