I tried the code below. At one time the code was working fine. I was able to send the emails. After a few minutes, when I tried it again without even changing anything, I get this error message Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
I dont know whats wrong. Lately I am facing a lot of similar bugs with codeigniter.
public function email($message = NULL, $subject=NULL, $email=NULL){
if(!isset($email)){
$to = $this->session->userdata('email');
}else{
$to = $email;
}
$this->load->library('email');
$this->email->from('noreply#mydomain.com', 'Mydomain');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
}
i face this problem and work hard and i find solution this
just little change in email config its working 100%
$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
I added the following lines and the mail is working again.
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
Related
I am in a little bit of trouble sending mail with attachments in CodeIgniter. Here is my code:
$to="mailtouser#gmail.com"
$fileUrl=array(base_url()."assets/pdf/sample.pdf", base_url()."assets/pdf/sample1.pdf");
$subject="TEST MAIL";
$message="TEST MESSAGE";
$this->load->library('email');
$from = "admin#gmail.com;
$config['wordwrap'] = TRUE;
$config['mailtype']='html';
$config['charset'] = 'utf-8';
$config['priority'] = '2';
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from($from, 'Little Bloom');
//$this->email->to();
if($cc!=''){
$this->email->cc('sendmail#gmail.com');
}
$this->email->bcc($to);
$this->email->subject($subject);
$this->email->message($message);
if(!empty($fileUrl))
{
foreach($fileUrl as $key)
{
$this->email->attach($key);
}
}
return $this->email->send();
There are multiple pdf files so I am using an array to save the URL but when I send this to my email function these files are not attached to the mail.
if you print you array $fileUrl, you'll se at once where the problem is. Using e.g.: echo '<pre>'; print_r($fileUrl);die;
the urls are missing a forward slash between your_site and assets: http://your_siteassets/pdf/sample.pdf
you can fix this either with:
$fileUrl=array(base_url("assets/pdf/sample.pdf"), base_url("assets/pdf/sample1.pdf"));
or with
$fileUrl= array(base_url()."/assets/pdf/sample.pdf", base_url()."/assets/pdf/sample1.pdf");
docs url helper base_url()
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 :(
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]
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.
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.