I have the following code for sending email using codeigniter email library using SMTP using office365 SMTP
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'xxxxxx#example.com';
$config['smtp_pass'] = '123456';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['smtp_port'] = 587;
$config['smtp_timeout'] = 60;
$config['smtp_crypto'] = 'tls';
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('xxxxxx#example.com');
$this->email->to('test123#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->set_mailtype('html');
$this->email->set_newline("\r\n");
$this->email->send();
And it shows an error
Failed to send AUTH LOGIN command. Error: 504 5.7.4 Unrecognized
authentication type [SG2PR0601CA0010.apcprd06.prod.outlook.com]
Unable to send email using PHP SMTP. Your server might not be
configured to send mail using this method.
In office365,shows configuration like bellow
NOTE: It is using office365 SMTP not gmail
Just use this line of code in your config file code:
$config['protocol'] = 'sendmail';
instead of
$config['protocol'] = 'smtp';
Hopefully, it will solve the error.
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 just set up google mx records for my domain. When i use my normal gmail account, emails are sent but when I use my gsuite account, the mails are not sent. I am hosting my site on siteground. However I can receive emails from other email accounts on my gsuite account.
$this->load->library('email');
$subject = 'Subject';
$message = 'My message';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465
$config['smtp_user'] = 'info#mydomain.com';
$config['smtp_pass'] = 'my_password';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$config['validation'] = TRUE;
$this->email->initialize($config);
//send mail
$this->email->from('info#mydomain.com', 'MyDomainName');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
//echo json_encode($this->email->print_debugger());
if($this->email->send())
return true;
else
return false;
You didn't attach any error message, but since it's working for your other Gmail accounts, try checking if turning on access to "less secure apps" for this account solves this issue and report back.
https://myaccount.google.com/lesssecureapps
(Don't forget to choose the right account, if you're logged in to several ones).
I had similar problem, I had to create a App Password to work (details here: https://support.google.com/accounts/answer/185833?hl=en)
Once the App Password was created I used:
$subject = 'Your subject';
$message = 'Your body';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'your#email.com';
$config['smtp_pass'] = 'your app password';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$config['validation'] = TRUE;
$this->email->initialize($config);
//send mail
$this->email->from('from#email', 'Title');
$this->email->to('to#email.com');
$this->email->subject($subject);
$this->email->message($message);
I am trying to send email from my server with gmail smtp. I am using following code:
$mesaj=$this->load->view('main/mail.php',$token,true);
$config['protocol'] = 'smtp';
$config['wordwrap'] = FALSE;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'info#site.com';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = '465';
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('info#site.com');
$this->email->to($email);
$this->email->subject('Subject');
$this->email->message($mesaj);
$this->email->send();
echo $this->email->print_debugger();
But I am getting following error:
SMTP ERROR OCCURED. AUTH LOGIN COMMAND COULD NOT SEND
I have no SSL certificate on my server. Is this could be the problem ? How can I resolve this problem ?
The port 465 is reserved for SSL requests on the smtp.gmail.com server.
It doesn't look like you specified the authorization method here (none / ssl / tsl).
I'm not too familiar with PHP, so I don't know exactly how you'd set it...