CodeIgniter Forums
email in codeigniter works fine in localhost, but not working on live environment - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: email in codeigniter works fine in localhost, but not working on live environment (/showthread.php?tid=64555)



email in codeigniter works fine in localhost, but not working on live environment - kmphpdev - 03-04-2016

Hello,
I am using codeigniter email library to send email. the code runs fine in localhost but it gives several warnings on live environment.

My code -->
*****************************************************************************************************
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test_email extends CI_Controller
{
function __construct()
{
parent::__construct();
}

function index()
{
$receiver = '[email protected]';
$subject = 'HOLAA User Authentication';
$message = 'HELLO USER, You Registered successfully..';

//email code
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',
'smtp_pass' => 'tauyqmdtyplkgeew',
'mailtype'  => 'html', 
'charset'   => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]');
$this->email->to($receiver);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
echo $this->email->print_debugger();
}
}

*****************************************************************************************

It is running fine on localhost, but when I put that code on -->http://uat.holaa.in/index.php/Test_email  It is giving several warnings & email sending fails(Plz. visit this link to know more about error I am getting. I turned email debugger on). Few snapshots of successful email sent message through localhost & error on live platform I have attached with this post. Please look at photos attached with this post.

Thank you in advance  Smile


RE: email in codeigniter works fine in localhost, but not working on live environment - code - 04-23-2019

Hi,

you can use following approach to send email from localhost and live server as well. it works for me perfectly. For more details, you can check this article of Coderanks.com.

https://www.coderanks.com/article/sending-html-email-using-codeigniter-from-localhost-and-live-server

$localhosts = array(
'::1',
'127.0.0.1',
'localhost'
);

$protocol = 'mail';
if (in_array($_SERVER['REMOTE_ADDR'], $localhosts)) {
$protocol = 'smtp';
}

$config = array(
'protocol' => $protocol,
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'Your-Email',
'smtp_pass' => 'Your-Email-Password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n",
);

$this->load->library('email');
$this->email->initialize($config);
$this->email->from("From-Email");
$this->email->to("To-Email");
$this->email->subject("New user contacts");
$this->email->message($final_mail);
$flag = $this->email->send();

if($flag){
echo "Email sent";
}else{
echo "Email sending failed";
}


RE: email in codeigniter works fine in localhost, but not working on live environment - InsiteFX - 04-23-2019

Fix your user and password you should never display on a live server or in the forums!