Codeigniter $this->email->send() not working while mail() does - php

I can't figure out why if I try to use the CI Email Class it doesn't send emails, while if I use the native PHP mail() Class works.
Has to be noted that sometimes I get "email sent" while is not actually sent and sometimes I get the error "my server is not setup".
I tried to figure out how to set it up but... nothing...
Controller code follows:
if($this->form_validation->run()){
//Set Language
$this->lang->load('site', $this->session->userdata('lang'));
//Random key
$user_valid_key = md5(uniqid());
//Prepare email
$this->load->library('email', array('mailtype' => 'html'));
$this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
$this->email->to($this->input->post('email'));
$this->email->subject($this->lang->line('email_signup_subject'));
//Format mail con %s per inserire i campi necessari
$signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);
$this->email->message((string)$signup_msg);
if($this->email->send()){
//TODO: load view...
echo "email sent";
}else{
$to = $this->input->post('email');
mail($to, 'test', 'Other sent option failed');
echo $this->input->post('email');
show_error($this->email->print_debugger());
}
//TODO: Add to db
}else{
// Form validation failed
}

Use this setup email..
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'sender_mailid#gmail.com';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('sender_mailid#gmail.com', 'sender_name');
$this->email->to('recipient#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();

I faced this problem and found the following solution. Just a little change in the email config and it's working 100%:
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';

Codeigniter User Guide: https://www.codeigniter.com/user_guide/libraries/email.html
This setup works for me:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'Your SMTP Server',
'smtp_port' => 25,
'smtp_user' => 'Your SMTP User',
'smtp_pass' => 'Your SMTP Pass',
'mailtype' => 'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);
$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
if($this->email->send()){
//Success email Sent
echo $this->email->print_debugger();
}else{
//Email Failed To Send
echo $this->email->print_debugger();
}

I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)
But when run this Below code in my server it works for me.
application>controller>Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "my#gmail.com"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
and my view page where I defined f_email and email comes through post method.
application>view>emailtesting.php
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
if some error comes again please visit official documentation below:
https://codeigniter.com/user_guide/libraries/email.html

After fighting with this same problem for a couple hours I finally decided to change my config to send through a different server. My original server for some reason would send to some addresses but not others (in same domain). As soon as I changed to sendgrid it worked as expected.
If you are not getting the results you expect, try a different smtp server. The problem may not be your code...

I had the same problem and I try below code instead of Codeignitor's mail function.
mail('mypersonalmail#domainserver.com' , 'Test', 'Test Email');
It works and mail is send to the email address. That mail has sent by already created email address (As i think). In my case it is:
gtt28651ff#p3plcpnl0552.srod.ahx3.domainserver.com
So I copy this email address and try it with below code.
$this->email->from('gtt28651ff#p3plcpnl0552.srod.ahx3.domainserver.com', 'www.domainserver.com');
And it work fine. It seems to be some servers need already created email address to send the email while others are NOT.
Hope this is clear and helpful.

Lately, sending emails through Google SMTP can be very tricky due to their security checks.
Verify if:
you have the rDNS on your server and it's matching your server IP
disable the 2-step auth on the email you are using to send emails from (on Google settings)
have IMAP and POP enabled on the email you are using to send emails from (on Google settings)
allow less secure apps on the email you are using to send emails from (on Google settings)
Change the CI settings as follows:
//Google settings for CI
$config['protocol'] = 'ssmtp'; //smtp not working
$config['smtp_host'] = 'ssl://ssmtp.gmail.com'; //notice ssmtp
$config['smtp_user'] = 'anyemail#server.com'; //must use Google
$config['smtp_pass'] = '***********';
$config['smtp_port'] = '465';
$config['smtp_crypto'] = 'ssl';
//general settings
$config['_smtp_auth'] = TRUE; //important
$config['smtp_timeout'] = 30;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
//optional
$config['wrapchars'] = 76;
$config['wordwrap'] = TRUE;
I hope this will save you the 2 days I have struggled with this issue.

I've spent hours trying to change the port,enabling the ssl extension in php.ini file etc nothing worked only ended up in a message SMTP server is not configured properly.I use WAMP as a localhost,when i turn off my antivirus avast the same config worked!!! the was send successfully.so it may be your antivirus program blocking on a local machine.

Related

Codeigniter 3 applcation bug: unable to send valid link via email message

I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4.
I have added a registration and login system to this application. I am current working on a password reset system.
I was able to do these 2 things separately:
Send a password reset email containing dummy text.
Create a valid password reset link.
I was unable however, to send the email once the reset link was inserted into the email body.
Here is he controller:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
private $sender_email = "noreply#yourdomain.com";
private $sender_name = "Razvan Zamfir";
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_token = '';
private $reset_url = '';
private $reset_link = '';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
//Get user email
$this->user_email = $this->input->post('email');
//create token
$this->reset_token = md5(str_shuffle($this->user_email));
//create url
$this->reset_url = base_url('changepasword/') . $this->user_email . '/'. $this->reset_token;
//create reset link
$this->reset_link = 'password reset link';
echo $this->reset_link;die();
$this->body = "Here is your $this->reset_link. \n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
// Loading the Email library
$config['protocol'] = 'sendmail';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
if(!$this->load->is_loaded('email')){
$this->load->library('email', $config);
} else {
$this->email->initialize($config);
}
// Build the body and meta data of the email message
$this->email->from($this->sender_email, $this->sender_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if($this->email->send()){
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else{
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}
I have inspected the link, it is valid and the value of the href attribute is as intended, but once I remove echo $this->reset_link;die() I see the attempt to send the email failing:
Where is my mistake?
The email not sending issue can be from multiple reasons:
Codeigniter 3 email library is not configured correctly
test: use built in email() in php and test if that's working
php not configured correctly to send email
server is not configured for handing over emails
DNS has external mx records and local email is not forwarded out from the server
test: use external SMTP server (free gmail is fine) and configure like this the ci3 library https://forum.codeigniter.com/thread-75525-post-371979.html#pid371979
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'your_email';
$config['smtp_pass'] = 'your_password';
$config['smtp_port'] = 465;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "\r\n";
if this is not working, check what's the cause by using
if ($this->email->send(FALSE))
{
echo $this->email->print_debugger();
}
server cannot access remote ports firewalls are not opened for this
test: disable firewall or selinux and test again
Are you running it locally? If so this might be the cause of the error. Can't send emails from local machines. If not check with your hosting company if it allows you to send mails via php. Most shared hostings disable this option and sell SMTP service to allow you to send emails
Have you tried loading a view into your email function? Then you could pass the content to that file and send a html email
This is what woked for me:
public function sendResetMail()
{
// Email settings
$config['protocol'] = 'sendmail';
$config['smtp_host'] = 'mail.mydomain.com';
$config['smtp_user'] = 'myemail#mydomain.com';
$config['smtp_pass'] = '******';
$config['smtp_port'] = 465;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "\r\n";
if (!$this->load->is_loaded('email')) {
$this->load->library('email', $config);
} else {
$this->email->initialize($config);
}
// Build the body and meta data of the email message
$this->email->from($this->sender_email, $this->sender_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if ($this->email->send()) {
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else {
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}

Getting not configured error while sending email in php-codeigniter

Please see my email sending code below and it is showing the following error while sending the email..
Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
My Controller:
$from_email= "******";
$to_email = 'dummy#dummy.com';
$subject='Password Reset Request';
$htmlContent = '<p>'.$link.'</p>';
$this->load->library('email');
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from($from_email,'*****');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($htmlContent);
$this->email->send();
This condition is showing the above error. Actually, it was working fine initially in development stage, but now it is not..What could have gone wrong in this query...Thank you in advance..
you're using $this->email->initialize($config); but there is no actual configs on your code. And as per your error Unable to send email using PHP mail(), your server can run through sendmail.
Email Class in codeingiter
Example
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
To check outgoing mail with this on server
telnet google.com 25 # 587 or 2095 ,2096
Most server outgoing ports will be 25 or 587
if you are in localhost the code work, are you in live server ?
if your URL is siteurl.domain then try ,
$from_email= "email#siteurl.domain"; //from email must be from same server url
$to_email = 'dummy#dummy.com';
$subject='Password Reset Request';
$htmlContent = '<p>'.$link.'</p>';
//{INCLUDE SMTP CONFIG HERE IF THIS NOT WORKiNG THIS METHOD}
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->email->initialize($config);
$this->email->from('$from_email', 'sender name');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($htmlContent);
$this->email->send();
else try with SMTP:),
//SMTP CONFIG
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //smtp host name
$config['smtp_port'] = '465'; //smtp port number
$config['smtp_user'] = 'ji******#gmail.com';
$config['smtp_pass'] = '*******'; //$from_email password
//Continue above code
I'm not able to comment(sorry) :),
sometimes SMTP method not working with Bluehost account :(

Codeigniter Email Issue : Your server might not be configured to send mail using this method

When sending emails using codeigniter, the emails are received but the page results in an error.
https://privatebin.net/?d89c7965b55f5712#+Y3hf+PE36m8bROxB/k4llwmcvOykt2JF+m72HhTLGU=
This is the error displayed when trying to send any email from script.
The code used for sending email is
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.domain.com';
$config['smtp_port'] = '587';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'mail#domain.com';
$config['smtp_pass'] = '******';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'html'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
//$this->email->initialize($config);
$this->email->from('mail#domain.com', $system_name);
$this->email->to($email);
$this->email->subject('Title');
$this->email->message($message);
$send=$this->email->send();
This was the response from hosting provider:
We have implemented anti-spoofing policies on our server to curb spoofing, hence you won't be able to use the mail function if the FROM/TO email address is not from the same domain.
I suggest you the following solution to resolve the issue.
Change the FROM address as something#domain.com.
Use SMTP authentication to send emails. [Recommended solution]

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method

Iam using codeigniter
I exicuted the code on live server.
got the following error using print_debugger()
Unable to send email using PHP SMTP. Your server might not be
configured to send mail using this method.
public function sendEnquiry() {
$this->load->library('email');
$name = $this->input->post("fname");
$cemail = $this->input->post("email");
$pno = $this->input->post("phone");
$message = $this->input->post("message");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://mail.gatewaykhobar.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = '***********';
$config['smtp_pass'] = '***********';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = FALSE;
$this->email->initialize($config);
$this->email->from('info#gatewaykhobar.com','Gateway Restaurent Contact');
$this->email->to($cemail);
$this->email->subject('Gateway Restaurent Contact Enquiry');
$this->email->message($message);
$send = $this->email->send();
if($send) {
echo json_encode("send");
} else {
$error = $this->email->print_debugger(array('headers'));
echo json_encode($error);
}
}
Change smtp_port from 465 to 587.
Make sure $config['newline'] = "\r\n"; is in double quotes not single quotes.
$mail_config['smtp_host'] = 'smtp.gmail.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'user#example.com';
$mail_config['_smtp_auth'] = TRUE;
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls';
$mail_config['protocol'] = 'smtp';
$mail_config['mailtype'] = 'html';
$mail_config['send_multipart'] = FALSE;
$mail_config['charset'] = 'utf-8';
$mail_config['wordwrap'] = TRUE;
$this->email->initialize($mail_config);
$this->email->set_newline("\r\n");
I just added the last line
A common cause of this is the way that CodeIgniter interacts with the SMTP server with regards to line breaks. Your SMTP server might require \r\n and CodeIgniter is using \n.
There is an easy fix: after your $this->email->initialize(), add the following:
$this->email->set_newline("\r\n");
That should get it working for you.
Just use "mail" for the 'protocol' array item, and that's all...
$config = array();
$config['useragent'] = $system_name;
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "mail"; //use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "your domain name";
$config['smtp_user'] = $from;
$config['smtp_pass'] = "*************";
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "utf-8";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
It looks like the mail server is hosted by yourself as well, try sending email from any e-mail client. If it fails - there's a problem with your mailserver config, not the code you pasted - check the server logs.
I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)
But when run this Below code in my server it works for me.
application>controller>Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "my#gmail.com"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
and my view page where I defined f_email and email comes through post method.
application>view>emailtesting.php
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
if some error comes again please visit official documentation below:
https://codeigniter.com/user_guide/libraries/email.html
For anyone else who finds this error, has set the settings mentioned elsewhere (even Codeigniter 4) but still getting tht error, one way to test what is going on is from the server console and using telnet. For example:
telnet smtp.yourprovider.com 587
Then test against a different provider and see if that works. If your provider doesn't but another does then the problem is with your provider and you should contact them. If both can't connect then you should speak with your webhost.
I've found another solution. I had the same problem: (Codeigniter 4 and the issue with php smtp), and the reason was 2-step verification of my gmail account, from which I wanted to send emails in my app. I fixed it by getting from my gmail account, password for my app. It works, at last.

Sending email with gmail smtp with codeigniter email library

<?php
class Email extends Controller {
function Email()
{
parent::Controller();
$this->load->library('email');
}
function index()
{
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'mygmail#gmail.com';
$config['smtp_pass'] = '*******';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('mygmail#gmail.com', 'myname');
$this->email->to('target#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
$this->load->view('email_view');
}
}
I am getting this error:
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Connection timed out)
Filename: libraries/Email.php
Line Number: 1641
Using PORT 25/587
I got this error:
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:func(119):reason(252)
Filename: libraries/Email.php
Line Number: 1641
I don't want to use phpmailer now. (Actually I have tried to use phpmailer, but I failed).
How do I solve this problem guys?
$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();
From the CodeIgniter Forums
According to the CI docs (CodeIgniter Email Library)...
If you prefer not to set preferences using the above method, you can
instead put them into a config file. Simply create a new file called
the email.php, add the $config array in that file. Then save the file
at config/email.php and it will be used automatically. You will NOT
need to use the $this->email->initialize() function if you save your
preferences in a config file.
I was able to get this to work by putting all the settings into application/config/email.php.
$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';
//$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'YOUREMAILHERE#gmail.com';
$config['smtp_pass'] = 'YOURPASSWORDHERE';
$config['smtp_port'] = 465;
$config['smtp_timeout'] = 5;
$config['wordwrap'] = TRUE;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['validate'] = FALSE;
$config['priority'] = 3;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['bcc_batch_mode'] = FALSE;
$config['bcc_batch_size'] = 200;
Then, in one of the controller methods I have something like:
$this->load->library('email'); // Note: no $config param needed
$this->email->from('YOUREMAILHERE#gmail.com', 'YOUREMAILHERE#gmail.com');
$this->email->to('SOMEEMAILHERE#gmail.com');
$this->email->subject('Test email from CI and Gmail');
$this->email->message('This is a test.');
$this->email->send();
Also, as Cerebro wrote, I had to uncomment out this line in my php.ini file and restart PHP:
extension=php_openssl.dll
You need to enable SSL in your PHP config. Load up php.ini and find a line with the following:
;extension=php_openssl.dll
Uncomment it. :D
(by removing the semicolon from the statement)
extension=php_openssl.dll
Change it to the following:
$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "blablabla#gmail.com";
$config['smtp_pass'] = "yourpassword";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('blablabla#gmail.com', 'Blabla');
$list = array('xxx#gmail.com');
$ci->email->to($list);
$this->email->reply_to('my-email#gmail.com', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->send();
send html email via codeiginater
$this->load->library('email');
$this->load->library('parser');
$this->email->clear();
$config['mailtype'] = "html";
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('email#example.com', 'Website');
$list = array('xxxxxxxx#archmage.lk', 'xxxxx#gmail.com');
$this->email->to($list);
$data = array();
$htmlMessage = $this->parser->parse('messages/email', $data, true);
$this->email->subject('This is an email test');
$this->email->message($htmlMessage);
if ($this->email->send()) {
echo 'Your email was sent, thanks chamil.';
} else {
show_error($this->email->print_debugger());
}
Another option I have working, in a linux server with Postfix:
First, configure CI email to use your server's email system: eg, in email.php, for example
# alias to postfix in a typical Postfix server
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
Then configure your postfix to relay the mail to google (perhaps depending on the sender address). You'll probably need to put you user-password settings in /etc/postfix/sasl_passwd
(docs)
This is much simpler (and less fragmente) if you have a linux box, already configured to send some/all of its outgoing emails to Google.
Perhaps your hosting server and email server are located at same place and you don't need to go for smtp authentication. Just keep every thing default like:
$config = array(
'protocol' => '',
'smtp_host' => '',
'smtp_port' => '',
'smtp_user' => 'yourname#gmail.com',
'smtp_pass' => '**********'
);
or
$config['protocol'] = '';
$config['smtp_host'] = '';
$config['smtp_port'] = ;
$config['smtp_user'] = 'yourname#gmail.com';
$config['smtp_pass'] = 'password';
it works for me.
It can be this:
If you are using cpanel for your website smtp restrictions are problem
and cause this error. SMTP Restrictions
Error while sending an email with CodeIgniter
There is very simple answer to this . and the only solution that worked at the en was this configuration ..i only had to chnage the port to 587 and overwrite the codeignitor default "Email.php"
$config = array( 'protocol' => 'smtp', 'smtp_host' => 'smtp.gmail.com', 'smtp_port' => 587, 'smtp_user' => 'admin#cafeadmin.com.au', 'smtp_pass' => '1800#Footscray123!' );$this->load->library('email', $config);$this->email->initialize($config);

Categories