Sending email after inserting the data into database using codeigniter PHP is not working.Data is inserting succesfully but the MAIL functionality is not working getting as www.hostname.com page isn’t working.Can any one help me this.Thanks in advance.Here is my code.
Controller:
class Blog extends CI_Controller
{
function __construct()
{
parent::__construct();
//here we will autoload the pagination library
$this->load->model('blogs_model');
$this->load->library('email');
}
function addcomments()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('first_name','First Name' , 'required');
$this->form_validation->set_rules('email','Email');
$this->form_validation->set_rules('location','Location');
$this->form_validation->set_rules('description','Description');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='blogs';
$this->load->view('templates/template',$data);
}
else
{
//insert the user registration details into database
$data=array(
'blog_id'=>$this->input->post('bl_id'),
'first_name'=>$this->input->post('first_name'),
'email'=>$this->input->post('email'),
'description'=>$this->input->post('description'),
'location'=>$this->input->post('location')
);
// insert form data into database
if ($this->blogs_model -> insertcomments($data))
{
// send email
if ($this->blogs_model->send_mail($this->input->post('email')))
{
// successfully sent mail
$this->flash->success('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
redirect("blog");
}
else
{
// error
$this->flash->success('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect("blog");
}
}
else
{
// error
$this->flash->success('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('blog');
}
}
}
}
Model:
function insertcomments($data)
{
return $this->db->insert('comments', $data);
//$this->db->insert('comments',$data);
//return $this->input->post('bl_id');
}
function sendEmail($to_email)
{
//configure email settings
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxx#gmail.com',
'smtp_pass' => '************', //$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('yyy#gmail.com');
$this->email->subject('Comments');
$this->email->message('Testing');
$this->email->set_newline("\r\n");
return $this->email->send();
}
You are using wrong method name for sending email in your controller:
$this->blogs_model->send_mail($this->input->post('email'))
Correct function name is sendEmail()
$this->blogs_model->sendEmail($this->input->post('email'))
Check your code:
You should change function "send_mail" in your controller because in model you used "sendEmail".
Change in to your controller :
$this->blogs_model->sendEmail($this->input->post('email'))
I think the problems in your code is this line
smtp_host' => 'ssl://smtp.googlemail.com
try instead: smtp_host' => 'http://smtp.gmail.com
Related
I have the code below to send emails using the Mail::to function. But Im not understanding how to set the subject and body of the message with the Mail::to function. I have the code below that is working to send emails but without subject and the $message is also not appearing in the email.
Do you know how to properly achieve that? (Have subject and the $request->message in the email using Mail::to)
public function send(Request $request, $id){
$conference = Conference::find($id);
if($request->send_to == "participant"){
// if is to send only 1 email the Mail::to is called directly here
Mail::to($request->participant_email)->send(new Notification($conference));
return;
}
if($request->send_to == "all"){
// $sendTo = query to get a set of emails to send the email
}
else{
// $sendTo = query to get a another set of emails to send the email
}
foreach($sendTo as $user){
$usersEmail[] = $user->email;
}
$message = $request->message;
$subject = $request->subject;
foreach ($usersEmail as $userEmail){
Mail::to($userEmail)->send(new Notification($conference, $message));
}
}
In the class Notification I have:
class Notification extends Mailable
{
public $conference;
public function __construct(Conference $conference)
{
$this->conference = $conference;
}
public function build()
{
return $this->markdown('emails.notification');
}
}
In the view notifications.blade.php I have:
#component('mail::message')
# Notification relative to {{$conference->name}}
{{$message}}
Thanks,<br>
{{ config('app.name') }}
#endcomponent
Try something like this:
$emailData = array(
/* Email data */
'email' => 'user#email.com',
'name' => 'User name',
'subject' => 'Email subject',
);
Mail::send('emails.template_name', ['emailData' => $emailData], function ($m) use ($emailData) { // here it is a closure function, in which $emailData data is available in $m
$m->from('info#domain.com', 'Domain Name');
$m->to($emailData['email'], $emailData['name'])->subject($emailData['subject']);
});
try
{
Mail::send('emails.contact_form', ['data' => $data],
function($message) use ($data)
{
$message->from('emailasinenv#hosting.com', 'ShortName');
$message->to( $data['adminEmail'] )->subject("Contact Form" );
});
return true;
}
catch (\Exception $ex) {
$ex->getMessage();
return false;
}
As you already have one template in your code use that template, Pass the message to template
$subject = 'Email Subject';
Mail::send('emails.notification', ['message' => $message], function ($mail) use ($userEmail, $subject) {
$mail->from('info#domain.com', 'Domain Name');
$mail->to($userEmail)->subject($subject);
});
I am new in codeigniter framework. I'm trying to send email but I have the problem:
Call to undefined method CI_Loader::libary()
here is my script on controller :
class Email extends CI_Controller{
function __construct()
{
parent::__construct();
}
function index()
{
$config = array(
'protocol' => 'smtp',
'smtp_host'=>'ss://smtp.googlemail.com',
'smtp_port'=>465,
'smtp_user'=>'thaitea.lumajang#gmail.com',
'smtp_pass'=>'thaitealumajang1'
);
$this->load->libary('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('thaitea.lumajang#gmail.com','Tri Wicaksono');
$this->email->to('cha8agust#gmail.com');
$this->email->subject('This is an email test');
$this->email->message('It is working bro');
if($this->email->send())
{
echo 'Your email send';
} else {
show_error($this->email->print_debbuger());
}
}
}
I already find out the answer.
There is wrong typing in $this->load->libary.
It should be $this->load->library - there is a missing r in library word.
The follwing code is related to the signup part in my web application developed using codeigniter
<?php
class user extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
$this->register();
}
function register()
{
//set validation rules
$this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]|md5');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');
//validate form input
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('user_registration_view');
}
else
{
//insert the user registration details into database
$data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
// insert form data into database
if ($this->user_model->insertUser($data))
{
// send email
if ($this->user_model->sendEmail($this->input->post('email')))
{
// successfully sent mail
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
redirect('user/register');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user/register');
}
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user/register');
}
}
}
function verify($hash=NULL)
{
if ($this->user_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('user/register');
}
else
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-danger text-center">Sorry! There is error verifying your Email Address!</div>');
redirect('user/register');
}
}
}
?>
I have done the relevant validations for the relevant fields. But when trying to signup I get the error password does not match with the confirm password field though i entered same values for the password and confirm password fields! How can i correct it?
It's been a while since I used codeigniter(CI). I think you're hashing (md5) the first password but not the confirmation password. What happens if you try the following:
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');
If this fixes your problem you can hash if afterwards, like so:
//insert the user registration details into database
$data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password'))
# Replace md5 with cryptographic hashing function that isn't deprecated.
);
I'd recommend not using md5, especially not without a salt. You might want to check out crypt(), password_hash() and the top answer here.
I just installed a fresh installation of Wamp server and copied a working CodeIgniter project(not done by me) into it. I have changed the settings in config.php(base_url) and database.php(mysql credentials) and am sure that the database connection is ok.
i can see the login page but when i login, it returns me the "unable to locate the file error", i believe its some settings on the Wamp server i need to do. there is no .htaccess file in the root CI folder, should there be? any suggestions what might went wrong?
in the controllers folder there is an admin.php file
<?php
class Admin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->lang->load($this->config->item('admin_language'), $this->config->item('admin_language'));
}
private $rules = array(
array(
'field' => 'username',
'label' => 'lang:username',
'rules' => 'trim|required',
),
array(
'field' => 'password',
'label' => 'lang:password',
'rules' => 'trim|required|min_length[8]',
)
);
function index()
{
$this->load->library('form_validation');
$redirect_to = $this->config->item('base_url') . 'index.php/admin/products/';
if ($this->auth->logged_in() == FALSE)
{
$data['error'] = FALSE;
$this->load->library('form_validation');
$this->form_validation->set_rules($this->rules);
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
//echo "1";die;
if ($this->form_validation->run() == FALSE)
{
//echo "if";die;
$this->load->view('admin/login', $data);
}
else
{
//echo "el";die;
$this->auth->login($this->input->post('username'), $this->input->post('password'), $redirect_to, 'index.php/admin/login');
}
}
else
{
//echo "asdadad";die;
redirect($redirect_to);
}
}
function logout()
{
$this->auth->logout($this->config->item('base_url') . 'index.php/admin');
}
}
?>
Please change you function name index to login.
If you are using function name index then url will we localhost/projectname/index.php/admin
If you change function name to login then this will we work.
I have been trying to send an email to multiple members within my database. I managed to get the application to send one email to specific users emails, but when I use a list function I get errors (undefined index) that are supposedly in the libraries/email.php file but as far as I am aware I should not have to change the email library file. I am working on my localhost and here is the code:
function send(){
$this->load->model('mail_model', 'emails');
$emails=$this->emails->get_emails();
foreach($emails as $row){
if($row['email']){
$this->email->set_newline("\r\n");
$this->email->from('myemail#test.com', 'my name');
$this->email->to($row['email']);
$this->email->subject('Test multiple receivers');
$this->email->message('Test');
$this->email->send();
echo $this->email->print_debugger();
$this->email->clear();
$path = $this->config->item('server_root');
}
if($this->email->send())
{
//echo 'Your email was sent.';
$data['main_content'] = 'signup_confirmation';
$this->load->view('includes/template', $data);
}
else
{
show_error($this->email->print_debugger());
}
}
}
I have already autoloaded the email library in the config files. This is the code from the model:
class Mail_model extends CI_Model
{
function get($id = null)
{
$this->db->select('mailing_member_id', 'mailing_list_id', 'email_address');
$this->db->from('mailing_members');
if (!is_null($id)) $this->db->where('mailing_member_id', $id);
$this->db->order_by('mailing_member_id', 'desc');
return $this->db->get()->result();
}
Your model's get()method returns an object instead of an array. Try:
return $this->db->get()->result_array();
Also, you need $row['email_address']not $row['email'] for the ifstatement.