CI Sending Mail using SMTP - php

I'm going straight to the point here.
I'm trying to send an mail using codeigniter library.
However, I'm not receiving any email and there are no errors shown on my code.
I don't know why it doesn't work.
I've also followed the 2 step verification and allow access to less secure apps
However when I omit the config, it send me an email which goes directly to the spam section. I don't know why it goes to spam section.
Here's my code:
public function sending_email(){
// The mail sending protocol.
$config['protocol'] = 'smtp';
// SMTP Server Address for Gmail.
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
// SMTP Port - the port that you is required
$config['smtp_port'] = '465';
// SMTP Username like. (abc#gmail.com)
$config['smtp_user'] = 'sample#gmail.com';
// SMTP Password like (abc***##)
$config['smtp_pass'] = '****';
// Load email library and passing configured values to email library
$this->load->library('email', $config);
// Sender email address
$this->email->from('sample#gmail.com', 'sample');
// Receiver email address.for single email
$this->email->to('receiver#gmail.com');
//send multiple email
//$this->email->to(abc#gmail.com,xyz#gmail.com,jkl#gmail.com);
// Subject of email
$this->email->subject('test');
// Message in email
$this->email->message('hello world!');
// It returns boolean TRUE or FALSE based on success or failure
echo $this->email->send();
}

You can try this code also if commented link's answer is not working.
$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");
// Set to, from, message, etc.
$result = $this->email->send();

Related

SMTP mailer is not working in codeignitor 3

Failed to authenticate password. Error: 535 Incorrect authentication
data Unable to send email uenter code heresing PHP SMTP. Your
server might not be configured to send mail using this method.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'mail.******.com',
'smtp_port' => 587,
'smtp_user' => 'tracker#******.com',
'smtp_pass' => '7Wj{S******KU{',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = 'Hiii';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// $this->load->library('email');
$this->email->from('tracker#******.com'); // change it to yours
$this->email->to($usremail);// change it to yours
$this->email->cc('');
// $this->email->bcc('gobinath#******.com');
$this->email->subject('hi');
$this->email->message($message);
$mailsucc = $this->email->send();
if ($mailsucc) {
echo "suc";
exit();
}else{
$de = $this->email->print_debugger();
print_r($de);
exit();
}
I've been looking for a while, and finally I get it. I hope this works for you too.
Define your timezone:
php timezone
date_default_timezone_set('America/Bogota');
Set up your email account to allow IMAP access, and allow email to be used in less secure applications
less secure apps
IMAP Access
Set up your mail server with codeigniter:
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'xxxxxxxxxx#gmail.com';
$config['smtp_pass'] = 'xxxxxxxxxxx';
$config['smtp_port'] = 465; (465/587)
$config['charset'] = 'utf-8';
$config['crlf'] = "\r\n";
$config['smtp_crypto'] = 'ssl'; (SSL/TSL)
$this->email->initialize($config);
$this->email->set_newline("\r\n");
Prepare your mail to send:
$this->email->from('xxxxxxxxxx#gmail.com', 'ANY-NAME');
$this->email->to('yyyy#yyy.yyy');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
if($this->email->send()){
echo "sent";
}
else{
echo $this->email->print_debugger();
}

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.

Sending smtp mail without smtp server installed locally

Should I be able to send SMTP email on my local development PC with CodeIgniter without having a SMTP server application installed on my local development PC?
For example, the following doesn't work.
application/config/email.php
$config['mailtype'] = 'html';
$config['protocol'] = 'smtp';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'my_gmail_account#gmail.com';
$config['smtp_pass'] = '*********';
controller code
$this->load->library('email');
$this->email->from('my_gmail_account#gmail.com', 'Noel');
$this->email->to('my_gmail_account#gmail.com');
$this->email->subject('SMTP email Test');
$this->email->message('Testing smtp email in codeigniter.');
$this->email->send();
$data['message'] = "Sorry Unable to send email...";
if($this->email->send()){
$data['message'] = "Mail sent...";
}
$this->load->view('test_view', $data);
The following returns
Sorry Unable to send email...
and doesn't arrive at the destination email.
Try loading parameters into an array and then initializing the library using array source: CodeIgniter Forum:
$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");
// Set to, from, message, etc.
$result = $this->email->send();
Also, maybe you need to enable SSL in your php.ini. If you see the line:
;extension=php_openssl.dll
uncomment it by removing semicolon from the beginning of the line and then restart PHP.

how to send email in codeigniter on localhost

hello all i have to send email on localhost
and following is my config file
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/sbin/sendmail -t -i";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xyz.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'xyz.com'; //change this
$config['smtp_pass'] = '######'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
i create code follwing code for email send
if(count($result) > 0 || $result != NULL){
$password=$result['password'];
$this->load->library('email');
$from='chirag#site.com';
$to=$email;
$subject='forget password';
$this->email->from($from);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($password);
if($this->email->send()){
$this->session->set_flashdata('email','We sent your password to your Email...!');
redirect('login');
}
else{
echo $this->email->print_debugger();
}
}
but i got following error when i send email
The following SMTP error was encountered:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
so what is problem there?
please help me to solve
First make sure you already done the proper localhost configuration for sending emails.
Then in your controller you load the email library by setting the appropriate credentials and port number. Like so:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'your_gmail_name#gmail.com',
'smtp_pass' => 'your_pass',
//'mailtype' => 'html',
// 'charset' => 'iso-8859-1',
// 'wordwrap' => TRUE
);
$this->load->library('email', $config);
i got answer
there is missing a ssl:// in config
but after writing following
$config['smtp_host'] = 'ssl://xyz.com';
mail is send successfully...

sendmail fails on CodeIgniter but works on CLI

I have sendmail installed on a CentOS 6 server that seems to work well given that I can send a test message using CLI.
However, when trying to send a test message using CodeIgniter's email class, there seems to be some problem. My email.php config is
$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
The controller for testing email:
$this->load->library('email');
$this->email->from('your#example.com', 'from name');
$this->email->to('someone#somewhere.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
When I send the test message through CodeIgniter, I get this error message
Exit status code: 71
Unable to open a socket to Sendmail. Please check settings.
Unable to send email using PHP Sendmail. Your server might not be configured to send mail using this method.
When switching to SMTP or mail in the config I still get errors.
Any suggestions where to start trying to troubleshoot this problem?
Try Something like this
$this->load->library('email');
$config['protocol']='smtp';
$config['smtp_host']='your host';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='your mail id';
$config['smtp_pass']='your password';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['mailtype'] = 'html';
$this->email->initialize($config); //intializing config array
$this->email->from('your#example.com', 'Site name');
$this->email->to('someone#somewhere.com');
$this->email->subject('Mail');
$this->email->message('Your message');
$this->email->send();
$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
);
//send email with #temp_pass as a link
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('**********#gmail.com', "Title");
$this->email->to($email);
$this->email->subject("Added");
$message = "<p>This email has been sent as a request to added</p>";
$this->email->message($message);
$this->email->send();
Try these.
Make sure apache is added to smmsp group.
id apache
If note add to group
usermod -g smmsp apache
chmod -v 2755 /usr/sbin/sendmail.sendmail
change config['mailpath'] to /use/sbin/sendmail.sendmail
If these tweeks didn't work,
chmod 777 /var/spool/clientmqueue
Its a security risk link

Categories