Why does this PHP error reference localhost? - php

I'm running XAMPP on my local machine and on a server in the office. Both are Windows machines.
I'm writing some code that uses mail() to send email from a form. By default, it uses sendmail.exe (which comes with XAMPP) to send the email. In all cases, the mail is actually sent via a third machine, which is the Exchange server.
From my local machine, PHP can send mail just fine. On the server, upon form submission I get this error:
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
... followed by my filename.
I don't understand why it's referencing "localhost." Nowhere in php.ini or sendmail.ini does is "localhost" used - I use the name of the mail server. The SMTP information used on both machines is the same.
As far as I can tell, the two environments have everything important in common:
The php.ini files are identical
The sendmail.ini files are identical
Both machines have the same version of XAMPP installed
The same batch script will run on both machines and successfully send email via sendmail.exe
I have stopped and started Apache several times to make sure it's using the updated config files.
When I get the error above, I notice that no log file is produced by sendmail.exe, which makes me think it's never run.
What am I missing?
Solved
My problem was that I thought it was using c:\xampp\php\php.ini, but it was actually using c:\xampp\apache\bin\php.ini. This should have been obvious, and I had previously edited the correct file on my local machine, but somehow I got confused when making the changes on the server.
Using php_info() showed me which config file was loaded, and I edited the correct one. It's working now! Thanks everyone for your help.

You should add a call to phpinfo() in your page, and check that:
Your PHP script is using the correct php.ini
Check that the SMTP ini settings (as displayed in the phpinfo tables) are correct.

Try to use this in the code on server:
ini_set("SMTP","smtp.example.com" );
ini_set('sendmail_from', 'user#example.com');

I had to do this also - you need to sent up the sendmail.ini:
Your sendmail.ini should be located in C:\xampp\sendmail\sendmail.ini.
You only need to be concern with 3 variables here:
1.smtp_server
2.auth_username
3.auth_password
Details are here: Send mail and xampp
Bill H

Related

PHP send e-mail

Okay, so I'm new to php, and so I searched this site to find out how to send an e-mail to myself after a user fills out a contact form. I found this answer.
Send email with PHP from html form on submit with the same script
I'm using the code from the selected answer. I tried the top answer, by combining both sections into a single php page, and the second, using an html and php page, but I get the following errors:
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 (MY WEBSITE) on line 14
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 (MY WEBSITE) on line 15
"MY WEBSITE" was put there by me.
Lines 14 and 15 are these:
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
Why am I getting this issue? I'm doing exactly what the poster said to do.
You are running PHP on Windows. On Unix-like systems, PHP is able to use the built-in sendmail command to send emails, and so needs no further configuration. On Windows, however, that command doesn't exist, so PHP sends email via an external SMTP server.
In order to send emails, as the error messages explain, you will need to configure your php.ini file or use ini_set() to set the SMTP and smtp_port ini settings. You'll want to specify a SMTP server and smtp_port (usually 25) that you can send email from. If you are running php from your home computer, this would probably be whatever mail server your ISP gave you. If you are are on a third-party hosting provider, you would have to get email server information from them (and they may not actually allow sending email, or may put tight controls on how much you can send, to prevent spam).
The location of the ini file is set by both configuration and convention. PHP's configuration file documentation. On Windows, a long list of registry keys is checked, before eventually falling back to C:\windows or C:\winnt, so that is where you should look for your ini file. (Running php --ini will also show where it's actually trying to read the ini file from.)
There is a sample configuration file shown in the configuration file documentation. If you don't already have a php.ini that was installed when you installed PHP, you can use that format to guide you in creating a new file.
I would bet you are on a Linux server or the host server has the mail function built in turned off. You are not with out options though.
First consider your server. If your server is a shared server and you are likely to have your mail sent to spam if it is delivered at all. Most websites will out source their message systems to keep the load off the server and to ensure delivery.
If you are not in the market for outsourcing your mail services you will need to use a library that will do most of the heavy lifting for you. I would recommend phpMailer it even has an autoset up build in with great examples on how to configure your server.
Next before you install php mailer read your host servers fine print most of them have strict rules about mail (meaning you have to keep a confirmation they wanted the email) if they let you send mail at all. They do this to keep their servers off of spam list.
Good Luck

DokuWiki on Windows wont send email

I set up DokuWiki on a Windows 2012 Server using wampserver
It works really well, but I can't get it to see any smtp server. I set up a local server using IIS, tested it using blat. I added the following lines to the php.ini:
SMTP = localhost
smtp_port = 25
sendmail_from address#host.com
I also tried using the computer's domain as the server, but Doku won't send any email. Looking at Doku's wiki, I found these helpful instructions: dokuwiki email, but I always get an error that says "Looks like there was an error on sending the password mail. Please contact the admin!"
I also tried the 'fake sendmail', but it gave me the same result.
I reset Apache and IIS after changing the configs, but there was no change. There is nothing in the log files either.
As far as I can tell, this should work. Is there another config file or something else that I missed?
Thanks,
Well, just pointing PHP to localhost for a mail server won't work unless you run a mailserver there.
You have three options:
configure your php.ini to point to a mail server on your LAN
run a local Mailserver (eg. http://www.pendriveapps.com/portable-smtp-server-mini-relay/)
use the swiftmail plugin (https://www.dokuwiki.org/plugin:swiftmail) to configure DokuWiki to use some other external mailserver (eg. gMail).

How to get PHP to send mail to local users (cakePHP, Linux SLES)

Currently I have a mail server configured (a real one from my ISP) and mail internal and external works on the command line. In PHP only external users work.
For testing I would like to send to internal users only.
( Ideally I would like to set up lots of aliases that point to one user so mail to:
tom#localhost.com, dick#localhost.com, harry#localhost.com end up in /var/mail/johnsmith )
I'd be greatful if someone could help here. I'm hesitant to edit the postfix config files...
On the command line johnsmith#localhost works but not in PHP. It's using cakePHP and I checked the value of $email-addr just before the send ($this->Email->send();) and the value is johnsmith#localhost. I'm not that familiar with cakePHP yet. The var/log/mail shows nothing, only external email addresses.
(server is Suse linux)
You can use the basic mail php function
http://php.net/manual/en/function.mail.php
Under linux, mail php function relies on sendmail, just check that sendmail is properly installed.
In /etc/postfix/main.cf add localhost.com:
mydestination = $myhostname, localhost.$mydomain,localhost,localhost.com
This allows sending on "localuser#localhost.com" via command line. I loaded up a test php mail form script in a browser and it works, sending mail through to /var/mail/localuser.
At the moment it means I have to check each local users /var/mail file. I'm working on the alias. My first attempt at that failed.

Fun fact with 'sendmail.exe' when using 'mail' function in PHP

I've already set sendmail_path to the path of 'sendmail.exe', but PHP didn't send email. it requires mail server or something.
PHP said can't connect to localhost at port 25. The fun fact in the php.ini file is that above the path to 'sendmail.exe', stated "Unix only!????", but 'sendmail.exe' is Windows executable. [i believe the sendmail.exe i have in XAMPP is windows version of the Unix sendmail, so the comment in php.ini might not be at right place]
I'm just only sending email, not receiving email, so why do i need a mail server (like Mercury)? or the SMTP (the protocol) requires a server for both sending & receiving?
As per my thinking, we can just open the port (usually 25? 465?) of the destination server to send message to. Anything wrong in my procedure here and above?
Just can't find the details in similar Stackoverflow questions so i bring up this matter, please help me get enlightened.
Only because you find a file called sendmail.exe on your computer it does not mean that PHP supports that specific binary out of the box.
From sendmail_path:
[...] This directive works also under Windows. If set, smtp, smtp_port and sendmail_from are ignored and the specified command is executed.
Double check that sendmail.exe on your disk is compatible with PHP.
Try:
sendmail_path = "X:\path\to\sendmail.exe -t"
I'm just only sending email, not receiving email, so why do i need a mail server (like Mercury)? or the SMTP (the protocol) requires a server for both sending & receiving?
You don't need to. It's only likely that the configuration you use with sendmail.exe (that's not the PHP configuration, look for sendmail.ini instead) that you have told sendmail.exe to use an SMTP server.
Either change that, and if sendmail.exe does not provide the feature you're looking for, replace sendmail.exe with something that matches your needs.
It's just that by default it actually makes sense to send emails. So why are you upset about a configuration that makes sense?
See also:
Mock mail on xampp development box

PHP Mail Error in Microsoft WebMatrix

I am a newbie in Web Development an I am currently learning PHP and MySQL. I have read HeadFirst PHP and MySQL, and tried the examples. But The PHP Mail() function doesn't work on my Local Machine. I have tried the script on a web server but nothing happens, the mail isn't sent. Please Help me. I have configured the PHP.INI file to send emails but still the problem persists.
<?php
$to = "me#me.com";
$sub = "hello";
$msg = "Hello, how are you?";
//Mail Function
mail($to,$sub,$msg);
?>
I am using WebMatrix with PHP 5.2 installed. Please help me, I am trying out this one since last 2 hours! I am stuck!
mail() uses 'localhost' to send - it generally assumes it's on Linux.
You will need to aquire a basic SMTP server and run it on windows, OR you may be able to use the SMTP server of your're ISP.
Whichever option, you will need to edit your php.ini, you will find:
[mail function]
; For Win32 only.
;SMTP =
you must set SMTP to the ip/port of a mailserver - again wither run one locally or use your ISP.
EDIT
You could try this approach - I have personally never tried to use GMail for sending: http://www.linuxquestions.org/questions/programming-9/php-pear-mail-packege-support-security-through-ssl-586976/
But The PHP Mail() function doesn't work
Yes it does. The problem is either with how you configured PHP or with the MTA you configured it to use. You'd need to provide details of both for us to understand why mail is not getting sent.
The above answer also is applicable to those who use a simulated local IIS e.g. the WebMatrix on IIS Express users who reside within a corporate network with a SMTP machine available.
To be able to send emails out from within it one needs to edit the PHP.INI file (found typically in \Program Files (x86)\IIS Express\PHP\v5.3) and replace 'localhost' with the SMTP server IP or DNS name.

Categories