Mail password with spaces corrupts .env file
Product: Crater Self-Hosted Invoicing (v6.0.6, Laravel 8.x)
File: app/Space/EnvironmentManager.php (method getMailData())
Description
When saving SMTP mail configuration through Settings → Mail Configuration, the MAIL_PASSWORD value is written to .env without quotes. If the password contains spaces (e.g., a Google App Password like abcd efgh ijkl mnop), the dotenv parser reads only the first word, silently discarding the rest. Subsequent mail sending fails.
Root Cause
In EnvironmentManager.php::getMailData(), all MAIL_PASSWORD lines are written unquoted:
'MAIL_PASSWORD='.$request->mail_password."\n".Compare with MAIL_FROM_NAME which is correctly quoted:
'MAIL_FROM_NAME="'.$request->from_name."\"\n\n";The same issue affects the "old data" patterns used to find-and-replace existing values (.config('mail.password') also unquoted). If the file was manually edited to add quotes (the only workaround), str_replace fails to match and appends a duplicate unquoted line instead.
Affected Patterns
All 5 switch cases (smtp, mailgun, ses, mail, sendmail) in getMailData() — both the $oldMailData and $newMailData blocks:
- Old data (line ~289):
'MAIL_PASSWORD='.config('mail.password')."\n". - New data (line ~298):
'MAIL_PASSWORD='.$request->mail_password."\n".
Reproduction
- Go to Settings → Mail Configuration
- Select driver "SMTP"
- Enter a password containing spaces (e.g., a Google App Password)
- Click "Save"
.envnow contains:MAIL_PASSWORD=abcd efgh ijkl mnop(unquoted)- Test email fails with:
The environment file is invalid! Failed to parse dotenv file. Encountered unexpected whitespace
Expected Behavior
MAIL_PASSWORD should be quoted when written to .env, matching how MAIL_FROM_NAME is handled:
'MAIL_PASSWORD="'.$request->mail_password."\"\n".This fix needs to be applied in all 5 switch cases, for both old and new data patterns.
Workaround
Manually edit .env to wrap the password in quotes:
MAIL_PASSWORD="abcd efgh ijkl mnop"Then php artisan config:clear or restart PHP-FPM.
Source: crater-invoice-inc/crater