Member Avatar for Member #481668

hi, i have a hard time testing my PHP mail() function in the localhost.
can you teach me? it's my first time to use this mail() function.

this is my PHP code in the file: contact.php

<?php
    $email_to = $_REQUEST['email_to'];
    $subject = "Re: FAQ Suggestion";
    $email =  "d26thletter@yahoo.com";
    $message = $_REQUEST['message'] ;
    $headers = "From: $email";
    $sent = mail($email_to, $subject, $message, $headers) ;
    if($sent)
    {print "Your mail was sent successfully"; }
    else
    {print "We encountered an error sending your mail"; }
    ?>

when i run it in the localhost, i got this error message:

SMTP server response: 553 We do not relay non-local mail, sorry. in C:\xampp\htdocs\FAQ\contact.php on line 15
We encountered an error sending your mail

how could i make the mail() function work?

Dani AI

Generated

@starsinthesky that 553 message means your local SMTP server is refusing to relay mail to non-local domains without authentication. On Windows/XAMPP, PHP’s mail() talks directly to the SMTP host defined in php.ini (SMTP, smtp_port, sendmail_from), so if that server does not allow relaying from localhost, mail() fails with exactly this kind of error. (php.net)

Two reliable ways to test on localhost:

  • Quick dev-only testing: run a local catch-all SMTP like smtp4dev, then set SMTP=127.0.0.1 and the matching port in php.ini, restart Apache, and watch captured messages in the web UI. No relaying or real email involved. (github.com)
  • To send real mail: do what suggested (use a provider/ISP SMTP), but use a library that supports SMTP auth and TLS instead of raw mail(). PHPMailer is the common choice. (github.com)

Example using PHPMailer (minimal and auth-capable):

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.yourprovider.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@yourdomain.com';
$mail->Password = 'app-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

$mail->setFrom('user@yourdomain.com', 'Your Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Test';
$mail->Body = 'Hello from localhost!';
$mail->send();

Tips that save time:

  • Match setFrom() to the domain you authenticate with; many servers reject mismatched senders.
  • If you still see relay errors like yours, enable $mail->SMTPDebug = SMTP::DEBUG_SERVER; to see the server’s exact response.
  • As noted, it works immediately on a properly configured server; if you stick with mail(), ensure the php.ini SMTP host actually permits relaying from your machine or use a local MTA. (php.net)

For reference, PHP’s docs explain the Windows-specific SMTP settings for mail(), and why you typically need an SMTP server or library for authenticated sending. (php.net)

Recommended Answers

All 6 Replies

its becoz an email server is nt configured on your local machine...It wil work fine when u upload the file to the server....

Member Avatar for Member #481668

how can i configure an email server to the local? thanks ahead.

You'll have to use your isp's SMTP server.
See here:

I agree with "shefeekj" you should configure your mail server. If you see Windows, I just wanna suggest you to follow these step using ArgoSoft:

Good luck, buddy

thingking........

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.