I am new to Laravel and I want to send an email. I don’t want to do a complex setup. Can someone please explain how I can send a simple email in Laravel? I need to know the easiest way to email without getting into too many details.
You can use the built-in Mail
class. Laravel already has mail functionality built-in. All you need to do is configure your mail settings in the .env
file.
Here is an example
MAIL_MAILER=smtp
MAIL_HOST=live.smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=<smtpuser>
MAIL_PASSWORD=<password>
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="admin@demomailtrap.com"
MAIL_FROM_NAME="Test User"
You can get your free SMTP
mail server details from mailtrap.io
Now, you can send the email from anywhere in your application, like a controller or route, using this code:
try {
Mail::raw('This is a simple test email.', function ($message){
$message->to('testuser@example.com')->subject('Test Email');
});
return "Email Sent!";
} catch (Exception $e) {
return $e->getMessage();
}