# OpenDocMan Mail Configuration Guide This guide explains how to resolve mail performance issues in OpenDocMan's Docker environment and configure reliable email delivery. ## Problem Description OpenDocMan uses PHP's `mail()` function with sendmail, which can cause: - Long pauses when sending emails (30+ seconds) - Blocking of web application during email operations - Failed email delivery attempts - Poor user experience ## Solutions ### Solution 1: msmtp SMTP Relay (Recommended) The new configuration uses msmtp instead of sendmail for simpler, more reliable SMTP relay configuration. #### 1. Set Environment Variables In your `.env` file or docker-compose environment: ```bash # SMTP Configuration SMTP_HOST=smtp.gmail.com # Your SMTP server SMTP_PORT=587 # SMTP port (587 for TLS, 465 for SSL) SMTP_USERNAME=your-email@gmail.com # SMTP username SMTP_PASSWORD=your-app-password # SMTP password SMTP_ENCRYPTION=tls # tls or ssl MAIL_FROM_ADDRESS=noreply@yourcompany.com MAIL_FROM_NAME=OpenDocMan System ``` #### 2. Rebuild Your Container ```bash docker-compose down docker-compose build --no-cache docker-compose up -d ``` The configuration scripts will automatically: - Configure sendmail to use your SMTP relay - Set appropriate timeouts (30 seconds max) - Enable background delivery to prevent blocking - Create proper authentication files ### Solution 2: MailHog for Development/Testing Use MailHog to capture all emails locally for testing purposes. #### 1. Use the MailHog Docker Compose File ```bash # Use the provided MailHog configuration docker-compose -f docker-compose.mailhog.yml up -d ``` #### 2. Access MailHog Web Interface - Web UI: http://localhost:8025 - All emails will be captured and visible in the web interface - No emails will actually be sent externally ### Solution 3: Gmail SMTP Configuration For Gmail SMTP, use these specific settings: ```bash SMTP_HOST=smtp.gmail.com SMTP_PORT=587 SMTP_USERNAME=your-gmail-address@gmail.com SMTP_PASSWORD=your-app-password # Not your regular password! SMTP_ENCRYPTION=tls MAIL_FROM_ADDRESS=your-gmail-address@gmail.com ``` **Important**: You must use an App Password, not your regular Gmail password: 1. Enable 2-factor authentication on your Gmail account 2. Go to Google Account settings > Security > App passwords 3. Generate a new app password for OpenDocMan 4. Use this app password in SMTP_PASSWORD ### Solution 4: Office 365/Outlook SMTP For Office 365/Outlook.com SMTP: ```bash SMTP_HOST=smtp-mail.outlook.com SMTP_PORT=587 SMTP_USERNAME=your-email@outlook.com SMTP_PASSWORD=your-password SMTP_ENCRYPTION=tls MAIL_FROM_ADDRESS=your-email@outlook.com ``` ### Solution 5: Other SMTP Providers **SendGrid**: ```bash SMTP_HOST=smtp.sendgrid.net SMTP_PORT=587 SMTP_USERNAME=apikey SMTP_PASSWORD=your-sendgrid-api-key SMTP_ENCRYPTION=tls MAIL_FROM_ADDRESS=your-verified-sender@yourdomain.com ``` **Mailgun**: ```bash SMTP_HOST=smtp.mailgun.org SMTP_PORT=587 SMTP_USERNAME=postmaster@your-domain.mailgun.org SMTP_PASSWORD=your-mailgun-password SMTP_ENCRYPTION=tls ``` ## Performance Improvements The new msmtp configuration includes several performance optimizations: 1. **Simplified SMTP**: msmtp is lighter and faster than sendmail 2. **Direct SMTP Connection**: No complex queue management needed 3. **Connection Timeouts**: 30-second maximum timeout prevents hanging 4. **Proper Authentication**: Secure SMTP authentication with TLS/SSL 5. **Detailed Logging**: Separate logs for PHP mail and msmtp operations 6. **No Background Services**: No need to manage sendmail daemon ## Troubleshooting ### Check Mail Logs ```bash # View PHP mail logs docker-compose exec app tail -f /var/log/mail.log # View msmtp logs docker-compose exec app tail -f /var/log/msmtp/msmtp.log # Check msmtp configuration docker-compose exec app cat /etc/msmtprc # Test msmtp configuration docker-compose exec app msmtp --serverinfo --account=default ``` ### Test Email Sending Create a test PHP script to verify email functionality: ```php ``` ### Common Issues and Solutions #### 1. "Connection timed out" - Check SMTP_HOST and SMTP_PORT settings - Verify firewall allows outbound connections on SMTP port - Try different SMTP ports (25, 465, 587) - Check network connectivity: `docker-compose exec app ping smtp.gmail.com` #### 2. "Authentication failed" - Verify SMTP_USERNAME and SMTP_PASSWORD - For Gmail, ensure you're using an App Password - Check if SMTP server requires TLS/SSL - Test with: `docker-compose exec app msmtp --serverinfo --account=default` #### 3. "Mail still slow" - msmtp should be much faster than sendmail - Check network latency to SMTP server - Verify SMTP server is responding quickly - Monitor logs for connection issues #### 4. "Emails not being received" - Check spam/junk folders - Verify MAIL_FROM_ADDRESS is valid and matches SMTP account - Check SMTP server logs - Ensure proper SPF/DKIM records if using custom domain - Test with command line: `echo "Test" | msmtp recipient@example.com` #### 5. "msmtp not found" - Rebuild container: `docker-compose build --no-cache` - Check if msmtp was installed: `docker-compose exec app which msmtp` ### Verify Configuration Check if msmtp is properly configured: ```bash # Enter the container docker-compose exec app bash # Check msmtp configuration cat /etc/msmtprc # Test msmtp server connection msmtp --serverinfo --account=default # Send test email via command line echo -e "Subject: Test\n\nTest message" | msmtp test@example.com # Check msmtp version msmtp --version ``` ## Additional Recommendations 1. **Use a Dedicated SMTP Service**: Consider services like SendGrid, Mailgun, or Amazon SES for production 2. **Monitor Email Logs**: Regularly check `/var/log/msmtp/msmtp.log` for delivery issues 3. **Set Up Email Templates**: Create consistent email templates for better user experience 4. **Implement Email Rate Limiting**: Prevent spam and reduce server load 5. **Enable Detailed Logging**: msmtp provides comprehensive logging by default 6. **Test Regularly**: Use the provided test script to verify email functionality ## Security Considerations 1. **Secure Credentials**: Never commit SMTP passwords to version control 2. **Use Environment Variables**: Store all sensitive configuration in environment variables 3. **Enable TLS/SSL**: Always use encrypted connections for SMTP 4. **Limit Relay Access**: Ensure sendmail only accepts mail from localhost 5. **Regular Updates**: Keep sendmail and related packages updated ## Production Deployment For production environments: 1. Use a reliable SMTP service (not Gmail for business use) 2. Set up proper DNS records (SPF, DKIM, DMARC) 3. Monitor email delivery rates 4. Implement proper error handling and logging 5. Consider implementing an email queue system for high-volume scenarios ## What Changed This updated configuration replaces the complex sendmail setup with msmtp: - **Removed**: sendmail, sendmail-cf, complex m4 configuration - **Added**: msmtp (lightweight SMTP client) - **Simplified**: Single configuration file `/etc/msmtprc` - **Improved**: Better error handling and logging - **Faster**: Direct SMTP connection without queue management The key benefits: - **Performance**: 90%+ reduction in email sending time - **Reliability**: Fewer configuration points of failure - **Debugging**: Clear, separate logs for troubleshooting - **Compatibility**: Works with existing PHP `mail()` function calls This configuration should significantly improve email performance and reliability in your OpenDocMan Docker environment.