PHP7.1.9+Mailgun环境下curl_exec返回消息但邮件未发送问题
Hey David, let's break down why your emails aren't hitting the inbox even though curl_exec is returning a response. I've worked through similar Mailgun issues before, so here are the key steps to diagnose and fix this:
1. First, Capture and Inspect the Full Mailgun Response
Right now, you're not checking what exactly Mailgun is sending back. Even if curl_exec returns something, it could be an error message that tells you exactly what's wrong. Update your code to log/print the response and HTTP status code:
function send_email($from, $to, $subject, $body) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, 'api:your-actual-private-api-key'); // Replace with real key curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/your-sandbox-domain.mailgun.org/messages'); // Replace with real URL // Critical missing piece: Pass your email content as POST fields curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'from' => $from, 'to' => $to, 'subject' => $subject, 'text' => $body // Use 'html' instead of 'text' if sending HTML content )); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Grab HTTP status code curl_close($ch); // Print these to debug var_dump(json_decode($response, true)); // Decode JSON response for readability echo "HTTP Status Code: " . $httpCode; return $response; }
Mailgun returns detailed JSON errors (like invalid API keys, missing parameters, or unauthorized recipients) that will point you straight to the issue. For example, a 401 Unauthorized means your API key is wrong, and a 400 Bad Request usually means missing or invalid fields.
2. Verify Your API Key and Domain URL
- API Key: Make sure you're using the Private API Key from Mailgun's dashboard (under Domains > Your Sandbox Domain > API Keys). Don't use the Public Key—this is a super common mistake.
- Domain URL: Your request URL must follow the format
https://api.mailgun.net/v3/your-sandbox-domain.mailgun.org/messages. Double-check that you've replaced the placeholder with your actual sandbox domain (it looks likesandbox123abc.mailgun.org).
3. You Forgot to Pass Email Parameters in the POST Request!
Looking at your original code, you never sent the $from, $to, $subject, or $body variables to Mailgun via POST fields. Without these, Mailgun has no idea what email to send—this is almost certainly a core issue. The code above adds CURLOPT_POSTFIELDS to fix this gap.
4. Check Sandbox Domain Restrictions
Mailgun's sandbox domains only allow sending to pre-verified email addresses. If you're trying to send to an email that hasn't been added and verified in your Mailgun dashboard, the API will return an error (even if curl_exec returns a response). Head to your Mailgun domain settings and confirm the recipient is in your authorized list.
5. Ensure cURL is Configured Correctly
- Make sure the cURL extension is enabled in your PHP 7.1.9 setup. You can check this by running
phpinfo()and looking for the cURL section. - Confirm cURL supports HTTPS (Mailgun requires HTTPS for API requests). Run
var_dump(curl_version())and check that thessl_versionfield is present and not empty.
Once you add the response logging, you'll get a clear picture of what's failing. Start there—it's the fastest way to resolve the issue.
内容的提问来源于stack exchange,提问作者David

