Problems with sending mail from localhost


I am getting an error while trying to send an automatic email notification through my IIS.

PHP.ini is set to: -
SMTP =localhost
smtp_port = 25

; For Win32 only.
;sendmail_from =

See code snippet below:
$our_email =
$emailfrom  = $arVals['email'];
$our_subject = 'Email Notification - Thankyou for your interest!';
$from_us = 'Me';
$headers2 = 
   'From:' . $from_us . '<' . $our_email . '>' . "\r\n" .
   'Reply-To:' . $our_email . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
$notification = 'hello';
mail($emailfrom, $our_subject, $notification, $headers2);

Open in new window

Who is Participating?
dereshConnect With a Mentor Commented:
gmail uses ssl on smtp su you need ssl support in php:
enable openssl.dll in php.ini and copy required dll in %SYSTEMROOT%\system32\ folder

and windows doesn't have sendmail. this is the reason you MUST use smtp to send mail on windows.

for other free smtp servers for windows you could try:

i haven't used php ond windows for quite some time, but this above should work as expected ( remember to turn off IIS SMTP off before installing other one)

dou you have localy (on server with iis) installed smtp/mail server. you need one if you intend to send mails through it ;)
for windows you have many free smtp servers which you could use.

RassacAuthor Commented:
I guess I'm using the windows one from
control panel > Administrative Tools > Internet Information Services.

In fact the code in the snippet works perfectly fine for me.

Am I missing something?

$to = "";
$subject = "test from localhost";
$msg = "I completely understand SMTP servers now!";
$headers = "From:\nReply-To:";
$config = "";
mail("$to", "$subject", "$msg", "$headers", "$config");
echo "finished!";

Open in new window

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Steve BinkCommented:
First, check the differences between line 7 of your working code snippet, and line 12 of your original code.  To help, here's the function header for mail():

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Also, line 1 of your original code does not include quotes around the string.  Perhaps that is just a typo in the post, though...?

Otherwise, what is the exact error you are receiving?
RassacAuthor Commented:
Hi routinet,

Yes line 1 double quotes is a mistake in the post. The error is:
SMTP server response: 501 5.5.4 Invalid Address in c:\Inetpub\wwwroot\hunggar\sql\process_form.php on line 72

Didn't quite understand why / what differences I have to check. Can you re-explain pls?

Steve BinkCommented:
Never mind...your headers are in the correct order.

The error you are receiving implies that the mail server either a) cannot deliver to the requested address or b) you are attempting to send from an unauthorized address.  I'm looking at (b) because of the ini snippet you posted originally:

>>> ; For Win32 only.
>>> ;sendmail_from =

That sendmail_from should be set for Windows servers, or set in the "From" headers of the mail() call.  Your header, as built in the code, will look like this:


At the very least, you need a space between them.  

"Me <>"

That assumes your mail server will recognize that format.  I would recommend setting the ini value to "", and testing that first.  

RassacAuthor Commented:
Hi Routinet,
I did the change in the php.ini file but I had no positive results. I used my yahoo email account. Could that be a problem?

Steve BinkCommented:
It could be.  If the server is set up *properly*, you are not an open relay, and the only accounts that can send mail from it are in the domain managed by the server.  Have you set that server to require authentication for sending?
RassacAuthor Commented:
I have Authentication as anonymous and Relay as "All except the list below" -  there nothing in the list. The "Allow all computers checkbox...." is ticked.

Does the above inspire something?
Steve BinkConnect With a Mentor Commented:
I've never used the SMTP server in IIS, so I can't be of help there.  The error you are receiving is definitely from the server, and is probably due to the account not being authorized.  Do you have a domain set up for email?  Have you tried an email account managed by this server?
RassacAuthor Commented:
No not really.... do you suggest anything else instead of SMTP server in IIS?
try changing line 5 to this:

$headers = "From:\r\nReply-To:\r\n";

Open in new window

RassacAuthor Commented:
OK I found one problem.... The script in the package downloaded had a small bug. The path to the included  files was incorrect. Now I fixed that and got this error message:

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl:// (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in c:\Inetpub\wwwroot\phpMailer_v2.2.1\examples\class.smtp.php on line 122

SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (40) Mailer Error: SMTP Error: Could not connect to SMTP host.

RassacAuthor Commented:
The error above came from using the test_gmail.php file ...
Using test_sendmail.php on my localhost I got the error "Mailer Error: Could not execute: /usr/sbin/sendmail"

However I managed to use the sendmail from my hosting account but not the test_gmail one.
RassacAuthor Commented:

That's great! I managed to use all scripts - only one I didn't manage to use from my localhost;  the test_mail.php. Seems I got some problems with the IIS SMTP server. Still strange though, because the test_smtp.php worked from my localhost.

However I think I read somewhere that the test_mail.php is slower than the others. Am I correct?
i don't know because i dont know what is in test_mail.php?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.