Code Igniter email config to config.php - codeigniter

I've put my email configurations in the config.php but I don't know how I can access them in my controller.
In my config.php:
/*EMAIL CONFIG*/
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'pass';
$config['smtp_port'] = '465';
$config['mailtype'] = 'html';
Should I make an instance of the super global object?
Thanks in advance.

You don't put email config settings in config.php. You make a new file called email.php and place it in the config folder. CI will automatically detect the email config setting from that file.
Look under "Setting Email Preferences in a Config File"
http://ellislab.com/codeigniter/user-guide/libraries/email.html

You can access it with:
$this->config->item('protocol');
Also, see the relevant chapter in CodeIgniter User Guide

Related

How can I transform a PHPMailer connection to Laravel .env?

I have a situation where an app is sending its mails with PHPMailer and next configuration:
$mail->isSMTP();
$mail->Host = 'myhost.dev';
$mail->SMTPAuth = false;
$mail->setFrom($from, $from_name);
$mail->addAddress($to, $to_name);
if ($replyTo != '') $mail->addReplyTo($replyTo, $replyToName);
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $body;
As you can see, SMTP server doesn't require authentication and all is working ok. That's all data I have about the mail server. Now, I need to make the same thing but instead of using PHPMailer I want to implement the sending method of Laravel, but I don't have any authentication credential for setting my .env file.
How can I pass the same configuration to my Laravel development? I've tried many combinations but nothing happens. This was the last one:
MAIL_DRIVER=smtp
MAIL_HOST=myhost.dev
MAIL_PORT=25
MAIL_USERNAME=no-reply#host.com
MAIL_PASSWORD=''
MAIL_ENCRYPTION=''
PHPMailer has no idea about Laravel, so you need to pass through the information yourself. Laravel provides the env() global function to retrieve data from the environment, and you would use it like this:
$mail->Host = env('MAIL_HOST');
if (!empty(env('MAIL_USERNAME')) {
$mail->SMTPAuth = true;
$mail->Username = env('MAIL_USERNAME');
$mail->Password = env('MAIL_PASSWORD');
}
and so on, for any other configuration variables you need. I'm not sure exactly what Laravel package defines the env function, but you will need to make sure it's loaded and within scope before calling it.

Ion Auth (Codeigniter) - How to use different database

Is it possible to use different database for authenticating user through Ion Auth?
I would like to create a database dedicated only for user authentication. So, it should be separated from transactional database.
How to do that?
Thank you.
In short...yes it is possible for you to use a different database for your authentication. You would need to create a second database configuration in your application/config/database.php file (similar to the below) in addition to your default config.
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "db_name";
$db['default']['dbdriver'] = "mysql";
$db['authentication']['hostname'] = "localhost";
$db['authentication']['username'] = "root";
$db['authentication']['password'] = "";
$db['authentication']['database'] = "auth_db_name";
$db['authentication']['dbdriver'] = "mysql";
For further reference see - http://ellislab.com/codeigniter/user-guide/database/configuration.html
You would then have to modify your ion_auth model to use this second db configuration, which you would set when loading the database.
$auth_db = $this->load->database('authentication', TRUE);
Then change all database queries in the model replacing $this->db with $auth_db.
So, $this->db->select('password, salt') would become $auth_db->select('password, salt').
For further reference see - http://ellislab.com/codeigniter/user-guide/database/connecting.html
Hope that helps!
The simplest way would be to expand on what MY_Mark said.
Create the new DB config
$db['authentication']['hostname'] = "localhost";
$db['authentication']['username'] = "root";
$db['authentication']['password'] = "";
$db['authentication']['database'] = "auth_db_name";
$db['authentication']['dbdriver'] = "mysql";
Then in the constructor of the ion_auth_model.php do this:
$this->db = $this->load->database('authentication', TRUE);
And it should just work after that.

why is codeigniter email not functional for abc#company.com.np

I am using codeigniter email. Everything is correct except that mail isn't sent to my client email which is something like abc#companyname.com.np but goes to gmail and my office mail.I have bulk email ids to send to. Have I done something wrong or is it the problem of client's company mail settings ??
$this->load->library('email');
$config['mailtype'] = 'html';
$config['validate'] = TRUE;
$this->email->initialize($config);
$this->email->from('abc#gmail.com', 'ABCTechnologies');
$list = array($form->admin_email);
$this->email->to($list);
$this->email->subject($form->form_title);
$message = 'You have got a new Feedback. Here are the Details : <br/>';
'.$email_fields['message'].'<br/>';
$this->email->message($message);
$this->email->send();
where $list is array of mail id.Any suggesstions are welcome.
I changed the smtp also but to no avail.
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.sipradi.com.np';
and smtp port is by default 25.
Help

SMTP Codeigniter email error '354 End data with .'

I'm having trouble getting the Codeigniter email library working on a new server.
This code has previously worked before but has recently stopped working and I cannot for the life of me figure out why. Essentially, here is the code:
$this->email->from('example#example.com', 'Intranet');
$this->email->to($c['email']);//This is def a valid email
//$name is being obtained from elsewhere
$this->email->subject('Time manager reminder');
$this->email->message("
{$name[0]},
<br/><br/>You haven’t completed your time for today. Please don’t forget to do so<br/><br/>
Intranet
");
$this->email->send();
And the email.php config password
$config['mailtype'] = "html";
$config['priority'] = 1;
$config['protocol'] = "smtp";
$config['smtp_host'] = "my server..";
$config['smtp_user'] = "u/n";
$config['smtp_pass'] = "p/w";
$config['smtp_timeout'] = 1;
$config['validate'] = true;
The error I receive from $this->email->print_debugger(); is as follows:
data: 354 End data with .
The following SMTP error was encountered:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
You can view the full error on pastebin here http://pastebin.com/y9UeaEGY
I've crossed out the emails in all places, but I can assure you they are valid and used email addresses.
I'd appreciate any help you can offer. Thanks
I figured out the reason. Apparently, in the config, you need to set
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
manually in the email.php file. Codeigniter doesn't seem to do it by default. Odd :)

CodeIgniter and autoloading the database library

I've got an issue connecting to the database with CodeIgniter.
In the model, if I do
$this->load->database();
then a query such as
$query = $this->db->get('articles', 10);
works and returns data. However, when I remove the load->database line and try autoloading the database library, using
$autoload['libraries'] = array('database');
in application/config/autoload.php, the above query fails. As it works when I explicitly load the library, it's not a problem with my database connection. I'm using CodeIgniter 2.0.2 (the current latest release).
As most of my pages use the database I'd like to autoload it to avoid having to load it anually in each model. Have I misunderstood the documentation regarding loading the database library or am I doing something wrong?
I know I am probably just answering a dead question but i had the same problem.
if you are using eclipse pdt or similar IDE and modified the system>core>model.php by adding some variables just before the constructor to get code completion then the autoload doesnt work
i dont know why but the fact is it doesnt work. I restored the core>model.php to original and autoload works fine. I dont get autoload now and its really a bad experience for new coder to CI.
If you have a workaround please comment so.
This is just an alternative if you still can't resolve the issue. Just extend the CI_Model and auto load the database library in the constructor. Then you can just make all other models inherit from that base class.
Example:
<?php
class My_Model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
}
And the all others will start like :
<?php
class Some_Model extends MY_Model{}
This method has the other benefit that you don't have to include the loading code line in every model you create. Also you can easily push shared functionalities among models into the parent MY_Model class.
Hope it helps!
This is my database.php from my application/config/ directory
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'mydbhost';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydatabase';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Check yours looks like this and rerun through the user guide at http://codeigniter.com/user_guide/database/configuration.html and reread the comments in the autoload.php file in the config directory.
The relevant line in mine reads:
$autoload['libraries'] = array('database', 'form_validation', 'session');
You should be loading the db library like this:
$this->load->library('database');
The only thing I can think of is in your application/config/database.php file, the following line is set to false, instead of true.
//['autoinit'] Whether or not to automatically initialize the database.
$db['default']['autoinit'] = TRUE;
Everything else looks ok.
Shababhsiduque,
Just you we're right, at least in my case!
The workaround to getting the ide to work is creating the reference variables in ANOTHER CI project.
That is leave the system model as it is in the current project but modify it in another project.
Then change the php buildpath (project->properties->phpbuildpath) to the project with the variables.
Note this are settings for Aptana Studio 3.
Worked for me.
You could try PHP Autoload Class Magic function in this case.
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'libraries/'. $class . '.php' );
}
}
This code must copied inside the config.php (after the last line) within Application folder.
This code will automatically load libraries for you when its needed.
It might work. Happy coding.
In my case the problem was because there was another occurrence of $autoload['libraries'] inside the same file and it reset the array.

Resources