Registration email won't send in code igniter - php

I wrote an function to send an email to the user using the email they put on their registration form. However while testing it the email does not go to the email address entered. I checked to make sure that the function was working by having the email cc to an address, which worked. Below is the function.
View
<div class="col-lg-4 col-lg-offset-4">
<h2>Hello There</h2>
<h5>Please enter the required information below.</h5>
<?php
$fattr = array('class' => 'form-signin');
echo form_open('/main/register', $fattr); ?>
<div class="form-group">
<?php echo form_input(array('name'=>'firstname', 'id'=> 'firstname', 'placeholder'=>'First Name', 'class'=>'form-control', 'value' => set_value('firstname'))); ?>
<?php echo form_error('firstname');?>
</div>
<div class="form-group">
<?php echo form_input(array('name'=>'lastname', 'id'=> 'lastname', 'placeholder'=>'Last Name', 'class'=>'form-control', 'value'=> set_value('lastname'))); ?>
<?php echo form_error('lastname');?>
</div>
<div class="form-group">
<?php echo form_input(array('name'=>'email', 'id'=> 'email', 'placeholder'=>'Email', 'class'=>'form-control', 'value'=> set_value('email'))); ?>
<?php echo form_error('email');?>
</div>
<?php echo form_submit(array('value'=>'Sign up', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
<?php echo form_close(); ?>
</div>
Controller
public function register()
{
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('register');
$this->load->view('footer');
}else{
if($this->user_model->isDuplicate($this->input->post('email'))){
$this->session->set_flashdata('flash_message', 'User email already exists');
redirect(site_url().'main/login');
}else{
$clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
$id = $this->user_model->insertUser($clean);
$token = $this->user_model->insertToken($id);
$qstring = base64_encode($token);
$url = site_url() . 'main/complete/token/' . $qstring;
$link = '' . $url . '';
$message = '';
$message .= '<strong>You have signed up with our website</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'host',
'smtp_port' => 'port',
'smtp_user' => 'username',
'smtp_pass' => 'password');
$this->load->library('email', $config);
$this->email->set_newline("/r/n");
$this->email->from('***#****.com', 'High Ball Run8');
$this->email->to(set_value("email"));
$this->email->cc('****#*****.com');
$this->email->subject('Highball Registration Email Activation');
$this->email->message($message);
if ($this->email->send()) {
redirect(site_url());
}else{
show_error($this->email->print_debugger());
}
exit;

Have you tried
var_dump(set_value("email")); exit();
To see what the output is? Chances are you are not getting the to address correctly, if the hardcoded CC works.

Please check following working code
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx#gmail.com', // change it to yours
'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());
}
}
Reference

Related

why did my attempt to send my email not work status HTTP 303 See Other in codeigniter

why is my attempt to send the email unsuccessful, I can't see the error, all I get is the http status code 303 See More
In my opinion, what I'm doing is in accordance with the official documentation https://codeigniter.com/userguide3/libraries/email.html
the result i expect is an email sent
Can anyone help me?
, How can I solve this?
config/email.php
$config['settings'] = array(
'smtp_host' => 'smtp.mailtrap.io',
'smtp_port' => '2525',
'smtp_user' => '9cf16dcde1asdasde5f8',
'smtp_pass' => '079205e1asdasd26bf43',
'from_email' => 'no-reply#test.com',
'from_name' => 'Test',
'protocol' => 'smtp',
'smtp_crypto' => 'tls',
'mailtype' => 'html',
'charset' => 'utf-8',
'newline' => "\r\n",
'crlf' => "\r\n",
);
libraries/Ah_Email.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Ah_Email {
public function __construct()
{
$CI =& get_instance();
$CI->load->config('email', true);
$this->configs = $CI->config->config['email'];
$CI->load->library('email');
$this->email = $CI->email;
}
public function send($email_to, $subject, $template, $template_vars = array(), $settings = array())
{
$settings = array_merge(
$this->configs['settings'],
$settings
);
$this->email->initialize($settings);
$this->email->from($settings['from_email'], $settings['from_name']);
if (isset($settings['send_email']) && $settings['send_email']) {
$this->email->to($email_to);
}
$CI =& get_instance();
$this->email->subject($subject);
$this->email->message($CI->load->view('emails/' . $template, $template_vars, true));
$res = $this->email->send();
return $res;
}
}
My Controller
public function SendMail() {
$this->load->library('ah_email', 'ah_email');
$send = $this->ah_email->send('receiver#gmail.com', 'Informasi Kode OTP Akun Kamu', 'example', array(
'username' => 'Ndo Malau',
'message' => 'Hello World',
'mailtype' => 'text',
));
if($send) {
$result = redirect(base_url('Systems/Users/index/'), $this->session->set_flashdata('succ_msg', 'success, email has been sending!'));
} else {
$result = redirect(base_url('Systems/Users/index/'), $this->session->set_flashdata('err_msg', 'failed, error while email sending'));
}
return $result;
}

How to send email using php codeigniter?

<?php
class SendEmail extends Controller
{
function SendEmail()
{
parent::Controller();
$this->load->library('email'); // load the library
}
function index()
{
$this->sendEmail();
}
public function sendEmail()
{
// Email configuration
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com.',
'smtp_port' => 465,
'smtp_user' => 'xxxx', // change it to yours
'smtp_pass' => 'xxxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('xxxx', "Admin Team");
$this->email->to("xxxx");
$this->email->cc("xxxx");
$this->email->subject("This is test subject line");
$this->email->message("Mail sent test message...");
$data['message'] = "Sorry Unable to send email...";
if ($this->email->send()) {
$data['message'] = "Mail sent...";
}
// forward to index page
$this->load->view('index', $data);
}
}
?>
I get an error - I am doing it in codeigniter PHP Error was encountered
Severity: Compile Error
Message: Cannot redeclare SendEmail::sendEmail()
Filename: controllers/SendEmail.php
Line Number: 16
Backtrace:
Controller code
<?php
class SendEmail extends Controller
{
function __construct()
{
parent::__construct();
$this->load->library('email'); // load the library
}
function index()
{
$this->sendEmail();
}
public function sendEmail()
{
// Email configuration
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com.',
'smtp_port' => 465,
'smtp_user' => 'xxxx', // change it to yours
'smtp_pass' => 'xxxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('xxxx', "Admin Team");
$this->email->to("xxxx");
$this->email->cc("xxxx");
$this->email->subject("This is test subject line");
$this->email->message("Mail sent test message...");
$data['message'] = "Sorry Unable to send email...";
if ($this->email->send()) {
$data['message'] = "Mail sent...";
}
// forward to index page
$this->load->view('index', $data);
}
}
?>

Uploading a resume and sending an email displaying blank page in codeigniter php

Having a career page where i will be filling the details and attaching the resume and sending an email once the user fills the data along with the resume displaying blank page.
Working fine if we are not adding upload resume option.
var $image_path;
var $image_path_url;
function __construct()
{
parent::__construct();
$this->image_path =realpath(APPPATH . '..admin/images/blogimages');
$this->image_path_url = base_url().'admin/images/blogimages/thumbs';
}
function apply($email)
{
$name = $this->input->post('fullname');
$email = $this->input->post('email');
$phone = $this->input->post('mobilenumber');
if ( $_FILES AND $_FILES['image_path']['name'])
{
$file_name = $this->do_upload2();
if(is_array($file_name))
{
$error['imageerror'] = $file_name['error'];
}
else
$data['image_path']=$file_name;
}
if(!isset($data['image_path']) && !isset($error['imageerror']))
$error['imageerror'] ="Please Upload an image";
if(isset($error))return $error;
//set to_email id to which you want to receive mails
$to_email = 'yyyy#gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxxx#gmail.com',
'smtp_pass' => 'PASSWORD123', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = array();
$message[] = 'Fullname : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Mobile : '.trim($phone).' ';
//$message = implode(PHP_EOL, $message);
$message = implode('<br>', $message);
//send mail
$this->load->library('email',$config);
$this->email->from($email);
$this->email->to($to_email);
//$list = array();
$path = set_realpath('admin/images/blogimages/thumbs');
$this->email->message($message);
$this->email->attach($path . 'image_path');
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
if ($this->email->send())
{
$this->flash->success('Thank you for applying to this post we will get back to you soon!</div>');
redirect('apply');
}
else
{
$this->flash->success('There is error in sending mail! Please try again later');
redirect('apply');
}
}
function do_upload2()
{
$config = array(
'allowed_types' => 'doc|docx|pdf',
'upload_path' => $this->image_path,
'max_size' => 20000,
'maintain_ratio'=>FALSE
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('image_path'))
{
return $error = array('error' => $this->upload->display_errors());
}
else
{
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' =>$this->image_path . '/thumbs',
'maintain_ratio' => FALSE,
'width' => 83,
'height' => 83
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$filename =time().preg_replace('/[^A-Za-z0-9\s.\s-]/', '_', $image_data['file_name']);
rename($image_data['full_path'],$image_data['file_path'].$filename);
rename($image_data['file_path'].'thumbs/'.$image_data['file_name'],$image_data['file_path'].'thumbs/'.$filename);
return $filename;
}
}
Add these lines for uploading images and files while sending email.
$config2 = array(
'upload_path'=>'../admin/images/blogimages',
'allowed_types' => 'pdf|doc|docx',
'max_size' => 20000,
);
$this->load->library('upload', $config2);
$this->upload->do_upload('attachment');
$upload_data = $this->upload->data();
$this->email->attach($upload_data['full_path']);

warning if register with email / username is already registered

I tried to make the project regrister using CodeIgniter framework , with verification email after list and it can be used. but there is weakness in my scirpt . when the register if he is using the same user then when submitted will appear statement that "you 've registered email" .. what should I add to controllers login.
function submit() {
//passing post data dari view
$_POST['dob'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$password = $this->input->post('password');
$email = $this->input->post('email');
$dob = $this->input->post('dob');
$jkl = $this->input->post('jkl');
$lastlogin = $this->input->post('lastlogin');
//memasukan ke array
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'password' => $password,
'email' => $email,
'dob' => $dob,
'jkl' => $jkl,
'lastlogin' => $lastlogin,
'active' => 0
);
//tambahkan akun ke database
$this->m_register->add_account($data);
//redirect(base_url().'homepage/homepage');
$id = $this->m_register->add_account($data);
//enkripsi id
$encrypted_id = md5($id);
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '*******#*****esy.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);
$this->email->from('jobrecruit#jobrecruit.esy.es', 'JOBRECRUIT');
$this->email->to($email);
$this->email->subject('Confirmation Email');
$this->email->message("WELCOME TO JOB RECRUIT <br><p></p>Hallo $firstname $lastname <br><br><br><p>Terimakasih telah melakuan registrasi dengan:<br><br><p>
Username = $email<p>
Password = $password
<br><br>
<p>
untuk memverifikasi akun silahkan klik tautan dibawah ini</p><br><br>"
.site_url("login/register/verification/$encrypted_id")."
<br><br><br>
<p></p><br>
<p>Thanks</p>Admin JOBRECRUIT");
if($this->email->send())
{
$data = array ( 'isi' => 'login/vsuccess');
$this->load->view('layout/wrapper',$data);
}else
{
$data = array ( 'isi' => 'login/vgagal');
$this->load->view('layout/wrapper',$data);
}
}
I will suggest you to use form validation library of codeigniter.
function submit() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|xss_clean|is_unique[TABLE_NAME.email]');
$this->form_validation->set_message('is_unique', 'you have registered email.');
if($this->form_validation->run())
{
//passing post data dari view
$_POST['dob'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$password = $this->input->post('password');
$email = $this->input->post('email');
$dob = $this->input->post('dob');
$jkl = $this->input->post('jkl');
$lastlogin = $this->input->post('lastlogin');
//memasukan ke array
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'password' => $password,
'email' => $email,
'dob' => $dob,
'jkl' => $jkl,
'lastlogin' => $lastlogin,
'active' => 0
);
//tambahkan akun ke database
$this->m_register->add_account($data);
//redirect(base_url().'homepage/homepage');
$id = $this->m_register->add_account($data);
//enkripsi id
$encrypted_id = md5($id);
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'jobrecruit#jobrecruit.esy.es ',
'smtp_pass' => 'jobrecruit123456',
'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);
$this->email->from('jobrecruit#jobrecruit.esy.es', 'JOBRECRUIT');
$this->email->to($email);
$this->email->subject('Confirmation Email');
$this->email->message("WELCOME TO JOB RECRUIT <br><p></p>Hallo $firstname $lastname <br><br><br><p>Terimakasih telah melakuan registrasi dengan:<br><br><p>
Username = $email<p>
Password = $password
<br><br>
<p>
untuk memverifikasi akun silahkan klik tautan dibawah ini</p><br><br>"
.site_url("login/register/verification/$encrypted_id")."
<br><br><br>
<p></p><br>
<p>Thanks</p>Admin JOBRECRUIT");
if($this->email->send())
{
$data = array ( 'isi' => 'login/vsuccess');
$this->load->view('layout/wrapper',$data);
}else
{
$data = array ( 'isi' => 'login/vgagal');
$this->load->view('layout/wrapper',$data);
}
}
}
Error message will be accessible in form_validation() or for specific form_error('email') print as it is on view to show error message
In Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
// if($this->session->userdata('user'))redirect('homepage/menu');
}
public function index($param='')
{
if($param == 'error')
$param = 'Incorrect username or password';
if($param == 'error1')
$param = 'User not acctive';
$data = array('title'=>'Login','message'=>$param, 'isi' => 'login/vlogin', 'base_url'=>base_url());
$this->load->view('layout/wrapper', $data);
}
public function do_login() {
$mail = $_POST['mail'];
$password = $_POST['password'];
$result = $this->Model_name->check_valid_user($mail,$password);
if ($result == 1)
{ //User exists
if ($result['active'] == 1)
{
//User exists and his email is verified
$session_set = array(
'user' => true,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'jkl' => $user->jkl,
'id' => $user->id,
'lastlogin' => $user->lastlogin
);
$this->Model_name->update_last_login($result['id']);
$this->session->set_userdata($session_set);
redirect('homepage/menu');
}
else
{
//User exists BUT his email is NOT verified
$this->session->set_flashdata('message', 'Akun anda belum aktif silahkan cek email anda untuk verifikasi');
//You have to capture and show the flash message in view
redirect('login/login/index');
}
}
else
{
//User does NOT exist at all
$this->session->set_flashdata('message', 'Username dan Password tidak sama.');
//You have to capture and show the flash message in view
redirect('login/login/index');
}
}
}
In Model
public function check_valid_user($mail,$password)
{
$query = $this->db->quesry("SELECT * FROM user WHERE mail='$mail' AND password = '$password'");
$result = $query->result_array();
$count = count($result);
if(empty($count) || $count >1 )
{
$log = 0;
return $log;
}
else
{
$log = 1;
return $log;
}
}
function update_last_login($id)
{
$data = array(
'lastlogin' => date('Y-m-d H:i:s')
);
$this->db->where('id', $id);
$this->db->update('user', $data);
}

Email Class in Codeigniter Using Form

Disclaimer: I'm new to web develoment.
Scenario: I've created a contact form, and I'm trying to pass the input into the email class functions used in CodeIgniter. I am receiving undefined variable errors when the form is submitted. Email library is autoloaded for reference. It's late, and I may be overlooking something easy, but it doesn't hurt to post. Thanks a lot for all of your help!
Controller:
public function index()
{
//form validation
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['subject'] = array(
'name' => 'subject',
'id' => 'subject',
'type' => 'text',
'value' => $this->form_validation->set_value('subject'),
);
$this->data['message'] = array(
'name' => 'message',
'id' => 'message',
'type' => 'text',
'value' => $this->form_validation->set_value('message'),
);
if ($this->form_validation->run() == true)
{
$this->email->from($email);
$this->email->to('support#example.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect('contact/success');
}
else
{
$this->data['error_message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error_message');
$title['title'] = 'Contact';
$this->load->view('public/head_view', $title);
$this->load->view('public/header_view');
$this->load->view('public/contact_view', $this->data);
$this->load->view('public/footer_view');
}
View:
<div id="infoMessage"><?php echo $error_message;?></div>
<?php $attritubes = array('class' => 'nice'); ?>
<?php echo form_open('contact'); ?>
<p>Email Address:<br />
<?php echo form_input($email); ?>
</p>
<p>Subject:<br />
<?php echo form_input($subject); ?>
</p>
<p>Message:<br />
<?php echo form_textarea($message); ?>
</p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<?php echo form_close(); ?>
Well, you barely define any variable here:
if ($this->form_validation->run() == true)
{
$this->email->from($email);
$this->email->to('support#example.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect('contact/success');
}
Where do $email, $subject, $message come from?
You might want to use something like (but I suggest you do better :))
$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
Also, make sure you've loaded the email library before calling it, and that you load the view in your else{} part (since you omitted it I cannot tell)

Categories