简体   繁体   中英

Mail Not Sending via Mailgun API Laravel 8

I am trying to send mail via the mailgun API from my controller but the mail is not reaching mailgun and I am not getting any error messages/logs.

This is in my .env :

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=subdomain.domain.ca
MAILGUN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxx

This is in my services.php :

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third-party services such
    | as Mailgun, Postmark, AWS, and more. This file provides the de facto
    | location for this type of information, allowing packages to have
    | a conventional file to locate the various service credentials.
    |
    */

    'mailgun' => [
        'domain' => env('subdomain.domain.ca'),
        'secret' => env('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'),
    ],

    'postmark' => [
        'token' => env('POSTMARK_TOKEN'),
    ],

    'ses' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
    ],

];

This is in my mail.php :

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Mailer
    |--------------------------------------------------------------------------
    |
    | This option controls the default mailer that is used to send any email
    | messages sent by your application. Alternative mailers may be setup
    | and used as needed; however, this mailer will be used by default.
    |
    */

    'default' => env('MAIL_MAILER', 'mailgun'),

    /*
    |--------------------------------------------------------------------------
    | Mailer Configurations
    |--------------------------------------------------------------------------
    |
    | Here you may configure all of the mailers used by your application plus
    | their respective settings. Several examples have been configured for
    | you and you are free to add your own as your application requires.
    |
    | Laravel supports a variety of mail "transport" drivers to be used while
    | sending an e-mail. You will specify which one you are using for your
    | mailers below. You are free to add additional mailers as required.
    |
    | Supported: "smtp", "sendmail", "mailgun", "ses",
    |            "postmark", "log", "array"
    |
    */

    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
        ],

        'ses' => [
            'transport' => 'ses',
        ],

        'mailgun' => [
            'transport' => 'mailgun',
        ],

        'postmark' => [
            'transport' => 'postmark',
        ],

        'sendmail' => [
            'transport' => 'sendmail',
            'path' => '/usr/sbin/sendmail -bs',
        ],

        'log' => [
            'transport' => 'log',
            'channel' => env('MAIL_LOG_CHANNEL'),
        ],

        'array' => [
            'transport' => 'array',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'info@domain.ca'),
        'name' => env('MAIL_FROM_NAME', 'From name'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown-based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

This is how I am sending the mail in my controller:

Mail::send('emailtemplates.trackeremail', $data, function($message)use($data, $pdf) {
                $message->to($data["toaddress"])
                        ->cc($data["ccaddress"])
                        ->subject($data["title"])
                        ->attachData($pdf->output(), "PDFName.pdf");
            });

This is working when I send to mailgun via SMTP in my local environment but I am unfortunately limited to shared hosting that does not allow 3rd party SMTP and I need to switch to the API method for production. I have gone through many tutorials and questions on this site (and others) related but nothing has solved my issue.

I always run php artisan config:clear after any kind of .env change.

Is there anything else that I can be doing to even get an error message or further debug this? Any help would be appreciated.

It might possibly be the port, host, encryption, and mail from:

Keep these as is in your .env :

MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Enter these in your .env :

MAIL_FROM_NAME=YourNameHere
MAIL_FROM_ADDRESS=enter@theemail.com
MAIL_FROM=enter@theemail.com
MAILGUN_DOMAIN=subdomain.domain.ca
MAILGUN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxx

Update services.php with:

    'mailgun' => [
        'domain'   => env('MAILGUN_DOMAIN'),
        'secret'   => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],

Update mail.php with adding:

'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'port' => env('MAIL_PORT', 587),

After going over this with A2 Hosting's technical support it turns out that this is just not possible with their shared hosting, I was informed that "Mailgun only supports the standard SMTP ports, so it is not supported on our shared server".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM