1

I have the following code to test if the website can send mail via php:

<?php 
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "emailtest@YOURDOMAIN";
$to = "YOUREMAILADDRESS";
$subject = "PHP Mail Test script";
$message = "This is a test to check the PHP Mail functionality";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>

I have uploaded this php file to www.mywebsite.com/data/iq/testing-email.php

when I run the script I get the following error:

Warning: 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 C:\mywebsite.com\data\iq\test-email.php on line 9

My question is why is the error showing a C:\ file directory when the website is already live?

For reference this website is running on a Microsoft IIS Server and has an SSL if that makes any difference.

1 Answer 1

2

When you use mail() your PHP script tries to connect to an external SMTP to deliver your email, so you must configure a valid one.

By default it tries to connect to localhost, but the webserver apparently is not running an SMTP server, so you should ask your provider for the correct SMTP to use, and set it with:

ini_set('SMTP','smtp.yourprovider.cxm');
ini_set('smtp_port',25);

About your second question, C:\mywebsite.com\ is probably the server folder where your files are hosted, so it looks very normal to me.

Sign up to request clarification or add additional context in comments.

2 Comments

Also what if it were gmail? Could I use ini_set() in the php file and not have to worry about updating php.ini? The problem is I don't have access to the web server and the guy who does is being difficult.
If you need SMTP authentication you cannot do it with PHP native implementation of mail(), you have to go through a PHP library which does all the job for you, there is one that works pretty well in Zend Framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.