I am trying to send email using codeigniter.
<?php
class email_page extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$config = Array(
'protocol'=>'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'testmail#gmail.com',
'smtp_pass' => 'password'
);
$this->load->library('email', $config);
$this->email->set_newline('\r\n');
$this->email->from('testmail#gmail.com','Sender name');
$this->email->to('testmail#gmail.com');
$this->email->subject('Test mail');
$this->email->message('This is a test mail from codeigniter');
if($this->email->send()){
echo "Your mail was sent successfully!!!";
}
else{
show_error($this->email->print_debugger());
}
}
}
The page keeps on loading and does not give any result. The code looks fine to me. Are there any errors? Thanks
change your account settings here
https://www.google.com/settings/security/lesssecureapps
I myself found the answer.
$this->email->set_newline('\r\n');
Changed the above line to
$this->email->set_newline("\r\n");
I am not sure how this can happen.
i have found error, just change port to 25
function test(){
$config = Array(
'protocol'=>'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => '25',
'SMTPAuth' => true,
'smtp_user' => 'email#gmail.com',
'smtp_pass' => 'password'
);
$this->load->library('email', $config);
//$this->email->set_newline('\r\n');
$this->email->from('email#gmail.com','Sender name');
$this->email->to('email#micromerger.com');
$this->email->subject('Test mail');
$this->email->message('This is a test mail from codeigniter');
//print_r($this->email);exit;
if( ! $this->email->send()){
//print_r("234");exit;
echo "Your mail was sent successfully!!!";
}
else{
//print_r("345");exit;
show_error($this->email->print_debugger());
}
}
Related
My codeigniter script will not send emails. I have tested sendmail on xampp with a native PHP email script and it works. This is definitely a problem with CI or my config. The send() function always returns false and the print_debugger() function is always blank. Am I doing something wrong?
<?php
$config = array(
'mailtype' => 'html',
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_user' => 'myaccount#gmail.com',
'smtp_pass' => '#############',
'smtp_port' => '465'
);
$this->load->library('email', $config);
$this->email->from($this->stg['site_name']);
$this->email->to($this->input->post('email_address'));
$this->email->subject('Your Account Email Has Been Changed');
$data = array(
'activation_code' => $this->member->activation_code
);
$message = $this->load->view('emailTemplates/change_email', $data, true);
$this->email->message($message);
if(!$this->email->send()){
$this->template->overallHeader('Email Failed');
$data = array(
'title' => 'Email Sending Failed',
'message' => 'The system failed to dispatch the email message to your new email address. Because of this, the email address on your account has NOT been changed. '.$this->email->print_debugger()
);
$this->load->view('errorBody', $data);
} else {
//do stuff
}
I have also tried static strings instead of the form input variables as the to and from, etc.
I have used this several ways but can't seem to get it working. I am running it on live server.
I am using bigrock hosting and as per their document it says just use "localhost" as smtp host. Link
It says email sent but I get nothing..
public function index() {
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'localhost',
'smtp_port' => '587',
// Other way it say Email sent
//'smtp_host' => 'ssl://localhost',
//'smtp_port' => '465',
'smtp_user' => 'support#abc.com',
'smtp_pass' => 'sdfsfsdfsd'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('support#abc.com', 'ABC');
$this->email->to('abc#gmail.com');
$this->email->subject('Test');
$this->email->message('Success');
if ($this->email->send()) {
echo 'Email Sent';
} else {
show_error($this->email->print_debugger());
}
}
Thanks in advance.
This line: $this->email->to('abc#gmail'); make it 'abc#gmail.com' and make sure it is existing..If it still doesn't work, try a legit email from inside you email->from.
I want send a email by Email Class in codeigniter with gmail, but i get following error:
Error:
A PHP Error was encountered Severity: Warning Message:
mail() [function.mail]: Failed to connect to mailserver at
"ssl://smtp.googlemail.com" port 25, verify your "SMTP" and
"smtp_port" setting in php.ini or use ini_set() Filename:
libraries/Email.php Line Number: 1553
This is my full function in controll:
function send_mail(){
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'xyz#gmail.com';
$config['smtp_pass'] = 'xxxxxxx';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('neginph#gmail.com', 'Negin Phosphate Shomal');
$this->email->to('neginfos#yahoo.com');
$this->email->subject('This is an email test');
$this->email->message('It is working. Great!');
if($this->email->send())
{
echo 'Your email was sent, successfully.';
}
else
{
show_error($this->email->print_debugger());
}
}
I changed SMTP in php.ini as this:
SMTP = ssl://smtp.googlemail.com smtp_port = 25
What do i do?
With respect
this is what worked for me
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'someuser#gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"
);
$this->load->library('email', $email_config);
$this->email->from('someuser#gmail.com', 'invoice');
$this->email->to('test#test.com');
$this->email->subject('Invoice');
$this->email->message('Test');
$this->email->send();
Have you turned on php_openssl?
Try to uncomment extension=php_openssl.dll in your php.ini file.
This is working for me on localhost:
<?php
class Email extends CI_controller
{
function index()
{
$this->load->library('email');
$this->load->helper('url');
$this->load->helper('form');
$config= Array(
'protocol' =>'localhost',
'smtp_host' => 'localhost',
'smtp_port' => 'localhost',
'smtp_user'=> 'root',
'smtp_pass' =>''
);
$this->load->library('email','$config');
$this->email->set_newline("\r\n");
$this->email->from('nisha#gmail.com','nisha');
$this->email->to('cse1473#gmail.com');
$this->email->subject('this is email with subject');
$this->email->message('it s working properly');
if($this->email->send())
{
echo "your email send";
}
else
{
show_error($this->email->print_debugger());
}
}
}
?>
I use CI library for sending HTML email through smtp. When i try to send from my computer, it send right HTML. but when i send it from server, why i get HTML tag. not formatted as html ? I already set 'mailtype' to 'html'.
I wonder why it work at my local but not at server. Is there any configuration in php ? or i should send email header or something?
public function index()
{
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'tls://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxx',
'smtp_pass' => 'xxxx',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->load->library('email', $this->config->item('email'));
$this->email->set_newline("\r\n");
$this->email->from('xxxx', 'xxxx');
$this->email->to("xxxx");
$this->email->subject('Test Email');
$body = $this->load->view('welcome_message',null,TRUE);
$this->email->message($body);
if (!$this->email->send()){
echo 'fail to load email';
}
else {
echo 'success to send email';
}
}
Setting config options doesn't work unless you initialize them. Add this after your $config array:
$this->email->initialize($config);
Based on what I think you're trying to do:
$this->load->library('email', $this->config->item('email'));
should be
$this->load->library('email', $config);
Try and let us know.
I am trying to load a template into an email using Codeigniters email class, however I receive the email it is black, can anyone tell me why? Below is my code,
if($this->session->userdata('group_id') == '1') {
$data['key'] = $insertUser['activation_key'];
$data['name'] = $insertUser['name'];
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '*********',
'smtp_pass' => '********',
'mailtype' =>'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('no-reply#email.com', 'Email');
$this->email->to($insertUser['email']);
$this->email->subject('Your employers account');
$this->email->message($this->load->view('emails/signup', $data));
if (!$this->email->send())
show_error($this->email->print_debugger());
else
redirect('admin/users');
}
You need to add a third parameter to your call to $this->load->view, so the view is returned as a string:
$this->email->message($this->load->view('emails/signup', $data, true));
There is further information in the CI user guide - http://codeigniter.com/user_guide/general/views.html