Sending Mail
Rhapsody provides a simple, powerful mailer service built on top of the robust symfony/mailer component. This allows you to easily send emails for notifications, confirmations, and more.
Configuration
All mailer configuration is handled in your .env file. You can configure your SMTP host, port, username, and password. For local development, a service like Mailtrap is highly recommended to safely test your emails in a "fake" inbox.
# .env
MAIL_TRANSPORT="smtp"
MAIL_HOST="sandbox.smtp.mailtrap.io"
MAIL_PORT="2525"
MAIL_USERNAME="your-username"
MAIL_PASSWORD="your-password"
MAIL_FROM_ADDRESS="no-reply@rhapsody.test"
MAIL_FROM_NAME="Rhapsody Framework"
Sending an Email
The easiest way to send mail is to inject the Rhapsody\Core\Mailer service into your controller's constructor via dependency injection. Then, call the send() method.
use Rhapsody\Core\Mailer;
use Rhapsody\Core\Request;
class ContactController extends BaseController
{
public function __construct(protected Mailer $mailer) {}
public function submit(Request $request): Response
{
// ... validation ...
try {
$this->mailer->send('admin@example.com', 'New Contact', 'Message
');
return redirect('/contact')->with('success', 'Message sent!');
} catch (\Exception $e) {
// Log error
return redirect('/contact')->with('error', 'Could not send message.');
}
}
}
Important: If your
.env file is missing MAIL_HOST (or it is empty), the Mailer service will throw a RuntimeException when you try to send an email. Always configure your mail settings before using the mailer in production.