You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Step 1: Configure hMailServer for PHP SMTP Access

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 (like local.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 > Connections and add 127.0.0.1 to the Allowed IP ranges list. 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 > Authentication and check Require SMTP authentication. You’ll need to use the sender account’s credentials in your PHP script later.
Step 2: Tweak PHP Settings in AMPPS

AMPPS uses its own PHP installation, so we need to point it to hMailServer’s SMTP service:

  • Open AMPPS, click the PHP icon, then select Edit php.ini to open the configuration file.
  • Scroll down to the [mail function] section and update these lines:
    SMTP = 127.0.0.1
    smtp_port = 25
    sendmail_from = auto-sender@local.test
    
    (Replace auto-sender@local.test with the email account you created in hMailServer.)
  • Save the php.ini file, then restart the Apache server in AMPPS (click Restart next to Apache) to apply the changes.
Step 3: Test with a Basic PHP Script

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 click Messages to 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 src folder and place it in your project directory.
  • Create a script like auto-email-phpmailer.php with 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.
Troubleshooting Common Issues
  • Check hMailServer logs: Go to Settings > Logging in hMailServer Admin, enable SMTP logging, then check the log file (usually C:\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, add 127.0.0.1 local.test to your C:\Windows\System32\drivers\etc\hosts file to ensure your computer resolves the domain to your local server.

内容的提问来源于stack exchange,提问作者Serena Lekhrajani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:11:12