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

如何在发送邮件前验证Mailgun API可用性并记录API错误

Mailgun PHP API 异常捕获失效与预验证方案

问题场景

我使用Mailgun PHP API发送邮件,原代码如下:

$senderEmail="no-reply@".MAILING_URL;
//configure mail gun to use guzzle http client
$client = new \GuzzleHttp\Client();
$configurator = new HttpClientConfigurator();
$configurator->setHttpClient($client);
$configurator->setApiKey($this->mailGunKey);
// First, instantiate the SDK with your API credentials
$mg = new Mailgun($configurator); // For US servers
// Now, compose and send your message.
$domain = MAILING_URL;
# Make the call to the client.
$mg->messages()->send($domain, array(
    'from' => "$sendername < $senderEmail >",
    'to' => "$recipientname < $recipientemail >",
    'subject' => $emailsubject,
    'html' => $emailcontent,
    'h:Reply-To' => $supportEmail,
        ));

当Mailgun账户出现域名未验证、订阅取消等问题时,会触发致命错误:

Fatal error: Uncaught Mailgun Exception HttpClientException: Forbidden! {"message":"Domain app.researchpaper101.com is not allowed to send: Subscription Canceled")

该错误会导致应用直接停止运行。我希望在发送邮件前验证Mailgun API的可用性,同时记录错误但不影响应用其他功能,但尝试的异常捕获代码并未生效:

try {
    $senderEmail = "no-reply@" . MAILING_URL;

    // Configure Mailgun to use Guzzle HTTP client
    $client = new \GuzzleHttp\Client();
    $configurator = new HttpClientConfigurator();
    $configurator->setHttpClient($client);
    $configurator->setApiKey($this->mailGunKey);

    // Instantiate the Mailgun SDK
    $mg = new Mailgun($configurator);

    // Compose and send your message
    $domain = MAILING_URL;
    $mg->messages()->send($domain, array(
        'from' => "$sendername <$senderEmail>",
        'to' => "$recipientname <$recipientemail>",
        'subject' => $emailsubject,
        'html' => $emailcontent,
        'h:Reply-To' => $supportEmail,
    ));

    // Email sent successfully, you can add any success handling code here
} catch (\GuzzleHttp\Exception\RequestException $e) {
    // Handle Guzzle request exception
    error_log("Guzzle Request Exception: " . $e->getMessage());
    echo "An error occurred while sending the email. Please try again later.";
} catch (Mailgun\Exception\HttpClientException $e) {
    // Handle Mailgun HTTP client exception
    error_log("Mailgun Error: " . $e->getMessage());
    echo "An error occurred while sending the email. Please try again later.";
} catch (Exception $e) {
    // Handle other general exceptions
    error_log("Unexpected Error: " . $e->getMessage());
    echo "An unexpected error occurred. Please try again later.";
}

解决方案

异常捕获失效原因

  1. 命名空间缺失:HttpClientConfigurator、Mailgun等类未使用完整命名空间,导致实例化和异常捕获时无法匹配正确类。
  2. 异常覆盖不全:仅捕获了HttpClientException,未覆盖Mailgun的通用父类异常,存在遗漏。

修正后的代码

// 顶部引入必要命名空间(或使用完整命名空间)
use Mailgun\Mailgun;
use Mailgun\HttpClient\HttpClientConfigurator;
use Mailgun\Exception\HttpClientException;
use Mailgun\Exception\MailgunException;
use GuzzleHttp\Exception\RequestException;

try {
    $senderEmail = "no-reply@" . MAILING_URL;

    // 配置Guzzle客户端与Mailgun
    $client = new \GuzzleHttp\Client();
    $configurator = new HttpClientConfigurator();
    $configurator->setHttpClient($client);
    $configurator->setApiKey($this->mailGunKey);

    $mg = new Mailgun($configurator);

    // 提前验证域名状态(可选但推荐)
    $domainDetails = $mg->domains()->show(MAILING_URL);
    if ($domainDetails->get('status') !== 'active') {
        throw new \RuntimeException("域名 {$domainDetails->get('name')} 状态异常: {$domainDetails->get('status')}");
    }

    // 发送邮件
    $mg->messages()->send(MAILING_URL, [
        'from' => "$sendername <$senderEmail>",
        'to' => "$recipientname <$recipientemail>",
        'subject' => $emailsubject,
        'html' => $emailcontent,
        'h:Reply-To' => $supportEmail,
    ]);

    // 发送成功后的逻辑
} catch (RequestException $e) {
    error_log("Guzzle请求错误: " . $e->getMessage());
} catch (HttpClientException $e) {
    error_log("Mailgun HTTP错误: " . $e->getMessage());
} catch (MailgunException $e) {
    // 捕获所有Mailgun相关异常
    error_log("Mailgun服务错误: " . $e->getMessage());
} catch (\Exception $e) {
    error_log("未知错误: " . $e->getMessage());
}

核心优化点

  • 命名空间规范:通过use引入类或使用完整命名空间,确保类实例化和异常捕获的准确性。
  • 全面异常覆盖:添加MailgunException捕获块,覆盖所有Mailgun SDK抛出的异常,避免遗漏。
  • 预验证机制:调用domains()->show()接口提前检查域名状态,在发送邮件前拦截无效状态,减少不必要的请求。
  • 友好错误处理:移除echo输出(建议根据应用框架返回标准化响应),仅记录错误日志,保证应用正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 12:59:59