safiullah's avatar

Sending emails via laravel. Help needed

I am learning laravel but I am stuck on email part. I want to know how to send emails for registration and other stuff. Mailgun seemed a good option but it doesn't offer free tier now. So my question is should I use PHPMailer or inbuilt laravel swift mail. I am so confused. Can anyone guide me regarding email via PHP comprehensively?

0 likes
6 replies
LaryAI's avatar
Level 58

Both PHPMailer and Laravel's built-in Swift Mailer are good options for sending emails. However, Laravel's built-in mail functionality is easier to use and requires less setup.

To use Laravel's built-in mail functionality, you need to configure your email settings in the .env file. Here's an example:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls

Once you've configured your email settings, you can use Laravel's Mail facade to send emails. Here's an example:

use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;

Mail::to($user->email)->send(new WelcomeEmail($user));

In this example, we're using the Mail facade to send a welcome email to a user. We're passing in the user's email address and a WelcomeEmail object, which is a custom email class that we've created.

Here's an example of what the WelcomeEmail class might look like:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->view('emails.welcome');
    }
}

In this example, we're defining a WelcomeEmail class that extends Laravel's Mailable class. We're passing in the user object to the constructor, and we're defining a build method that returns the view for the email.

Finally, we're creating a view file for the email at resources/views/emails/welcome.blade.php. Here's an example of what that file might look like:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Site</title>
</head>
<body>
    <h1>Welcome, {{ $user->name }}!</h1>
    <p>Thanks for signing up for My Site. We're excited to have you on board!</p>
</body>
</html>

This view file contains the HTML for the email, and we're using the $user variable to personalize the email for each user.

That's a basic overview of how to send emails in Laravel. There are many more advanced features you can use, such as sending attachments, using email templates, and sending emails asynchronously with queues. But this should be enough to get you started.

Beptum's avatar

Hi, Mailgun still can be used for free. There is FLEX plan, which is for free but you are limited with 1000 emails per month. Another option is using some other SMTP email service such as Gmail. Then you have to set you SMTP config: MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=<Enter your Gmail address> MAIL_PASSWORD=<Enter your Gmail password> MAIL_ENCRYPTION=ssl And dont forget to configure Google account settings to allow sending emails from gmail .

safiullah's avatar

@Beptum I didnt any find any reference to it on mailgun. All they have is a one month foundation trial which requires credit card info

Snapey's avatar

Surely you would want your emails to come from the same domain as your website domain name? EG not a gmail address.

So, at low scale, your choices are either a) your domain name registrar, or b) your web hosting company. You then use the local mailer configured to connect to the SMTP service of your domain provider or your hosting company.

Alternatively use a third party like sendgrid, it really depends how many emails you need to send per month

Please or to participate in this conversation.