Search code examples
phplaravelemailsmtp

Office365 SMTP Issue in Laravel 9


I am using Mailtrap to send emails from my Laravel app but I want to use Office365 SMTP but unable to do so: I am using following credentials

MAIL_MAILER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=MyOutlookUsername
MAIL_PASSWORD=MyOutlookPassword
MAIL_ENCRYPTION=starttls
MAIL_FROM_ADDRESS="Email"
MAIL_FROM_NAME="${APP_NAME}"

Sometimes this "Failed to authenticate on SMTP server with username" error is coming or "Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully."

One thing I am not sure about is, which MAIL_USERNAME and MAIL_PASSWORD I am supposed to put? Is it my outlook Username/Password or I have to create an application and use its credentials here?

Thank You.


Solution

  • You might need to change the MAIL_ENCRYPTION to tls instead of starttls

    If you're still facing issues, try setting the stream option in your config/mail.php file to tls:

    'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
        'tls' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true,
        ],
    ],
    

    Run Optimize command to clear the cache

    php artisan optimize:clear OR php artisan config:clear

    Also, For the Gmail account, we need to create an app and use its secret key but I'm not sure about Office365.