@aubrey
To send emails using Gmail in Symfony 4, you need to configure the Swift Mailer library to use Gmail's SMTP server. Here are the steps:
- Install Swift Mailer via Composer:
composer require symfony/swiftmailer-bundle
- Configure Swift Mailer in the config/packages/swiftmailer.yaml file:
swiftmailer:
transport: smtp
host: smtp.gmail.com
port: 465
encryption: ssl
username: '[email protected]'
password: 'your-password'
- Configure the emailer service in the config/services.yaml file:
services:
_defaults:
autowire: true
autoconfigure: true
AppYourEmailerClass:
- Create a new class to handle email sending, for example src/YourEmailerClass.php:
mailer = $mailer;
}
public function sendEmail()
{
$email = (new Email())
->from('[email protected]')
->to('[email protected]')
->subject('Hello!')
->text('Body of the message');
$this->mailer->send($email);
}
}
- Use the emailer service wherever you need to send an email:
use AppYourEmailerClass;
// ...
public function sendEmail(YourEmailerClass $emailer)
{
$emailer->sendEmail();
}
Remember to replace '[email protected]'
and 'your-password'
with your actual Gmail email and password in the configuration file (swiftmailer.yaml
). Also, ensure that you have enabled "Less secure apps" in your Gmail account settings if you encounter any authentication issues.
With this configuration, you should be able to send emails using Gmail in Symfony 4 using the Swift Mailer library.