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

PHP网站部署至dash.infinity.com后调用Resend API遇500内部服务器错误求助

问题解决:Resend API部署后500错误排查与替代方案

一、先排查Resend API 500错误的核心原因

你的代码在本地和Ngrok环境正常,部署到dash.infinity.com后出现异常,大概率是服务器环境差异或Resend平台限制导致,按以下步骤逐一排查:

  1. 检查服务器PHP环境匹配度

    • 确认服务器PHP版本≥7.4(Resend PHP SDK的最低要求),缺少cURL/json/mbstring这类核心扩展也会导致请求失败。可在服务器创建phpinfo.php查看环境信息,或直接联系主机商确认配置。
    • 验证vendor目录完整性:本地的依赖包需全部上传至服务器,若主机支持SSH操作,可在服务器执行composer install重新安装依赖,避免依赖缺失。
  2. Resend平台限制排查

    • IP白名单限制:登录Resend控制台,查看是否设置了IP访问白名单,将dash.infinity.com的服务器IP添加至允许列表。
    • 发件域名验证:你当前使用的onboarding@resend.dev是Resend的测试邮箱,生产环境可能被限制发送。需在Resend控制台验证自己的域名,改用no-reply@你的域名.com这类已验证的发件邮箱。
    • API状态检查:查看Resend控制台的API日志,确认密钥是否被禁用、是否触发限流(比如请求频率超限),日志会显示具体的错误码(如403/429),比代码catch到的模糊信息更有用。
  3. 完善错误日志定位问题
    当前代码的catch逻辑仅返回简单错误信息,AJAX可能无法获取完整报错细节。修改PHP代码,将错误写入本地日志文件:

    catch (Exception $e) {
        $errorLog = date('Y-m-d H:i:s') . " Resend Error: " . $e->getMessage() . PHP_EOL;
        file_put_contents(__DIR__ . '/resend_error.log', $errorLog, FILE_APPEND);
        echo "An error occurred: " . $e->getMessage();
    }
    

    查看生成的resend_error.log,即可明确是连接失败、权限问题还是参数错误。

  4. 密钥安全优化
    不要将API密钥硬编码在代码中,改用服务器环境变量存储:

    $resend = Resend::client(getenv('RESEND_API_KEY'));
    

    若使用Apache服务器,可在.htaccess中添加环境变量:

    SetEnv RESEND_API_KEY re_SN6U9cej_iLHNvbmjwgGCQEjyCQqiKtsL
    

二、稳定的替代邮件API

若排查后仍无法解决Resend的问题,推荐以下成熟的替代方案:

1. PHPMailer(SMTP方式,无第三方API依赖)

直接通过SMTP发送邮件,无需依赖第三方API,灵活性高,适合中小项目。以Gmail为例(需开启两步验证并生成应用密码):

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$email = $_REQUEST['email'];

try {
    $mail = new PHPMailer(true);
    
    // SMTP配置
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '你的Gmail账号@gmail.com';
    $mail->Password = '你的Gmail应用密码';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port = 465;
    
    // 邮件内容
    $mail->setFrom('no-reply@你的域名.com', 'Survey Notification');
    $mail->addAddress('appnotificationagent@gmail.com');
    $mail->Subject = 'Survey project notification';
    $mail->Body = $email . ' successfully completed their survey.';
    
    $mail->send();
    echo "success";
} catch (Exception $e) {
    echo "An error occurred: " . $mail->ErrorInfo;
}
?>

2. SendGrid

免费额度每月100封邮件,足够小项目使用,PHP SDK成熟稳定:

  • 注册后获取API密钥,安装SDK:composer require sendgrid/sendgrid
  • 代码示例:
<?php
require 'vendor/autoload.php';
$email = $_REQUEST['email'];

$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$from = new \SendGrid\Mail\From('no-reply@你的域名.com', 'Survey Notification');
$to = new \SendGrid\Mail\To('appnotificationagent@gmail.com');
$subject = new \SendGrid\Mail\Subject('Survey project notification');
$plainTextContent = new \SendGrid\Mail\PlainTextContent($email . ' successfully completed their survey.');

$mail = new \SendGrid\Mail\Mail($from, $to, $subject, null, $plainTextContent);

try {
    $response = $sendgrid->send($mail);
    echo "success";
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
}
?>

3. Mailgun

免费计划每月5000封邮件,适合中小项目,支持SMTP和API两种方式:

  • 注册后验证域名,获取API密钥,安装SDK:composer require mailgun/mailgun-php
  • 代码示例:
<?php
require 'vendor/autoload.php';
$email = $_REQUEST['email'];

$mgClient = Mailgun\Mailgun::create(getenv('MAILGUN_API_KEY'));
$domain = '你的已验证域名';

try {
    $mgClient->messages()->send($domain, [
        'from'    => 'Survey Notification <no-reply@你的域名.com>',
        'to'      => 'appnotificationagent@gmail.com',
        'subject' => 'Survey project notification',
        'text'    => $email . ' successfully completed their survey.'
    ]);
    echo "success";
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
}
?>

内容的提问来源于stack exchange,提问作者Au Wai Lun

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 17:14:51