Sending emails in Silverstripe 5

Silverstripe Version: 5

Question:

I have just updated an old silverstripe 3 site to silverstripe 5. Everything is good except that it does not send emails. I have tried adding the following .env variables:

MAILER_DSN="smtp://user@domain.com:password@mail.domain.com:465"
MAILER_DSN="smtp://user@domain.com:password@smtp.domain.com:587"
MAILER_DSN="sendmail://default"

I have run dev/build?flush=all after each change. At best I get error messages and at worst it is an internal server error and nothing gets written to the error log.

I have also tried adding a mailer-project.yml file with the following in it:

---
Name: mailer-project
After: 'mailer'
---

SilverStripe\Core\Injector\Injector:
  Symfony\Component\Mailer\Transport\TransportInterface:
    constructor:
      dsn: '%env(MAILER_DSN)%'

The error messages I have gotten are:

# [Emergency] Uncaught Symfony\Component\Mailer\Exception\InvalidArgumentException: The mailer DSN must contain a scheme.

ERROR: Uncaught Exception Error: "Call to undefined function Symfony\Component\Mailer\Transport\Smtp\Stream\proc_open()" at /home/mcscomau/public_html/vendor/symfony/mailer/Transport/Smtp/Stream/ProcessStream.php line 41 {"exception":"[object] (Error(code: 0): Call to undefined function Symfony\\Component\\Mailer\\Transport\\Smtp\\Stream\\proc_open() at /home/.../public_html/vendor/symfony/mailer/Transport/Smtp/Stream/ProcessStream.php:41)"} []

What am I missing? I have not used Silverstripe much in the last year or two, so I am a bit rusty with it all.

Thanks

I don’t know where you got the syntax for '%env(MAILER_DSN)%' but to use an environment variable with injector configuration the syntax is '`MAILER_DNS`'
See https://docs.silverstripe.org/en/5/developer_guides/extending/injector/#using-constants-and-environment-variables

The %env bit came out of Symfony docs.

I could not get it to work with the .env variable. Anytime I loaded the .env file up with a MAILER_DNS variable it wound up causing an internal server error.

So I put the full smtp://user@domain.com:password@mail.domain.com:465 in the yml file and set a SS_SEND_ALL_EMAILS_FROM variable in the .env file.

For anyone else that might have this issue, it is also worth ensuring the password does not contain special characters.

May not be useful exactly to the OP but maybe someone somewhere…

How I do it with MailGun API and Docker compose is simply:

MAILER_DSN: "mailgun+api://key-<your_key>:<your_address>@default"

In the environment section of the docker-compose file.

Swap <your_key> and <your_address> for your values.