Log in

View Full Version : PHP Mail Help



Moshambi
01-21-2009, 08:47 AM
I'm trying to learn how to send email using PHP. Here is the code:



<?php

if(isset($_GET["sub"]))
{

$to = "blaaaah@gmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$headers = "From: sender@example.com\r\n" .
"X-Mailer: php";
if (mail($to, $subject, $body, $headers)){
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

}

?>


Here are the settings I have in my php.ini :



; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "D:\XAMPP\xampp\sendmail\sendmail.exe -t"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =


And finally here is the error I'm getting:



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:\XAMPP\xampp\htdocs\practice\test.php on line 13



I don't understand how all this stuff works or what I need to do to fix it. Help is appreciated, thanks!

rangana
01-21-2009, 09:56 AM
I'm experiencing the same thing here at the company.

The best thing for you is to ask what's your SMTP server, and replace highlighted:


ini_set('SMTP','SMTP.MYSITE.COM');


...add this code on the first line of your PHP code.

Moshambi
01-21-2009, 04:06 PM
Thank you for the reply rangana. I'm trying to do this locally though, so do I need to have my own mail server set up or something?

prasanthmj
01-21-2009, 06:05 PM
Yes you will need a mail server.

The easiest way to get a complete server up and running is to set up a virtual server (see vmware.com) You can download a complete web server with email/apache/php from http://www.vmware.com/appliances/directory/370 (look around the virtual appliance market place for more)
Using Vmware server or vmware player you can run these virtual machines.