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

escozul's avatar

Error 500 when sending emails

Hello,

I am faced with really perculiar problem. I moved a Laravel 7 website from cpanel to an nginx-based web panel on ubuntu. (Cloudpanel.io)

That caused a few incompatibilities but with some help and modifications (including translating .htaccess to vhost configuration) I was able to have it running.

Right now I have a part of the website functions not working. It's the emails.

Whenever users try to sign up they should receive an email that is generated with the following code:

Mail::send(['html' => 'emails.sxm_user_activation'], $emaildata , function ($message) use ($emaildata) {
            $message->from(env('SXM_MAIL_NOREPLY'), env('SXM_SITE_NAME'));
            $message->to($emaildata['email'], env('SXM_SITE_NAME'))->subject(env('SXM_SITE_NAME').': Account Activation');
        });

That part of the code creates a white screen and nginx records a 500 internal server errror. I have no other clues about that other than the visiting IP. The error produces no output and simply stops the signup process at that point. If I comment it out, it seems that the site works just fine with the exception that no activation email is ever sent out and I need to activate users through phpmyadmin.

So I've narrowed it down to that Mail::send command.

After that error being so persistent, I tried to see what configuration my entire server was using. I found out that it was impossible to use Ipage as a mail provider so I used gmail. Now if I change the above command with the simple php mail(); command, laravel can send emails just fine. It's just that Mail::send was used to setup mailing troughout the site and I'd prefer to use that. I don't even know how to convert the Mail::send to mail();

I have setup the .env file with the settings and the OTP of gmail as an smtp provider.

MAIL_MAILER=smtp
#MAIL_HOST=smtp.ipage.com
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
#[email protected]
[email protected]
#MAIL_PASSWORD=****************
MAIL_PASSWORD=*****************
MAIL_ENCRYPTION=tls
#[email protected]
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

I have only included the current .env setting that includes the configuration for gmail as SMTP. Removed sensitive information. Of course my domain is not "domain.com"

I've also setup a failover in 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', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | 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' => [
        'failover' => [
            'transport' => 'failover',
            'mailers' => [
                'smtp',
                'postmark',
            ],
        ],
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.gmail.com'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
        ],
        /*'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', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

    /*
    |--------------------------------------------------------------------------
    | 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'),
        ],
    ],

];

So this is my Standpoint of status at the moment. I will try to get mail(); working properly so that sign ups may resume but I'd really like to figure out how to make Mail:send to work again.

Any idea will be hot...

0 likes
8 replies
Tray2's avatar

500 means that you have made something wrong in the code, and without the full error message we can't even begin to guess what the issue is.

1 like
escozul's avatar

@Tray2 I stand on the same boat as you. I have no clue where to dig up the actual error. The programmer who made the app included an error handler that should intercept 500 errors but it doesn't.

Snapey's avatar

don't use env() helper anywhere other than config files.

When you cache config env() will return null

regarding the error, check this by looking in the laravel log files.

Snapey's avatar
Snapey
Best Answer
Level 122

@escozul move the things that you have in env() into config() elements

add the following into your config\mail.php file

'sxm_mail_noreply' => env('SXM_MAIL_NOREPLY'),
'sxm_site_name' => env('SXM_SITE_NAME'),

and then change your invite code to use config

Mail::send(['html' => 'emails.sxm_user_activation'], $emaildata , function ($message) use ($emaildata) {
            $message->from(config('mail.sxm_mail_noreply'), config('mail.sxm_site_name'));
            $message->to($emaildata['email'], config('mail.sxm_site_name'))->subject(config('mail.sxm_site_name').': Account Activation');
        });

now your settings should come from config files

remember to re-cache the config

2 likes
escozul's avatar

@Snapey Well... daaaaamn... it worked!

But this env('SXM_SITE_URL') also is taken as null...

That has to be all over the site. How come the creator did that mistake?

Do you know where to define env('SXM_SITE_URL') the same way as 'sxm_mail_noreply'? Obviously I can't place that in mail.php... where do I place that?

Also... the previous programmer was using env('SOME_GLOBAL_VARIABLE') all the time. The site was working on cpanel like that. How come that changed on the new installtion?

Edit: should I add the other env() variables to app.php and then refer to them as config('some_global_variable')?

Snapey's avatar

@escozul so if you don't use artisan config:cache in the deployment then the config will be read from the .env file or the servers environment variables. This is not as good for performance and some people have reported intermittent problems when reading direct from .env on production.

Instead of env('SXM_SITE_URL') you should be able to use config('app.url') since this should already be your app's external URL

You can check any config parameter most easily by running php artisan tinker on the server and then

>>> config('app') 

for instance

The first part of the config corresponds with the name of the filename. eg config('app.url) is the url parameter from the config/app.php file which should be pulling in env('APP_URL') by default or could be hard coded.

To review your error logging issue, first tinker the values of config('logging') this will show you what the system is configured for and could be useful to create a new question.

1 like

Please or to participate in this conversation.