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

使用PHPMailer对接Migadu邮件服务器发送邮件失败求助

Hey there, let's figure out why your Migadu + PHPMailer contact form is hanging and failing to send emails. I’ve gone through your code and put together targeted troubleshooting steps and fixes:

1. Turn on Debug Mode to Uncover Hidden Issues

Right now, your code gives no feedback because PHPMailer’s debug is disabled—this is why the page just hangs. Add these lines right after initializing the PHPMailer object to see exactly what’s going wrong:

$mail->SMTPDebug = 2; // Shows full SMTP conversation details
$mail->Debugoutput = 'html'; // Prints debug info directly to your page

This will stop the infinite loading and show you if the problem is a failed connection, authentication error, or something else entirely.

2. Test Your Server’s Connection to Migadu

Migadu uses port 587 for TLS, but many web hosts block outbound SMTP ports by default. Test if your server can reach Migadu’s SMTP server by running these commands in your server’s terminal:

# Test basic connectivity
telnet smtp.migadu.com 587

# Or use curl for more details
curl -v smtp://smtp.migadu.com:587

If the connection fails, reach out to your host to unblock port 587, or check your server’s firewall rules.

3. Add Input Validation & Error Handling

Your code uses raw $_POST data without checks, which can cause unexpected issues (like empty fields breaking the email body). Add this validation at the top of your script to catch bad inputs early:

# Check required fields exist
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
    echo json_encode(['status' => 'error', 'errorType' => 'missing_fields']);
    exit;
}

# Validate email format
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    echo json_encode(['status' => 'error', 'errorType' => 'invalid_email']);
    exit;
}

# Sanitize inputs to prevent XSS (critical since you're using HTML emails)
$name = htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');
$email = htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8');
$message = htmlspecialchars($_POST['message'], ENT_QUOTES, 'UTF-8');

This will stop invalid requests from reaching the email-sending logic and give clear error messages instead of hanging.

4. Verify Migadu Account Settings

Double-check these Migadu-specific details:

  • Confirm your info@mywebsite.com mailbox is active and not restricted/frozen in Migadu’s dashboard.
  • Check Migadu’s sending limits: Free or basic plans have hourly/daily send caps—if you’ve tested too many times, you might be temporarily blocked.
  • Ensure your SMTP username is the full email address (your code has this right, but it’s worth a quick double-check).
5. Update PHPMailer to the Latest Version

You’re using an older PHPMailer version (with separate class.phpmailer.php and class.smtp.php files). Old versions can have compatibility issues with modern TLS standards or Migadu’s server. Switch to the latest version—here’s a simplified example with proper error handling:

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

require 'vendor/autoload.php'; // Use Composer autoloader, or include the new PHPMailer files

try {
    $mail = new PHPMailer(true);

    # Server settings
    $mail->isSMTP();
    $mail->Host = 'smtp.migadu.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'info@mywebsite.com';
    $mail->Password = 'mypassword';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    # Recipients
    $mail->setFrom('info@mywebsite.com', $name);
    $mail->addAddress('info@mywebsite.com');
    $mail->addReplyTo($email, $name);

    # Content
    $mail->isHTML(true);
    $mail->Subject = 'Contact Form';
    $mail->Body = '<h1>Contact!</h1>
        <p><b>Name: </b>' . $name . '</p>
        <p><b>Email: </b>' . $email . '</p>
        <p><b>Message: </b>' . $message . '</p>';

    $mail->send();
    echo json_encode(['status' => 'ok']);
} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'errorType' => 'server',
        'message' => $mail->ErrorInfo
    ]);
}

The latest PHPMailer uses exceptions, so it’ll catch errors immediately instead of hanging.

6. Increase PHP Execution Time

If the page is still hanging, your PHP script might be timing out. Add this line at the very top of your script to extend the execution limit:

ini_set('max_execution_time', 60); // Allow up to 60 seconds for the script to run

Start with enabling debug mode—it’s the fastest way to pinpoint the exact issue. Once you have the debug logs, you’ll know if it’s a connection problem, authentication error, or something else.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:15:45