I cannot send any emails using Codeigniter - php

I have created a controller and made a test function in the controller to test if the email is sent or not.
I checked with different email addresses, but didn't succeed. This is my code example:
public function sendmail() {
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$user_id = 1;
$name = 'Mark Alan';
$this->email->set_newline("\r\n");
$this->email->from('info#domainname.com', "Test Name");
$this->email->to('test#domainname.com');
$this->email->subject('Test Mail');
$this->email->message('This is a test message');
$this->email->send();
echo $this->email->print_debugger();
}

With Codeigniter (assuming you have auto-loaded the email library) you can either set an email preference in a config file, named email.php and these preferences are loaded automatically from there. It could look like:
// Setting Email Preferences
$config['useragent'] = 'CodeIgniter'; // Mail engine switcher: 'CodeIgniter' or could be 'PHPMailer'
$config['protocol'] = 'smtp'; // 'mail', 'sendmail', or 'smtp'
$config['mailpath'] = '/usr/sbin/sendmail';
or like in your example, you can set them manually, but need to initialize it, so don't forget this line after defining your $config[] array:
$this->email->initialize($config);

Try this code:
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com', //Your Host
'smtp_port' => 465, // Add 25 port if you sending from your smtp mail server
'smtp_user' => 'xxx#gmail.com', // change it to yours, server email
'smtp_pass' => 'xxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = '';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#gmail.com'); // change it to yours
$this->email->to('xxx#gmail.com');// change it to yours
$this->email->subject('Resume from JobsBuddy for your Job posting');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
Note: Please add your smtp port and your smtp email account detail.

Related

SMTP mailer is not working in codeignitor 3

Failed to authenticate password. Error: 535 Incorrect authentication
data Unable to send email uenter code heresing PHP SMTP. Your
server might not be configured to send mail using this method.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'mail.******.com',
'smtp_port' => 587,
'smtp_user' => 'tracker#******.com',
'smtp_pass' => '7Wj{S******KU{',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = 'Hiii';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// $this->load->library('email');
$this->email->from('tracker#******.com'); // change it to yours
$this->email->to($usremail);// change it to yours
$this->email->cc('');
// $this->email->bcc('gobinath#******.com');
$this->email->subject('hi');
$this->email->message($message);
$mailsucc = $this->email->send();
if ($mailsucc) {
echo "suc";
exit();
}else{
$de = $this->email->print_debugger();
print_r($de);
exit();
}
I've been looking for a while, and finally I get it. I hope this works for you too.
Define your timezone:
php timezone
date_default_timezone_set('America/Bogota');
Set up your email account to allow IMAP access, and allow email to be used in less secure applications
less secure apps
IMAP Access
Set up your mail server with codeigniter:
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'xxxxxxxxxx#gmail.com';
$config['smtp_pass'] = 'xxxxxxxxxxx';
$config['smtp_port'] = 465; (465/587)
$config['charset'] = 'utf-8';
$config['crlf'] = "\r\n";
$config['smtp_crypto'] = 'ssl'; (SSL/TSL)
$this->email->initialize($config);
$this->email->set_newline("\r\n");
Prepare your mail to send:
$this->email->from('xxxxxxxxxx#gmail.com', 'ANY-NAME');
$this->email->to('yyyy#yyy.yyy');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
if($this->email->send()){
echo "sent";
}
else{
echo $this->email->print_debugger();
}

How to send email from localhost using CodeIgniter with Gmail

Hello everyone, I'm currently working on a CodeIgniter application in which user will be able to create their own accounts,etc.
I'm trying to create a function to recover password in case is lost or forgotten.
Here is my code:
public function admin_recover(){
$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[7]|valid_email');
if($this->form_validation->run() == FALSE){
//Load View Into Template
$this->template->load('admin', 'login', 'users/recover');
} else {
//Get Post Data
$email = $this->input->post('email');
$password = $this->input->post('password');
$user_email = $this->User_model->get_list();
if($user_email){
$user_email_data = array(
'email' => true
);
///////// Sending email
// Configure email library
$config['wordwrap'] = FALSE;
$config['mailtype'] = 'html';
$config['crlf'] = '\r\n';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['priority']=1;
$config['protocol']='smtp';
$config['smtp_host']='ssl://smtp.gmail.com';
$config['smtp_port']='25';
$config['smtp_timeout']='30';
$config['smtp_user']='kuaf1998#gmail.com';
$config['smtp_pass']='mypassword';
$config['newline']="\r\n";
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('no-reply#socialgeek.com', 'Social Geek');
$this->email->to('kebin1421#hotmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
if($this->email->send())
$this->session->set_flashdata("success","Email sent successfully.");
else
$this->session->set_flashdata("error","Error in sending Email.");
echo $this->email->print_debugger();
//////////////
}
}
}
I'm using the gmail smtp service to send emails from my localhost but I'm still unable to make it happen. Any idea how can I fix this?
I already did some changes on(I'm using xampp) the ini file which is found on my xampp>php>php.ini
When I go to the view, I get this error:
Thanks for helping.
Try to use these configuration settings below and let me know if it works for you.
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'user#gmail.com',
'smtp_pass' => 'yourpassword',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);

Unable to send email using SMTP gmail config in codeigniter 3

Below are my code and I refer all examples in stack overflow and codeigniter user guide. still I unable to solve this
public function send()
{
$config['protocol'] = 'smtp';
$config['smtp_crypto'] = 'ssl';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = '**********#gmail.com';
$config['smtp_pass'] = '********';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['smtp_timeout'] = 30;
$this->email->initialize($config);
$this->email->from('uvizag#gmail.com', 'admin');
$this->email->to('siddharthaesunuri#gmail.com, siddhu.php#gmail.com');
$this->email->subject('Registration Verification:');
$message = "Thanks for signing up! Your account has been created...!";
$this->email->message($message);
if ( ! $this->email->send()) {
show_error($this->email->print_debugger());
}
}
How Can I solve this error ? could please favor to me
Are you try this?
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$result = $this->email->send();
And make sure you had setting your email account with
Allowing less secure apps to access your account
Quỳnh Nguyễn's answer worked for me too. However I think it's worth pointing out that my code fails unless I put the line
$this->email->set_newline("\r\n");
Without that line, gmail complaints about wrong user/password :|
Regards
I had difficulties making this native CI feature to work. I had to implement an external library: PHPMailer. Check this thread.

Sending smtp mail without smtp server installed locally

Should I be able to send SMTP email on my local development PC with CodeIgniter without having a SMTP server application installed on my local development PC?
For example, the following doesn't work.
application/config/email.php
$config['mailtype'] = 'html';
$config['protocol'] = 'smtp';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'my_gmail_account#gmail.com';
$config['smtp_pass'] = '*********';
controller code
$this->load->library('email');
$this->email->from('my_gmail_account#gmail.com', 'Noel');
$this->email->to('my_gmail_account#gmail.com');
$this->email->subject('SMTP email Test');
$this->email->message('Testing smtp email in codeigniter.');
$this->email->send();
$data['message'] = "Sorry Unable to send email...";
if($this->email->send()){
$data['message'] = "Mail sent...";
}
$this->load->view('test_view', $data);
The following returns
Sorry Unable to send email...
and doesn't arrive at the destination email.
Try loading parameters into an array and then initializing the library using array source: CodeIgniter Forum:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$result = $this->email->send();
Also, maybe you need to enable SSL in your php.ini. If you see the line:
;extension=php_openssl.dll
uncomment it by removing semicolon from the beginning of the line and then restart PHP.

how to send email in codeigniter on localhost

hello all i have to send email on localhost
and following is my config file
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/sbin/sendmail -t -i";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xyz.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'xyz.com'; //change this
$config['smtp_pass'] = '######'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
i create code follwing code for email send
if(count($result) > 0 || $result != NULL){
$password=$result['password'];
$this->load->library('email');
$from='chirag#site.com';
$to=$email;
$subject='forget password';
$this->email->from($from);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($password);
if($this->email->send()){
$this->session->set_flashdata('email','We sent your password to your Email...!');
redirect('login');
}
else{
echo $this->email->print_debugger();
}
}
but i got following error when i send email
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.
so what is problem there?
please help me to solve
First make sure you already done the proper localhost configuration for sending emails.
Then in your controller you load the email library by setting the appropriate credentials and port number. Like so:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'your_gmail_name#gmail.com',
'smtp_pass' => 'your_pass',
//'mailtype' => 'html',
// 'charset' => 'iso-8859-1',
// 'wordwrap' => TRUE
);
$this->load->library('email', $config);
i got answer
there is missing a ssl:// in config
but after writing following
$config['smtp_host'] = 'ssl://xyz.com';
mail is send successfully...

Categories