0

I think i tried to fix this issue fr 3 days now and cant seem to find the problem.

I use XAMPP and use this code:

<?php

$to = "carl.j.97@live.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "carl.j.97@live.com";
$headers = "From: $from";
$res= mail($to,$subject,$message,$headers);
echo " $res Mail Sent.";
?>

when i enter that page i get an error that says:

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(

My php.init file in xampp are as follow:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smpt.gmail.com
; http://php.net/smtp-port
smtp_port = 25

That is all my codes.

AnFi
  • 10,493
  • 3
  • 23
  • 47
Peter
  • 45
  • 1
  • 8
  • It's as the error says, it can't connect to your SMTP server. This basically means you didn't setup a SMTP server. XAMPP is shipped with **mercury**, do some research on it. – HamZa Jun 30 '13 at 16:21
  • possible duplicate of [Failed to connect to mailserver at "localhost" port 25](http://stackoverflow.com/questions/4532486/failed-to-connect-to-mailserver-at-localhost-port-25) – Herbert Jun 30 '13 at 16:22
  • XAMPP uses Mercury. Here's a post that might help http://stackoverflow.com/questions/4486155/how-do-i-enable-xampp-to-locally-use-the-phps-mail-function-so-i-can-test-my – SeanWM Jun 30 '13 at 16:22
  • @BadWolf i tried what you said, and now it says: SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host. – Peter Jun 30 '13 at 16:31

3 Answers3

0

Run this script in your production server. You cant send emails from localhost unless you install a SMTP server in local.

Vishnu Sureshkumar
  • 2,246
  • 6
  • 35
  • 52
0

If you run this on XAMPP or anoher program that runs on localhost, search on Google for the settings you need to get email service working. You need to edit some files and change some ports/client names.

EDIT: you only have to modify 2 ini files: php.ini and sendmail.ini

1)look for mail function in php.ini(c:/xampp/php/php.ini)>>[mail function]

change the following::

SMTP=smtp.gmail.com

smtp_port=587


sendmail_from = from@gmail.com

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

NOTE: ensure that the path you specify for sendmail_oath is valid.In my case it is in C. SAVE your changes.

2) next modify sendmail.ini(c:/xampp/sendmail/sendmail.ini) comment the mercury as shown below

# Mercury
#account Mercury
#host localhost
#from postmaster@localhost
#auth off

# A freemail service example
#account Hotmail
#tls on
#tls_certcheck off
#host smtp.live.com
#from [exampleuser]@hotmail.com
#auth on
#user [exampleuser]@hotmail.com
#password [examplepassword]

then paste the following lines:

account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from x@gmail.com
auth on
user x@gmail.com
password x

port 587

# Set a default account
account default : Gmail

Again SAVE your changes.

Use the code below to test if it's working!

<?php
$subject="Test mail";
$to="someone@whatever.com";
$body="This is a test mail";
if (mail($to,$subject,$body))
echo "Mail sent successfully!";
else
echo"Mail not sent!";
?>

NOTE in the above configuration the sender should use gmail email service,for the recipient any email service will do.

Sergiu
  • 345
  • 2
  • 5
  • 18
0
  1. Are you sure you edited right php.ini file?

  2. Did you restart your Apache server after editing php.ini?

Sergei Beregov
  • 738
  • 7
  • 19