DEV Community

Anders Björkland
Anders Björkland

Posted on

Configure Email over SMTP with SilverStripe

If you have used SilverStripe CMS, a lot of it may seem intuitive to you. But as a newcomer there may be a few things you might wish there were more documentation on. For me, one of those things was how to configure the CMS to use my mailtrap.io account for my development environment. For today, that is what we will do.

Mailtrap is a service that catches all outbound mails from your application and let you view them on their website. You will see a mail as in most mail clients, with the addition of switching views between different outputs: HTML, HTML-source, Text, and Raw. Suffice to say; this is not something that would be a good idea to use on a production-server. Mailtrap uses SMTP, so when we are configuring here for Mailtrap - the same configurations can be used for your SMTP service with updated values.

We will configure the following environment variables (be it in .env or "first class environment variables"):

SS_SEND_ALL_EMAILS_FROM="example@smtp.mailtrap.io"
APP_SMTP_USERNAME="example"
APP_SMTP_PASSWORD="password"
APP_SMTP_HOST="smtp.mailtrap.io"
APP_SMTP_PORT="2525"
Enter fullscreen mode Exit fullscreen mode

If you are using Mailtrap, you will find your SMTP credentials at https://mailtrap.io/inboxes/ and then click on "My Inbox" or the inbox you want to use for your specific project if you have multiple inboxes. Under "SMTP Settings" -> "Integrations", click on PHPMailer to get a pretty good idea on which values are which.

Having configured your environment variables, we will have to update our configurations as well. Open up app\_config\email.yml (or create it if it doesn't exist). We will update our email service to use SMTP:

---
Name: project-emailconfig
After:
  - '#emailconfig'
---

SilverStripe\Core\Injector\Injector:
  Swift_Transport:
    class: Swift_SmtpTransport
    properties:
      Host: '`APP_SMTP_HOST`'
      Port: '`APP_SMTP_PORT`'
      Encryption: tls
    calls:
      Username: [ setUsername, ['`APP_SMTP_USERNAME`'] ]
      Password: [ setPassword, ['`APP_SMTP_PASSWORD`'] ]
      AuthMode: [ setAuthMode, ['login'] ]
Enter fullscreen mode Exit fullscreen mode

Having updated SilverStripe like this, it may be a good idea to visit the dev/build URI (or run ?flush) to update the application to use it. This was appareantly a step that I missed.

Discussion (0)