Reputation: 203
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.";
}
Upvotes: 0
Views: 340