0

I am using Mailgun PHP API to send emails using the code below

$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,
        ));

I would like to check if Mailgun is working correctly because it is triggering Server error when there is any problem with the Mailgun account, such as the domain not being verified or the subscription being canceled. Here is the example error


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

My application stops working when these server errors are triggered. I would like to log any errors without stopping other application functionalities.

I tried code blow but it is not working.

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.";
}

Joe Nyambura
  • 303
  • 5
  • 10
  • Are you sure you modified the correct code? Even if you were not able to catch the specific exception type because maybe the namespacing was wrong or something, the `catch (Exception $e)` should still catch _any_ kind of exception that might have occurred in the previous `try` block. – CBroe Aug 14 '23 at 08:15
  • Exceptions are Stringable, you can just error_log("Catched for logging - doh! : $e"); alternatively, you coud echo and re-throw or echo only. for the case you expect not to be exceptional. – hakre Aug 14 '23 at 12:23

0 Answers0