CodeIgniter + MAMP : unable to send emails - php

I am trying to send emails with CI running on MAMP free. But it doesn't work, my script is encountering an infinite loop and nothing is happening...
Do i need to set up something especially to send emails from localhost?
Here is my email config for CI:
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = '*****#gmail.com';
$config['smtp_pass'] = '******';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = '\r\n';
Cheers
EDIT: Here is my code to send an email:
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = '****';
$config['smtp_pass'] = '*****';
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = '\r\n';
$this->email->initialize($config);
$this->email->from('****');
$this->email->to($email);
$this->email->subject($title);
$this->email->message($content);
$this->email->send();
error_log($this->email->print_debugger());
With $title, $content and $email vars defined in another part of my script. Don't worry about this stuff, I've already checked that my issue is not due to these.

Finally I found the solution:
Send emails with MAMP (mail() PHP function)
Set up SSL: http://soundsplausible.com/2012/01/14/enable-ssl-in-mamp-2-0-5/
Set up Postfix: http://benjaminrojas.net/configuring-postfix-to-send-mail-from-mac-os-x-mountain-lion/
Into MAMP's php.ini (glance phpinfo() to know which version is used and conclude into which folder you need to edit), comment lines SMTP, smtp_port and sendmail_from. Uncomment line sendmail_path and set /usr/sbin/sendmail -t -i as new value.
You should be able to send emails now, if PostFix works fine (run test given in tutorial above).
Send emails with CI
To send emails with CI, you don't need to write your logins into a file for PostFix. However, you need to be able to run PostFix and SSL.
Here is an example of config file for a google account:
$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.gmail.com";
$config['smtp_port'] = "587";
$config['smtp_user'] = "*****";
$config['smtp_pass'] = "*****";
$config['smtp_crypto'] = "tls"; //very important line, don't remove it
$config['smtp_timeout'] = "5"; //google hint
$config['mailtype'] = "text";
$config['charset'] = "utf-8";
$config['newline'] = "\r\n";
Be careful with " " which are necessary, ' ' could create issues. Here I'm using a TLS connection. If you prefer SSL, use port 465 and fix correctly smtp_crypto value.

You need use sendmail(details how install it on MAMP). Or you can use solution below to store emails in localhost(something like emulate email sending).
I'm use this solution on my localhost(XAMPP). Maybe it will be helpful for you.
Specify path for send mail in php.ini
sendmail_path = "path/to/php path/to/sendmail.php"
The second step - you can try to use this script
define('DIR','path/to/sendmail_dir');
$stream = '';
$fp = fopen('php://stdin','r');
while($t = fread($fp,2048)){
if($t === chr(0)){
break;
}
$stream .= $t;
}
fclose($fp);
$fp = fopen(mkname(),'w');
fwrite($fp,$stream);
fclose($fp);
function mkname($i=0){
$fn = DIR.date('Y-m-d_H-i-s_').$i.'.eml';
if (file_exists($fn)){
return mkname(++$i);
}
else{
return $fn;
}
}

Related

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

Unable to send email using PHP Sendmail. Your server might not be configured to send mail using this method.
// config content
$protocol = 'sendmail';
$mailpath = '/usr/sbin/sendmail';
$charset = 'iso-8859-1';
$wordwrap = TRUE;
$mailtype= "html";
$smtp_host= "mail.domain.com";//pengaturan smtp
$smtp_port= "465";
$smtp_timeout= "25";
$user = "";
$pass = "";
// config array
$config = array();
$config['protocol'] = $protocol;
$config['mailpath'] = $mailpath;
$config['charset'] = $charset;
$config['wordwrap'] = $wordwrap;
$config['mailtype'] = $mailtype;
$config['smtp_host'] = $smtp_host;
$config['smtp_port'] = $smtp_port;
$config['smtp_timeout'] = $smtp_timeout;
$config['smtp_user'] = $user;
$config['smtp_pass'] = $pass;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['send_multipart'] = FALSE;
// send
$this->email->initialize($config);
$this->email->from($config['smtp_user'], 'Info');
$this->email->to($query['email']);
$this->email->subject("Reset password berhasil");
$this->email->message("adadad");
if ($this->email->send()) {
echo "berhasil";
}
Well sendmail allows you to send email using the sendmail program installed on your local server. Do you have sendmail installed on your server.
Also you have specified SMTP connection settings in your config file. Maybe you dont need sendmail. Try to change the protocol field from sendmail to smtp.

Codeigniter Amazon SES SMTP Connect Error

I want to send email using Amazon SES in Codeigniter, i have tried everything, i also tried the earlier questions asked on codeigniter too, but i am not able to find the solution for this.
public function add_applicants()
{
$this->load->model('visa/India_model');
$datan = $this->India_model->insertappdata();
$appid = $datan[appid];
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['charset'] = 'ISO-8859-1';
$config['mailtype'] = 'html';
$config['smtp_host'] = "ssl://email-smtp.eu-west-1.amazonaws.com";
$config['smtp_user'] = "SMTP USER SES";
$config['smtp_pass'] = "SMTP Password SES";
$config['smtp_port'] = "465";
$config['newline'] = "\r\n";
$config['mailsender'] = "support#indiaevisaservice.com";
$config['smtp_crypto'] = 'tls';
$this->email->initialize($config);
$this->email->print_debugger();
$this->email->from('support#verfiedemail.com', 'GYEV Experts');
$this->email->to('imthegrv#gmail.com', 'Test To');
$this->email->subject('Test');
$this->email->message('test');
if($this->email->send()):
print_r($appid);
else:
print_r($this->email->print_debugger());
endif;
}
i am getting error
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
You didn't follow the link provided in the error message, which provides tests that you can use to diagnose your problem.
These three lines are inconsistent:
$config['smtp_host'] = "ssl://email-smtp.eu-west-1.amazonaws.com";
$config['smtp_port'] = "465";
$config['smtp_crypto'] = 'tls';
tls mode will not work on port 465. That should be ssl. Alternatively, change port to 587, and it should be an integer, not a string. There's also no need for the ssl:// prefix on your host; that just adds confusion. First step, change them to this:
$config['smtp_host'] = 'email-smtp.eu-west-1.amazonaws.com';
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
You may still have other issues (e.g. your ISP may block outbound SMTP connections), but there are many other tests listed in the guide that will help you diagnose that.

Codeigniter - Sending emails using different SMTP settings

I have a system that uses different emails for different parts of the system. I've got my config setup with a No-Reply email. However, if I try and send an email using different SMTP settings, it will try and use the Email config settings rather than the array settings I've specified.
These are the settings that are in my Email config file
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'REMOVED';
$config['smtp_pass'] = 'REMOVED';
$config['smtp_port'] = '587';
$config['mailtype'] = 'html';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = '120';
$config['smtp_crypto'] = 'tls';
Below is the list of settings I have specified in the array (These are the settings that I want to send this particular email through Gmail rather than Office365)
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'REMOVED';
$config['smtp_pass'] = 'REMOVED';
$config['smtp_port'] = '587';
$config['mailtype'] = 'html';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = '120';
$config['smtp_crypto'] = 'tls';
Is there any way to send emails via different SMTP settings rather than the ones specified in the Email config file? I have tried to send this email via the Gmail settings but it always appears to try and send through the Email config settings.
SOLUTION:
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = 'REMOVED';
$config['smtp_pass'] = 'REMOVED';
$config['smtp_port'] = '587';
$config['mailtype'] = 'html';
$config['charset'] = 'utf8';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
$config['crlf'] = "\r\n";
$config['smtp_timeout'] = '120';
$config['smtp_crypto'] = 'tls';
$this->email->initialize($config);
Fixed my issue and now I am able to send an email through an Ad-Hoc Gmail account rather than the Office365 one.
You can override the configuration in the config/Email.php (office365 in your case) file with a specific one (gmail, in your case) by building the $config array in the controller and then use it to initialize the email library with:
$this->email->initialize($config);
For this to work you must initialize the library using the overriding $config before actually doing anything else that requires the class, or the default settings will be used automatically

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.

Error when calling send method in Codeigniter email library

Edited:
Im trying to send an email using codeigniter's library. Whenever I call the $this->email->send() function it returns
Warning: include(C:\xampp\htdocs\psems\application\views\errors\html\error_php.php): failed to open stream: No such file or directory in C:\xampp\htdocs\psems\system\core\Exceptions.php on line 269
Warning: include(): Failed opening
'C:\xampp\htdocs\psems\application\views\errors\html\error_php.php'
for inclusion (include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\psems\system\core\Exceptions.php on line 269
This is the code I have so far
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "smtp";
$config['smtp_host'] = "localhost";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('example#gmail.com', 'harold decapia');
$this->email->to('example#gmail.com');
$this->email->subject('Тест Email');
$this->email->message('Hello World');
$this->email->send(); //I tried commenting this out and there was no error returned
I'm confused as to why it returns an error just by calling the send method. Do I have to configure something first? I'm thinking I'm missing a tiny bit of info here :(
Also, is there any way for me to know what error it returns specifically? Thank you in advance
The error as far as I can see it's coming because you're trying to send an email with SMTP on a server without and actual FTP server (Happens a lot with local servers).
I'm going to suggest a free service like Mailgun to use as an SMTP server while you're developing.
Also if you're going to use SMTP protocol, you'll need to use an User and Password to authenticate on the SMTP server (Mailgun will provide you with that too).
Here it's a sample of the new configurations for the SMTP server/authentication, remember you need to adjust to the information the Mailgun website will give you on your account.
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.mailgun.org';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '30';
$config['smtp_user'] = 'postmaster#mydomain.com';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
:)

Categories