Codeigniter 3: send account activation email fails - arguments

I am working on a Register and Login application with CodeIgniter 3 and Twitter Bootstrap.
When a user registers, an email should be send to the address he/she provided, with an account confirmation link. The problem is that the confirmation email does not send.
In the Usermodel I have:
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
{
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.code-love.tk',
'smtp_port' => 465,
'smtp_user' => 'razvan#code-love.tk',
'smtp_pass' => '******',
'smtp_crypto' => 'ssl',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$messg = 'Wellcome, '. $first_name . ' ' . $last_name . '! Click the <strong>confirmation link</strong> to confirm your account.';
$this->email->initialize($config);
$this->email->set_newline('\r\n');
$this->email->from('mail.code-love.tk','Razvan Zamfir');
$this->email->to($email);
$this->email->subject('Account activation');
$this->email->message($messg);
if (!$this->email->send()) {
show_error($this->email->print_debugger());
}
else {
echo 'Your e-mail has been sent!';
}
}
In my Signup controller I have this code:
public function signup() {
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$verification_key = md5($email);
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$active = 0;
// Load user model
$this->load->model('Usermodel');
// If email does not already exist in the database
// signup new user
if (!$this->Usermodel->email_exists($email)) {
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $verification_key, $date_created, $date_updated, $active) && $this->Usermodel->activationEmail($first_name, $last_name, $email, $verification_key)) {
$this->session->set_flashdata("signup_success", "Your account has just bean created. You must confirm it before you can sign in. We have send you a confirmation email at $email for this purpose.");
} else {
// unless sigup does not fail for whatever reason
$this->session->set_flashdata("signup_failure", "We ware unable to create your account.");
}
redirect('signup');
} else {
// If email is already in the database
// urge user to sign in (redirect to signup page too)
$this->session->set_flashdata("email_exists", "The email address $email already exists in the database. Please signin.");
redirect('signin');
}
} else {
$this->load->view('signup');
}
}
The sign up does happen, with the error below, and the verification email is not send. I am not doing this from a local XAMPP/WAMP/MAMP server, but from a "live" one, you can signup yourself.
Severity: Warning
Message: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Filename: libraries/Email.php
Line Number: 1950
Backtrace:
File: /home/roxoqdat/code-love.tk/ciauth/application/models/Usermodel.php
Line: 52
Function: send
File: /home/roxoqdat/code-love.tk/ciauth/application/controllers/Signup.php
Line: 42
Function: activationEmail
File: /home/roxoqdat/code-love.tk/ciauth/index.php
Line: 292
Function: require_once
What am I doing wrong?

Check if you have the required SMTP service.
Try with google SMTP service.
$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");
$this->email->from('mygmail#gmail.com', 'myname');
$this->email->to('target#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$result = $this->email->send();
Next try with:
$config = array('mailtype' => 'html');
Remove the rest from $config = array.

First of all, after setting flashdata, I don't see you redirecting the user to where they can see it...
Also, send the mail first, then you set flash data as An Activation Link Has Been Sent To Your Email, Please Check Your Email To Activate Your Account!
So,
User Registers,
An email is sent to them only if it doesn't exist already,
Set flashdata for any errors that may arise... Eg:
Assign a variable to the email sending function:
$send_email = $this->activationEmail('params');
In your activationEmail function, make sure there is a simple control structure like this:
$success=$this->email->send();
if($success){ return true; } else{ return false; }
Now in your controller, compare to see if mail is actually sent:
if($send_email == true){ $this->session->flashdata('success','Your success message');
// use the redirect() helper function to the function that loads your view
} else { //set failed flashdata and redirect to where you want it seen like above}
You then will know where the problem is.
Let the program flow in your mind first, then implement it. Try that first and get back to me right after.
Hope it somehow helps

Debug whether your parameters are passing or not (specially last_name). Try to write the model method like this to avoid showing errors
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
Try adding header to your email (You are setting mailtype twice, might be an issue).
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');

Related

send email with attachment in CodeIgniter

Pdf attachment through email in CodeIgniter Mysource code here
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('test#gmail.com');
$this->email->to($useremail);
$this->email->cc('');
$this->email->subject('pdf');
$this->email->message($message);
$this->email->attach('public_html/invoicepdf/171.pdf');
$mailsucc = $this->email->send();
I tried with this but didnt work
$this->email->attach('public_html/invoicepdf/171.pdf');
And i also replace path with URL.
You can send attach file using
$this->email->attach($atch);
method in codeigniter. in this below code i'm sending mail using SMTP with
Attached File.
its Working perfectly.
You only need to specify base_url to define attachment file path
Controller
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'yourmail#gmail.com', // change it to yours
'smtp_pass' => 'mypasswords', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
// Defined Attachment file using variable
$atch=base_url().'html/images/logo.png';
$this->load->library('email', $email_config);
$this->email->set_newline("\r\n");
$this->email->from('test#gmail.com', 'Rudra'); // email From
$this->email->to('mailtosend#gmail.com'); // eMail to send
$this->email->subject('Mail with Attachment'); // Subject
$this->email->message("This is mail with Attachment file using CodeIgniter."); // Message
$this->email->attach($atch); // Attachment file defined using variable
$maill=$this->email->send(); // send mail
if($maill>0)
{
echo 'Email sent.'; // success
}
else
{
show_error($this->email->print_debugger());
}

Unable to send email from CodeIgniter

I am using CodeIgniter v3.1.4.In my website i have a 'Contact Us' page whose email sending function is not working at all.The strangest thing is that the exact code is working perfectly in one of my another project while its not in this project.
$this->email->send()
The above line executes but it does not send any emails.Neither does it show any error messages.However, if 'from' address & 'to' address are the same like(info#mywebsite.org) then it does send mail to itself.When 'from' address is like (user#gmail.com) and 'to' address is like (info#mywebsite.org), it does not work.
The code that i am using to send email is:
$this->form_validation->set_rules('sender_name', 'Name', 'required');
$this->form_validation->set_rules('sender_email', 'Enter Email', 'required|valid_email');
$this->form_validation->set_rules('mail_subject', 'Subject', 'required');
$this->form_validation->set_rules('mail_message', 'Your Message', 'required');
if (isset($_POST) && !empty($_POST))
{
if ($this->form_validation->run() == TRUE)
{
$this->email->from($this->input->post('sender_email'), $this->input->post('sender_name'));
$this->email->to("info#mywebsite.org");
$this->email->subject($this->input->post('mail_subject'));
$this->email->message($this->input->post('mail_message'));
if($this->email->send()){
$this->session->set_flashdata('success', 'Thanks for writing to us.You would hear from us shortly.');
}
else
{
$this->session->set_flashdata('error', 'Your message could not be sent.');
}
}
}
I tried to implement other solutions here in StackOverflow, but none of them are working.Please help me.
First Check With static data
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo "<pre>status<br>"print_r($this->email->print_debugger());die;

Get last insert_id in the URL in codeigniter

i have one requirement from in my application..so when ever user insert details in the requirement.and then submit..the mail will go to the particular vendor with one link..when vendor click on the link i redirect the page to login page..
So what i want to do is i want to send email link with last inserted id..
Here is my controller:
public function requirement()
{
$data["msg"]="";
$this->load->model('RequirementModel');
$data['user']=$this->RequirementModel->getusers();
$data['rolename']=$this->RequirementModel->getrolename();
if($this->input->post())
{
$this->RequirementModel->add_requirement($this->input->post());
$all_users = $this->input->post('user_id');
foreach($all_users as $key)
{
$get_email = $this->RequirementModel->get_user_email_by_id($key);
$req_id = $this->input->post('req_id');
$role_name = $this->input->post('role_name');
$vacancies = $this->input->post('vacancies');
$experience = $this->input->post('experience');
$jd = $this->input->post('jd');
$hiring_contact_name = $this->input->post('hiring_contact_name');
$hiring_contact_number = $this->input->post('hiring_contact_number');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to($get_email);
$this->email->subject('this is our requirements pls go through it');
$link = 'Click on this link - <a href="http://localhost/job_portal/index.php/Login/signin>Click Here</a>';
$this->email->message($link);
print_r($get_email);
if($this->email->send())
{
echo "email sent";
}
else
{
echo "email failed";
}
}
}
$this->load->view('Requirements/requirements',$data);
}
Can anyone help me..
Thanks in advance..
What you need is the database helper function insert_id(), it returns the id of the last insert: https://www.codeigniter.com/userguide3/database/helpers.html?highlight=insert_id
Have your add_requirement function of the RequirementModel model return the insert_id like so:
return $this->db->insert_id();
Then just save that value when you call the function:
$insert_id = $this->RequirementModel->add_requirement($this->input->post());

laravel5.2 Mailgun work but not send the email

Hello I setup mailgun correctly. and did form contact us.
To send the message to my email by this code
public function send_contact_us()
{
$data = array(
'name' => Request::get('name'),'email'=>Request::get('email'),'subject'=> Request::get('subject'));
$client_m=Request::get('message');
$data_message=array('message_c'=>$client_m);
echo "we above MAIL";
Mail::send('emails.message',$data_message, function ($message)use ($data) {
$message->from($data['email'], 'E-SHOPPER');
$message->to("azharnabil013#yahoo.com")->subject($data['subject']);
});
return view('contact_us', array('title' => 'Welcome', 'description' => '', 'page' => 'contact_us','subscribe'=>'','sent'=>"Message has been sent successfuly"));
}
The code run correctly and this page display
But when I check my email I didn't find any message
I don't know why this problem .please, anyone help me.

CodeIgniter 2 and Ion Auth - edit own user account

I am using the latest Ion Auth files along with the latest version of CodeIgniter 2.
There is a function called edit_user within the auth.php Controller file. This function is restricted to use only by members within the "Admin" group, and any Admin member can edit any other member using the function via this URL...
/auth/edit_user/id
The problem is that I don't see any Controller function or View that allows a regular (non-Admin) user to edit their own account details.
Would this be a new Controller function I'd need to write (modify edit_user function?) or is this something that Ion Auth should already do? If so, how?
Here is the stock Ion Auth edit_user function contained within the auth.php Controller...
function edit_user($id)
{
$this->data['title'] = "Edit User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
$user = $this->ion_auth->user($id)->row();
//process the phone number
if (isset($user->phone) && !empty($user->phone))
{
$user->phone = explode('-', $user->phone);
}
//validate form input
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
{
show_error('This form post did not pass our security checks.');
}
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
);
//update the password if it was posted
if ($this->input->post('password'))
{
$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
$data['password'] = $this->input->post('password');
}
if ($this->form_validation->run() === TRUE)
{
$this->ion_auth->update($user->id, $data);
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Saved");
redirect("auth", 'refresh');
}
}
//display the edit user form
$this->data['csrf'] = $this->_get_csrf_nonce();
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
//pass the user to the view
$this->data['user'] = $user;
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company', $user->company),
);
$this->data['phone1'] = array(
'name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
);
$this->data['phone2'] = array(
'name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
);
$this->data['phone3'] = array(
'name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password'
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password'
);
$this->load->view('auth/edit_user', $this->data);
}
Unless I'm missing something, I ended up modifying the edit_user function within the auth.php Controller as follows.
I changed this line which checks to see that the user is "not logged in" OR "not an admin" before dumping them out...
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
...into this, which check to see that the user is "not logged in" OR ("not an admin" AND "not the user") before dumping them out...
if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id)))
This seems to be working...
Admin can edit all accounts
User can only edit his own account
and somebody not logged in, can't edit any account.
Edit: However, the user also has access to the "groups" setting and could simply put themself into the "admin" group. Not good.
Ion Auth's developer refers to the files he provides as working "examples". Therefore, it's up to the end-developer to edit Ion Auth to suit the needs of the project.
To prevent the user from being able to make himself an "admin" requires a simple change to the edit_user.php view file.
Verifies the user is already an "admin" before creating the checkboxes...
<?php if ($this->ion_auth->is_admin()): ?>
// code that generates Groups checkboxes
<?php endif ?>
Then you'll also need to thoroughly test and adjust as needed. For example, after editing a user profile, you're redirected to the auth view. Since the user doesn't have permission to see the auth view, there is a "must be an admin" error. In the controller file, you'll have to add the appropriate logic to properly redirect the user when they're not an "admin".
No Ion Auth doesn't do this as is - it's pretty light weight. But it's not hard to do and your on the right track, just grab that edit_user method and take out the admin checks and make it so the user can only edit their own account, just alter it so that it only updates user details for the currently logged in user.
Check the ion auth docs, have a crack at it and come back with some code if you have any problems.

Resources