Sending and Receiving Emails in Laravel Applications
September 24, 2024 by Vincent Schmalbach
Implementing email functionality is far from straightforward. The right solution depends on your specific needs, user base, and resources.
Sending and Receiving
Sending and receiving emails are two separate concerns.
Sending Emails
The Basics: SMTP
At its core, sending emails typically relies on the SMTP (Simple Mail Transfer Protocol).
Your email hosting company provides you with SMTP credentials:
SMTP server address
Username
Password
These are the same credentials you'd use in email clients like Outlook or Thunderbird. In Laravel, you can use these credentials to send emails.
Gmail and Outlook
Here's where things get tricky. Most people today use Gmail (or Google Workspace) or Outlook/Office 365. These providers have made developers' lives significantly harder.
Gmail Challenges:
No more simple SMTP credentials
Multiple access methods, each with drawbacks:
Registering as a public app:
Requires a thorough security audit
Time-consuming and expensive
Must pass several major security topics
Application passwords:
Only available for users with two-factor authentication
Complicated process, especially for non-technical users
Outlook/Office 365 Challenges:
Need to register your app in Microsoft's Azure Directory
Implement OAuth2 workflow for user authorization
Users may need to enable SMTP in their accounts manually
The integration process for both these providers is labor-intensive and complex.
Specialized Email Providers
Services like Mailgun, Postmark, and SendGrid offer simpler solutions:
Pros:
Easy setup with API and/or SMTP access
Reliable delivery
Cons:
Domain configuration required for each sender domain
Primarily for transactional emails
Marketing emails may face deliverability issues
Strict opt-in requirements for recipients of marketing emails
Volume Considerations
The number of emails you send matters:
Low volume: Most hosting providers won't cause issues
High volume: Becomes challenging, especially for non-transactional emails
Receiving Emails
IMAP Basics
Receiving emails typically uses the IMAP (Internet Message Access Protocol). Your email host provides IMAP server credentials, similar to SMTP.
Challenges with IMAP:
Pull mechanism: Requires constant checking for new emails
Often slow and clunky
Resource-intensive for multiple user accounts
Gmail and Outlook Issues (Again)
The same OAuth2 and app password issues apply here as with sending emails.
Processing Email Content
Once you've received an email, you need to:
Parse the email content (which can be complex)
Filter out spam and auto-replies
Inbound Email Routing Services
Services like Mailgun and Postmark offer inbound routing:
Receive webhook with email content
Often provide parsed content
May include spam/autoreply detection
User setup typically involves email forwarding to a provided address.
The EmailEngine Solution
EmailEngine is a self-hosted tool that aims to simplify email integration:
Unified REST API for IMAP, SMTP, Gmail API, and Microsoft Graph API
Self-hosted solution
Priced at €895.00 / year
Pros:
Handles OAuth2 workflows
Provides a consistent API across providers
Cons:
Requires self-hosting
Still needs significant integration work
Doesn't eliminate all the challenges mentioned above