Your server might not be configured to send mail using this method - php

I want to verify email when users register. I am using codeigniter3.
When I run on the localhost I do not receive any mail. I get this message " Unable to send email using PHP mail(). Your server might not be configured to send mail using this method. "
Here is my code : controllers/User.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('users_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('session');
//get all users
$this->data['users'] = $this->users_model->getAllUsers();
}
public function index(){
$this->load->view('register', $this->data);
}
public function register(){
$this->form_validation->set_rules('email', 'Email', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[30]');
$this->form_validation->set_rules('password_confirm', 'Confirm Password', 'required|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('register', $this->data);
}
else{
//get user inputs
$email = $this->input->post('email');
$password = $this->input->post('password');
//generate simple random code
$set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = substr(str_shuffle($set), 0, 12);
//insert user to users table and get id
$user['email'] = $email;
$user['password'] = $password;
$user['code'] = $code;
$user['active'] = false;
$id = $this->users_model->insert($user);
//set up email
$config = array(
'protocol' => 'mail',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_user' => '********#gmail.com', // change it to yours
'smtp_pass' => '*********', // change it to yours
'mailtype' => 'html',
'wordwrap' => TRUE
);
$message = "
<html>
<head>
<title>Verification Code</title>
</head>
<body>
<h2>Thank you for Registering.</h2>
<p>Your Account:</p>
<p>Email: ".$email."</p>
<p>Password: ".$password."</p>
<p>Please click the link below to activate your account.</p>
<h4><a href='".base_url()."user/activate/".$id."/".$code."'>Activate My Account</a></h4>
</body>
</html>
";
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($config['smtp_user']);
$this->email->to($email);
$this->email->subject('Signup Verification Email');
$this->email->message($message);
//sending email
if($this->email->send()){
$this->session->set_flashdata('message','Activation code sent to email');
}
else{
$this->session->set_flashdata('message', $this->email->print_debugger());
}
redirect('register');
}
}
public function activate(){
$id = $this->uri->segment(3);
$code = $this->uri->segment(4);
//fetch user details
$user = $this->users_model->getUser($id);
//if code matches
if($user['code'] == $code){
//update user active status
$data['active'] = true;
$query = $this->users_model->activate($data, $id);
if($query){
$this->session->set_flashdata('message', 'User activated successfully');
}
else{
$this->session->set_flashdata('message', 'Something went wrong in activating account');
}
}
else{
$this->session->set_flashdata('message', 'Cannot activate account. Code didnt match');
}
redirect('register');
}
}

In your configuration change protocol to "smtp"
$config = array(
'smtp_host'=> "smtp.gmail.com",
"smtp_user"=> 'Username', // YOUR Username
"smtp_pass" => 'Password', // YOUR Password
'protocol' => 'smtp',
'smtp_crypto' => 'ssl',
"smtp_port" => 465,
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,
'mailtype' => 'html',
'newline' => "\r\n"
);
If you are using gmail account,
Make sure you have turn on "Less secure app" here

Related

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'
);

Clicking on verification email getting error in codeigniter

Once submittied the form sending an email with verification link to user clicking on that link getting 500 internal server error.Once i click on the link status is to changed to 1 from 0 and need to set the time limit for the link
Error:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
Here is my code for email verification.
Signup:
function signup()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('email','Email','required|is_unique[profile_details.email]');
if($this->form_validation->run()== FALSE)
{
$data['country'] = $this->signup_model->getcountry();
$data['mainpage']='signup';
$this->load->view('templates/template',$data);
}
else
{
$data=array(
'email'=>$this->input->post('email'),
);
if($this->signup_model->insertprofiledetails($data))
{
if ($this->signup_model->sendEmail($this->input->post('email')))
{
redirect("index.php/welcome/add");
}
else
{
redirect("index.php/welcome/add");
}
}
else
{
redirect('index.php/welcome/add');
}
}
}
function verify($hash=NULL)
{
if ($this->signup_model->verifyEmailID($hash))
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-success text-center">Your Email Address is successfully verified! Please login to access your account!</div>');
redirect('index.php/welcome/add');
}
else
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-danger text-center">Sorry! There is error verifying your Email Address!</div>');
redirect('index.php/welcome/add');
}
}
Model:
function sendEmail($to_email)
{
//configure email settings
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxx#gmail.com',
'smtp_pass' => 'yyyyy', //$from_email password
'mailtype' =>'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
//send mail
$this->load->library('email',$config);
$this->email->from('xxxx#gmail.com', 'Admin');
$this->email->to('yyyyy#gmail.com');
$this->email->subject('Email Verification');
$this->email->message('Dear User,<br /><br />Please click on the below activation link to verify your email address.<br /><br /> http://qa.domain.in/index.php/welcome/verify/' . md5($to_email) . '<br /><br /><br />Thanks<br />Mydomain Team');
$this->email->set_newline("\r\n");
if($this->email->send())
{
redirect('welcome/add');
} else { echo $this->email->print_debugger(); }
}
function verifyEmailID($key)
{
$data = array('status' => 1);
$this->db->where('md5(email)', $key);
return $this->db->update('profile_details', $data);
}
Check to see if you are missing any closing brackets for any methods in the signup_model class or the sign_up model class itself or for the controller

sent email verification link with php condeigniter

I want to send an email verification link to the user with PHP CodeIgniter.
This is my controller function.
public function Sent_Confirmation_Email()
{
$emailid = $this->uri->segment(3);
$verificationLink = base_url() . 'MainController/Confirm_Activation/'.$emailid;
$msg .= "Please use the link below to activate your account..<br /><br /><br />";
$msg .= "<a href='".$verificationLink."' target='_blank'>VERIFY EMAIL</a><br /><br /><br />";
$msg .= "Kind regards,<br />";
$msg .= "Company Name";
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'sender#gmail.com',
'smtp_pass' => 'password'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->isHTML(true);
$this->email->from("sender#gmail.com");
$this->email->to("$emailid");
$this->email->subject("Email Confirmation - Courses and Tutors");
$this->email->message($msg);
if($this->email->send())
{
$this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
} else {
show_error($this->email->print_debugger());
}
}
Note that I am sending emails from my localhost. I receive the email but the problem is it shows the html tags as well. This is the email which I received:
Please use the link below to activate your account..<br /><br /><br /><a
href='http://localhost/tutorhunt/MainController/Confirm_Activation/fareedshuja#gmail.com'
target='_blank'>VERIFY EMAIL</a><br /><br /><br />Kind regards,<br />Company
Name
Try to initialize email library and add mailtype as following
$this->email->initialize(array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://mailserver',
'smtp_user' => 'user',
'smtp_pass' => 'password',
'smtp_port' => 465,
'crlf' => "\r\n",
'newline' => "\r\n",
'mailtype' => 'html',
));
To Send Verification Link In Email Using Email Template is simpler than adding HTML content as a message and send it.
Creare an array of the variable which you want to send in an email.
Load that email template view and load that variable array and set the variable in the template properly.
Here is the code sample:
$this->email->set_newline("\r\n");
$this->email->isHTML(true);
$this->email->from("sender#gmail.com");
$this->email->to("$emailid");
$this->email->subject("Email Confirmation - Courses and Tutors");
$template_data = array(
'verificationLink' => base_url() . 'MainController/Confirm_Activation/'.$emailid,
'message' => 'Please use the link below to activate your account..',
'company_name' => 'test company'
);
$body = $this->load->view ('your_view.php', ['template_data'=>$template_data], TRUE); //Set the variable in template Properly
$this->email->message ( $body );
if($this->email->send())
{
$this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
} else {
show_error($this->email->print_debugger());
}

Codeigniter ion auth forgot password not sending email

If I submit to reset the new password to my email, CodeIgniter is not sending mail. But it's returning a message that Password Reset Email Sent. So it doesn't trigger an error.
I'm using CodeIgniter 2.1.4 and IonAuth 2.5.2
$config['use_ci_email'] = TRUE;
I already set this config to TRUE, and still not sending mail.
Not change anything in 'ion_auth.php'. file as it is like :
$config['use_ci_email'] = FALSE; // Send Email using the builtin CI email class, if false it will return the code and the identity
$config['email_config'] = array(
'mailtype' => 'html',
);
and then 'Auth.php' which is controller file in change some code which is like :
if ($forgotten)
{
// if there were no errors
// $this->session->set_flashdata('message', $this->ion_auth->messages());
// redirect("auth/login"); //we should display a confirmation page here instead of the login page
$config = [
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html'
];
$data = array(
'identity'=>$forgotten['identity'],
'forgotten_password_code' => $forgotten['forgotten_password_code'],
);
$this->load->library('email');
$this->email->initialize($config);
$this->load->helpers('url');
$this->email->set_newline("\r\n");
$this->email->from('xxx');
$this->email->to("xxx");
$this->email->subject("forgot password");
$body = $this->load->view('auth/email/forgot_password.tpl.php',$data,TRUE);
$this->email->message($body);
if ($this->email->send()) {
$this->session->set_flashdata('success','Email Send sucessfully');
return redirect('auth/login');
}
else {
echo "Email not send .....";
show_error($this->email->print_debugger());
}
}
else
{
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect("auth/forgot_password");
}
I use config in codeigniter
application/config/email.php
$config['protocol'] = 'mail';
$config['wordwrap'] = false;
$config['mailtype'] = 'html';
application/config/autoload.php
$autoload['libraries'] = array('lang','template', 'email','form_validation','session','encrypt','pagination','upload','database' );
In controller
$this->email->from(MAIL,MAIL);
$this->email->to($mailto);
$this->email->subject($text_subject);
$this->email->message($this->load->view('email/template_email',$data,TRUE));
$this->email->send();

cannot send email using CodeIgniter

I was trying sent email using PHP CodeIgniter framework. after i uploaded that file into my browser it didn't show me any error. it said:"Your email was sent successfully". But i didn't get any email in my email account. I could not figure out what was the problem. I am using CodeIgniter version 2.1.3. can anyone please help me. I am new in PHP. Thank You.
Here is my code:
<?php
class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->library('email');
$this->email->from('hasib32#gmail.com', 'Hasan Hasibul');
$this->email->to('riar32#gmail.com');
$this->email->subject('email test');
$this->email->message('testing the email class. email sent');
if($this->email->send()){
echo"Your email was sent successfully";
}else
{
show_error($this->email->print_debugger());
}
}
}
You can try this:
$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
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$email_setting = array('mailtype'=>'html');
$this->email->initialize($email_setting);
$email_body ="<div>hello world</div>";
$this->email->from('...#gmail.com', 'shahriar');
$list = array('...#gmail.com');
$this->email->to($list);
$this->email->subject('Testing Email');
$this->email->message($email_body);
$this->email->send();
echo $this->email->print_debugger();
}
this works for me. happy coding :)
Its because you don't have mail server setup in your localhost. You can either setup it or you can use your gmail account to send your mail like this-
$config = Array(
‘protocol’ => ‘smtp’,
‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
‘smtp_port’ => 465,
‘smtp_user’ => ‘myusername#gmail.com’,
‘smtp_pass’ => ‘mypassword’,
);
$this->load->library('email', $config);
$this->email->from('hasib32#gmail.com', 'Hasan Hasibul');
$this->email->to('riar32#gmail.com');
$this->email->subject('email test');
$this->email->message('testing the email class. email sent');
if($this->email->send()){
echo"Your email was sent successfully";
} else {
show_error($this->email->print_debugger());
}
Check e-mail server in the mail queue. probably waiting.

Categories