Send Email Codeigniter

PHP mail() function returns true, but doesn’t send mail

I know that this question was asked before. When I call the PHP mail() function, it returns true. I checked my php.ini (I’v running CentOS):

SMTP = localhost smtp_port = 25 sendmail_path = /usr/sbin/sendmail -t -i mail.add_x_header = On 

I read in a forum that I have to install sendmail. So I installed it. Now sites with a mail() function doesn’t load anymore. So I removed sendmail, and the mail() function returns true again, but doesn’t send the mail. Any idea?

Take a look here: stackoverflow.com/questions/14456673/… — it’s usually when a host filters mail being sent via the mail() function instead of using SMTP.

5 Answers 5

To send an email you need a SMTP server (local or remote). Actually your mail function just passes the mail to your SMTP server and is this one which really send your email.

In your php.ini appears this line

sendmail_path = /usr/sbin/sendmail -t -i 

You should be aware if you use that configuration parameter (from manual):

If set, smtp, smtp_port and sendmail_from are ignored and the specified command is executed.

But the most important thing here is you just uninstall sendmail so you can expect your mail goes nowhere. I know sendmail was giving you some problems, possibly configuration problems, but now your php.ini configuration is wrong.

How to solve it?

  • Start removing the sendmail_path parameter from the php.ini.
  • Install a simple to configure SMTP server like postfix.
  • Verify postfix is listening at port 22:
  • Try to send a mail from your php mail() function
  • Verify your mail has been sent correctly (check your /var/log/mail.log or /var/log/mail/mail.log files)
  • You also can verify the mail is not in the postfix queue:

Источник

Sending email from localhost

I’m using WampServer on Windows to test a site. I have a registration system where users get a confirmation email. Is it possible to send emails from a localhost?

5 Answers 5

If you want to send emails from localhost directly, you need to install a Mail Transport Agent (MTA), or if you like, a SMTP service.

IIS provides one. You can otherwise find some others on Google.

You can also change your php.ini mail settings. This won’t use localhost per say to send emails, but a relay host that will allow you to send emails from a PHP script.

Not unless you install a SMTP server on it. I think XAMPP includes one, but you’re probably better off just using an existing external server. You can set this with the php.ini SMTP setting.

I actually just set this up the other day using fake sendmail.

Читайте также:  Memcached php module install

It is actually fairly easy to set up. I found this tutorial to be helpful.

There are other alternatives out there but this is working great for me using Gmail.

I can use the mail() function in PHP without issues. One issue that I haven’t resolved yet is that the email always appears to come from my gmail address instead of whatever I supplied in the mail() arguments. I believe this may be a Gmail feature, not a sendmail issue though.

It’s quite simple. (Adapt syntax for your convenience)

public $smtp = array( 'transport' => 'Smtp', 'from' => 'your_email@gmail.com', 'host' => 'ssl://smtp.gmail.com', 'port' => 465, 'timeout' => 30, 'username' => 'your_email@gmail.com', 'password' => '*****' ) 

The easiest way I have found to create a mail sever in windows is to install Python and then use the inbuilt SMTP debug server with the following one-liner:

python -m smtpd -c DebuggingServer -n localhost:25 

On Windows, I have found that it sometimes has strange behaviour with localhost and 127.0.0.1 so, if all else fails, you could trying finding your IP address using the ‘ipconfig’ command (at the DOS command prompt) and you might see something like:

c:\> ipconfig -- stuff cut here --- Wireless LAN adapter WiFi: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::856d:fd33:3e01:e656%9 IPv4 Address. . . . . . . . . . . : 192.168.8.111 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.8.1 

and, let’s say you find you ip address is 192.168.8.11 (as I did in the example above), then changing the command to look like:

python -m smtpd -c DebuggingServer -n 192.168.8.11:25 

And setting the following lines in your php.ini file:

[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = 192.168.8.111 ; http://php.net/smtp-port smtp_port = 25 

And, this has worked for me on occasions where nothing else does!

I also note you can check if you local mail server is working by typing the command:

or for my second configuration:

Telnet comes for free on Linux but to get it on windows you have to go to «Programs and Features» and then click «Turn Windows feaures on and off» (from the little panel on the left) and then check the box for «Telnet Client» in the list of Windows Features.

When you run the telnet command you will see something like this if the server is not running:

C:\Projects>telnet localhost 25 Connecting To localhost. Could not open connection to the host, on port 25: Connect failed 

But, if it is all working you should get the screen cleared and a «server identifier» string back from the mail server. On my machine this looked like:

220 ZOOT Python SMTP proxy version 0.3 

If you are really keen you can type some SMTP commands and send a mail here or you can type ‘quit’ to disconnect from the server.

Читайте также:  Gradient colors for html

Источник

Unable to send mail via php mail()

I am unable to send an email via php’s mail function. This is the error I receive. Warning: mail() [function.mail]: Failed to connect to mailserver at «localhost» port 25, verify your «SMTP» and «smtp_port» setting in php.ini or use ini_set() in D:\inetpub\vhosts\southernbatteries.com\httpdocs\includes\utils.php on line 6 Array ( [0] => [1] => Could not send mail, please try later. ) What exactly does this mean ? Does it mean there is not mail server installed on my webserver ? Is it possible to use a different mail server. E.g Gmails mail server to send the mail ?

5 Answers 5

If you want to use built-in mail() , you will need to talk to the server administrator. It is possible to use an external SMTP server on Windows, but it has to be specified in the php.ini settings, and PHP’s built-in functions don’t support SMTP authentication.

The usual way is to either use a local mail server that in turn can talk to a «real» SMTP server with authentication, or use a mailing package like SwiftMailer that can connect to a 3rd party SMTP server like GMail directly.

Thing is, I am trying to help out a friend and they don’t know the details of the hosting service. If I were to upload the SwiftMailer files to the webserver, will I be able to send Mails using that ?

@Prakash yes, it will work independently from mail() . If the server is not misconfigured, you should be able to work with it. See swiftmailer.org/wikidocs/v3/connections/smtp for a GMail example

According to the example it connects to localhost:25. And according to the error my page throws, 25 might not be open «Failed to connect to mailserver at «localhost» port 25, verify your «SMTP» and «smtp_port» » This is a section phpinfo() SMTP localhost localhost smtp_port 25 25 Do things look ok ?

@Prakash those are separate examples (it’s arguably a bit confusing). Use the one prefixed //Connect to Gmail (PHP5)

@Pekka: I have a doubt. Does SwiftMailer require my server to have a mail server running ? Or is it completely independent of it ?

Источник

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

Make sure $config[‘newline’] = «\r\n»; is in double quotes not single quotes.

Thank you! that is so strange. why port 465 doenst work on codeigniter? o.O, Ive always used Swift-4.* php to send emails without any problem using 465 port.

For me, changing $config[‘newline’] = «\r\n» to $config[‘newline’] = «\r» works though. Before this is «\r\n», but this cause the email subject to show some weird encoding content.

$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

Читайте также:  Character style in html

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:

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; 

Actually what I found is by changing it to mail, the emails are sent from the server mail itself. So this doesn’t work. You can check the sender details of the email you receive.

This is not right. What you are doing, you are telling the server to sent mail through mail service only not by SMTP. The question asked is regarding the SMTP.

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

     session->flashdata('email_sent'); echo form_open('/Sendingemail_Controller/send_mail'); ?>  

if some error comes again please visit official documentation below:

Источник

Оцените статью