While it is possible to create an environment variable SENDER_ADDRESS
and access that in a controller, the address is not available in a functional test. This is due to the \tests
directory being at the same level as \src
. In an environment service EnvService
:
use Symfony\Component\Dotenv\Dotenv;
class EnvService
{
public function mailSender() {
$dotenv = new Dotenv();
$dotenv->load('../.env');
$sender = getenv('SENDER_ADDRESS');
return $sender;
}
}
then in a controller:
public function invite(Request $request, \Swift_Mailer $mailer, EnvService $env)
{
$invite = new Invitation();
$form = $this->createForm(InvitationType::class, $invite);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$invitation = $request->request->get('invitation');
// $sender = $this->get('mailer.sender_address');
$sender = $env->mailSender();
...
the email will be sent.
But in a functional test this error occurs:
Unable to read the "../.env" environment file
If instead swiftmailer.yaml
is modified to includesender_address: 'admin@bogus.info'
the error
Service "swiftmailer.sender_address" not found: the container inside "App\Controller\RegistrationController" is a smaller service locator
I've tried multiple variations on the theme of adding a SwiftMailer service in services.yaml
without success. The most recent iteration is:
swiftmailer:
class: 'Swift_Mailer\Swift\Mailer'
sender_address: 'admin@bogus.info'
public: true