Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dave Wize's avatar

Email (Laravel through Mailgun) is sent everywhere except to the same domain (it is in Office 365)

I'm using Laravel 10 and I encountered issues that emails are not being delivered if the receiver is from the same domain.

My email configuration is Mailgun on a subdomain mail.mydomain.com and if I send an email to any other address it works but if I send it to [email protected] it doesn't arrive.

Worth mentioning that In Mailgun it shows the email as delivered but in reality, it is not arriving...

0 likes
5 replies
LaryAI's avatar
Level 58

It sounds like you're experiencing an issue where emails sent from your Laravel application through Mailgun to addresses within your own domain are not being received, even though Mailgun reports them as delivered. This is a common issue related to DNS and email routing configurations.

Here's a step-by-step solution to troubleshoot and potentially resolve this issue:

  1. Check Mailgun Logs: Since Mailgun is reporting the emails as delivered, the first step is to check the logs in your Mailgun dashboard. Look for any errors or warnings that could indicate what might be happening after Mailgun sends the email.

  2. Verify DNS Settings: Ensure that your DNS settings are correctly configured for Mailgun. This typically involves setting up TXT, MX, and CNAME records as per Mailgun's instructions. For emails to be delivered within your own domain, the MX records should point to Mailgun's servers.

  3. Check Office 365 (Microsoft 365) Configuration: Since your domain's email is hosted with Office 365, you need to make sure that it's configured to accept emails from your Mailgun subdomain. This might involve setting up connectors in the Office 365 Exchange admin center to trust emails coming from Mailgun.

  4. Email Routing: If your domain's MX records are pointing to Office 365, emails sent to your domain are expected to be handled by Office 365 servers. However, since you're using Mailgun to send emails, there might be a conflict. You may need to configure Office 365 to recognize that emails sent from your Mailgun subdomain are legitimate and should be routed correctly.

  5. Check Spam/Junk Folder: Sometimes emails might be marked as spam or junk by the receiving email server. Ask the recipients within your domain to check their spam/junk folders.

  6. Contact Support: If you've gone through all the above steps and the issue persists, it might be time to contact Mailgun support for further assistance. They can provide more detailed logs and guidance specific to your account.

Here's a sample code snippet to send an email using Laravel's Mail facade, which you're likely already using:

use Illuminate\Support\Facades\Mail;

Mail::raw('This is a test email', function ($message) {
    $message->from('[email protected]', 'Your Application');
    $message->to('[email protected]');
    $message->subject('Test Email');
});

Remember to replace '[email protected]' and '[email protected]' with your actual sending and receiving email addresses.

If you've confirmed that your Laravel code is correct and the issue persists, it's likely a configuration issue outside of Laravel, either with Mailgun or Office 365.

Snapey's avatar

make sure you have the correct SPF record

Dave Wize's avatar

@Snapey the SPF from Mailgun? It is correct.

The office 365 server is configured by the IT company from my client.

Snapey's avatar

@Dave Wize when you send, is your from address, using the subdomain or the main domain?

Please or to participate in this conversation.