I've been learning php and following the examples on w3schools until I came to this example and now I'm pretty stuck after having tried to make it work for countless hours.

This is the bit I'm on: http://www.w3schools.com/php/php_mail.asp

I try to send the email using the following code:

<?php
$to = "someone@example.com"; //I changed this with my email address
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

When I open the page I get the following error message:
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()

I've tried to find out about it on google apparently the php.ini file needs to be changed to accomodate this but some of the instructions were written in a very complicated way and I was unable to follow so if it can be explained in simple terms - it would be greatly appreciated!

Are you running from your personal machine or a webserver ? If you are running from your local machine, then it is hard to get it to work, because you either need an smtp server on your machine, or you need to connect to a mail provider which doesn't need authentication (most do).

I've set up a home server (as far as I know it's called that). I'm using xampp and use "localhost/" to access the page on google chrome.

Are you saying that if this was hosted on the web then this problem would dissapear?

I've set up a home server (as far as I know it's called that). I'm using xampp and use "localhost/" to access the page on google chrome.

Are you saying that if this was hosted on the web then this problem would dissapear?

Most likely. Every time I've encountered this problem, it is resolved as soon as it is hosted on a server with a SMTP.

Most likely. Every time I've encountered this problem, it is resolved as soon as it is hosted on a server with a SMTP.

Ahh. If that's the case I have nothing to worry about. By the way, did you have to change anything in php.ini etc.

Not usually on a properly-configured server with SMTP.

In the past, I have found that it sometimes works on a local machine if you configure the SMTP line in php.ini to point to the SMTP server of your ISP.

Sometimes it works, sometimes it doesn't- depends on your ISP and what kinds of restrictions they have on their SMTP server (usually whether they require SMTP authentication).

So if you have, say, Comcast as your ISP and their SMTP server address is "smtp.comcast.net" on port 25, you could try changing your php.ini file so that it reads like this:

SMTP = smtp.comcast.net
smtp_port = 25

Feel free to try that out, but don't be surprised if you don't have any luck. Also, I should note that the above php.ini configuration is for Windows. Unix servers rely on the "sendmail_path" line for that configuration. But that's only if you have sendmail set up on your server.

Oh, and if you do try that out, remember to save a backup of php.ini first. You probably know that already, but I feel obligated to say it, knowing the hell I've put myself through by messing with php.ini without a backup.

Hmm I've just tried changing the SMTP line from "localhost" to my ISP and still no luck. Do you think I should just leave it now with smtp being my ISP or change it back to "localhost" as it was. Because I doubt I'll be hosting this website and testing it on the web anytime soon.

What would you reccomend based on which one of these options will have more of a chance of making the mail function work (if any?)

Don't worry I managed to find a free web hosting place, uploaded the page, and it works :D:D

Member Avatar for Zagga

Hi folks,

Sorry for the late reply but I thought it worth mentioning that you can use PEAR to send emails from your local server if you have a gmail or yahoo email account.

PEAR is included in most XAMPP type installations so you can just replace the 'Mail' sub-folder inside the PEAR directory with the folder I have attached (to make sure you have the correct, up-to-date files).

Add the attached 'mail.php' to your include directory (or the PEAR directory) and include the following code to actually send the email (remember to change the 7 commented parameters at the top of the code).

<?php
require_once "mail.php";


$to = "Somebody <somebody@somewhere.com>"; // This is who you want to sent the message to.
$subject = "From localhost via PEAR"; // This is the email subject.
$body = "Hi,\n\nHow are you doing?"; // This is the email message.

$name = "Your Name"; // This should be your name.
$provider = "gmail"; // Should be 'gmail' or 'yahoo'.
$username = "you@gmail.com"; // This is your Gmail/Yahoo email address.
$password = "password"; // This is your Gmail/Yahoo password.


if ($provider == "gmail"){
	$host = "ssl://smtp.googlemail.com";
}
elseif ($provider == "yahoo"){
	$host = "ssl://smtp.mail.yahoo.com";
}
else{
	exit ("This script is currently only set up to work with yahoo or gmail accounts.");
}
$port = "465";
$from = $name . " <" . $username . ">";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));

// The lines below are the actual command to send the email so all the code above could be an 'included' file to reduce space. 
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

This does generate a bunch of errors if you have error reporting set to E_STRICT but is fine if set to E_ALL. The email gets sent either way.

Hope this helps.


Zagga

I'll remember to refer back to this if I have any more problems. Thank you Zagga!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.