Windows10下Ampps环境配置hMailServer实现PHP自动发邮件
Hey there! Let's get your PHP script sending automatic emails via hMailServer in your AMPPS local environment on Windows 10. I’ve tackled this exact setup before, so here’s a straightforward, step-by-step guide to make it work smoothly.
First, we need to set up hMailServer to accept connections from your AMPPS server:
- Open the hMailServer Administrator and connect to your local instance.
- Set up a local domain: Navigate to
Domains > Add, enter a dummy local domain (likelocal.test), and save it. This gives you a base to create email accounts under. - Create a sender email account: Under your new domain, go to
Accounts > Add. Pick a username (e.g.,auto-sender) and set a secure password. This will be the account your PHP script uses to send emails (e.g.,auto-sender@local.test). - Allow local SMTP connections: Head to
Settings > Protocols > SMTP > Connectionsand add127.0.0.1to theAllowed IP rangeslist. This lets your AMPPS/Apache server connect to hMailServer without being blocked. - Optional security tweak: If you want to require authentication for SMTP (recommended), go to
Settings > Protocols > SMTP > Authenticationand checkRequire SMTP authentication. You’ll need to use the sender account’s credentials in your PHP script later.
AMPPS uses its own PHP installation, so we need to point it to hMailServer’s SMTP service:
- Open AMPPS, click the
PHPicon, then selectEdit php.inito open the configuration file. - Scroll down to the
[mail function]section and update these lines:
(ReplaceSMTP = 127.0.0.1 smtp_port = 25 sendmail_from = auto-sender@local.testauto-sender@local.testwith the email account you created in hMailServer.) - Save the php.ini file, then restart the Apache server in AMPPS (click
Restartnext to Apache) to apply the changes.
Let’s start simple with PHP’s built-in mail() function. Create a file called test-auto-email.php in your AMPPS web root (usually Ampps/www) with this code:
<?php // Replace with your recipient email (can be a local account or external if hMailServer is configured for it) $to = "test-recipient@local.test"; $subject = "Automatic Test Email from PHP"; $message = "This email was sent automatically when a trigger condition was met!"; $headers = "From: auto-sender@local.test\r\n"; $headers .= "Reply-To: auto-sender@local.test\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; if(mail($to, $subject, $message, $headers)) { echo "Automatic email sent successfully!"; } else { echo "Failed to send automatic email."; } ?>
- Access this script via your browser (e.g.,
http://localhost/test-auto-email.php). If it says “Automatic email sent successfully,” check the recipient’s inbox in hMailServer: go back to hMailServer Admin, select the recipient account, and clickMessagesto view incoming emails.
PHP’s mail() function can be inconsistent, so I highly recommend using PHPMailer—it’s more flexible and provides detailed error messages:
- Download the latest PHPMailer
srcfolder and place it in your project directory. - Create a script like
auto-email-phpmailer.phpwith this code:
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'src/Exception.php'; require 'src/PHPMailer.php'; require 'src/SMTP.php'; $mail = new PHPMailer(true); try { // Server settings $mail->isSMTP(); $mail->Host = '127.0.0.1'; $mail->SMTPAuth = true; // Enable if you turned on SMTP authentication in hMailServer $mail->Username = 'auto-sender@local.test'; $mail->Password = 'your-sender-account-password'; $mail->Port = 25; // Recipients $mail->setFrom('auto-sender@local.test', 'Auto Sender'); $mail->addAddress('test-recipient@local.test', 'Test Recipient'); // Content $mail->isHTML(true); $mail->Subject = 'Triggered Automatic Email'; $mail->Body = '<h1>Trigger Condition Met!</h1><p>This email was sent automatically by your PHP script.</p>'; $mail->AltBody = 'Trigger Condition Met! This email was sent automatically by your PHP script.'; $mail->send(); echo 'Automatic message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Error: {$mail->ErrorInfo}"; } ?>
- Adjust the credentials and recipient to match your setup, then run the script. This will give you clear feedback if something goes wrong.
- Check hMailServer logs: Go to
Settings > Loggingin hMailServer Admin, enable SMTP logging, then check the log file (usuallyC:\Program Files (x86)\hMailServer\Logs) for connection or authentication errors. - Verify Apache restart: Always restart Apache after changing php.ini—AMPPS won’t pick up the changes otherwise.
- Firewall settings: Make sure Windows Firewall isn’t blocking port 25 between AMPPS and hMailServer. Add an inbound/outbound rule for port 25 to allow local connections.
- Hosts file setup: If using a local domain like
local.test, add127.0.0.1 local.testto yourC:\Windows\System32\drivers\etc\hostsfile to ensure your computer resolves the domain to your local server.
内容的提问来源于stack exchange,提问作者Serena Lekhrajani

