cannot send email using CodeIgniter - php

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.

Related

sending verification email via SMTP using gmail SMTP server failed in codeigniter

I am trying to send a registration verification email to my new user via Gmail SMTP. user data is successfully saved to the database but email is not sent to the user email ID.
I am working on localhost
here is my code
$subject = "Please verify email for login";
$message = "
<p>Hi " . $this->input->post('hospital_name') . "</p>
<p>This is email verification mail from Codeigniter Login Register system. For complete registration process and login into system.
First you want to verify you email by click this <a href='" . base_url() . "register/verify_email/" . $verification_key . "'>link</a>.</p>
<p>Once you click this link your email will be verified and you can login into system.</p>
<p>Thanks,</p>
";
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'tls://smtp.googlemail.com',
'smtp_port' => 587,
'smtp_user' => '****',
'smtp_pass' => '****',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('****');
$this->email->to($this->input->post('hospital_email'));
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
$this->session->set_flashdata('message', 'Check in your email for email verification mail');
redirect('register');
}else{
$this->session->set_flashdata('message', 'something wrong');
}
There could be a possibility that there might be something wrong with the code and it would be throwing some error. Did you check in the session, what is the message you're receiving?
If there's an error, try checking what the error could possibly be. You may check the error by using the below code.
if($this->email->send()) {
echo 'Your Email has successfully been sent.';
}
else{
show_error($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'
);

I cannot send any emails using Codeigniter

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.

How to get the response of bounced mail when working with google mail smtp (smtp.googlemail.com)

I am working on making newsletters. I have bulk of 1200 emails but it's not verified. I am using google mail smtp for verification in codeigniter framework. Mail is properly sending but when I tested it for wrong email then also it shows send mail status. I want to ask that how to get the response of bounced mail when working with google mail smtp.
$check is containing array of emails :
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'mymail#gmail.com',
'smtp_pass' => 'mypass',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'send_multipart' => FALSE
);
$this->email->initialize($config);
$this->load->library('email', $config);
foreach($_POST['chk'] as $check)
{
$this->email->clear();
$data = array(
'userName'=> 'Myname'
);
$this->email->set_newline("\r\n");
$this->load->library('email',$config);
$this->email->from("mymail#gmail.com","Myname");
//echo $row->email;
$this->email->to($check);
$this->email->subject("THIS IS AN EMAIL TEST");
$gett = $this->load->view('home_test.php',$data,TRUE);
$this->email->message($gett);
$this->email->set_mailtype("html");
if($this->email->send())
{
echo "Your Mail send";
}
else
{
show_error($this->email->print_debugger());
exit();
}
}
Its always showing Your Mail send although if I provide fake email id. How to track this bounced mail in PHP.
You will not get status of the bounced emails immediately after sent. What you can do is, specify return path email as third parameter of $this->email->from("mymail#gmail.com","Myname");.
Change this line
$this->email->from("mymail#gmail.com","Myname");
to
$this->email->from("mymail#gmail.com","Myname", 'yourreturnemail#gmail.com');
For more details see codigniter's email documentation here
This way all bounced emails will come to the return path email address. Then you can create a cron or something to read and parse this emails. php's imap extension can be installed and use for read email bounced status.

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

Categories