i used this code for email

$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);

however this error shows up Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()..

i wanted to fix it without using outlook but the best solution i have googled so far is from this one, Drupal(Im not advertising it lol). However, I think I have to download Drupal. Is there any other way?

BTW i use XAMPP phpMyAdmin - 2.10.3

Dani AI

Generated

you do not need a live host for this to work. That error means PHP tried to talk to an SMTP server at 127.0.0.1 on port 25 and nothing answered. On Windows, mail() does not send mail by itself; it connects to the SMTP server defined in php.ini (SMTP and smtp_port). If no server is running there, you get exactly this message. is on the right track: it is a config issue, not Drupal. See the PHP docs for how mail() behaves on Windows. (php.net)

Two practical fixes:

  • Use SMTP directly from PHP with a mailer library. PHPMailer is the go-to choice and lets you send through a real provider (your ISP, Gmail, etc.). Example (minimal setup over TLS on port 587):
use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'you@example.com';
$mail->Password = 'your-app-password';
$mail->setFrom('you@example.com');
$mail->addAddress('somebody@example.com');
$mail->Subject = 'Test';
$mail->Body = 'Hello world!';
$mail->send();

PHPMailer setup and examples are in its README. Gmail’s SMTP supports TLS on 587 and SSL on 465; if you use Gmail with 2‑Step Verification, generate an App Password and use that instead of your normal password. (github.com)

  • For local testing without sending real email, run a dev SMTP catcher like MailHog (SMTP on 1025, web UI on 8025). Point your mailer to 127.0.0.1:1025 with no auth to see messages instantly in the browser. Alternatively, XAMPP includes Mercury Mail, which you can enable from the control panel to provide a localhost SMTP server. (github.com)

If you insist on mail(), change php.ini to point SMTP to a real SMTP host and use its recommended port, then restart Apache. The library route above is more reliable and easier to debug. (php.net)

Recommended Answers

All 5 Replies

You are running this program on local host.
I think there should be a server on cloud to do that, not XAMPP on your system.
And i dont think Drupal is the solution for the problem. It is a complete CMS.
Vinayak

You are running this program on local host.
I think there should be a server on cloud to do that, not XAMPP on your system.
And i dont think Drupal is the solution for the problem. It is a complete CMS.
Vinayak

Ok so It should be live for it to work?

Ok so It should be live for it to work?

Yes
you can try any free web host for practicing(this is what i think you are doing) your PHP programs.
Vinayak

your error means that ur server is not configure to use smtp
or configured incorrectly which i dont think so .. are u using free webhosting?

or run it on local server?

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.