Mario Klisanic
Mario Klisanic

Reputation: 607

Sending email using office365 server with swiftmailer in symfony

I'm trying to send an confirmation email after registration using swiftmailer and office365 server in symfony. I've tried every combination of host,port and encryption type I've come across.

Currently my .env file contains this line:

MAILER_URL=smtp://smtp.office365.com:587?encryption=ssl&auth_mode=login&username="[email protected]"&password="mypassword"

*Note: I've used " " for my username and password since they contain special characters and I've read somewhere that this can cause problems with MAILER_URL.

My swiftmailer.yaml file containts this:

swiftmailer:
    url: '%env(MAILER_URL)%'
    stream-options:
        ssl:
            allow_self_signed : true
            verify_peer: false

Lastly, I'm sending my email using this code in my controller:

 $message = (new \Swift_Message('Referral tool registration'))
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody(
                $this->renderView(
                    'email/notification/user_registered.html.twig',
                    ['firstName' => $user->getFirstName(),
                     'lastName' => $user->getLastName()
                    ]
                    ),
                'text/html'
            );

 $mailer->send($message);

With the current choice of host,port and encryption I'm getting: "Connection could not be established with host smtp.office365.com [ #0]"

UPDATE: When I type in telnet smtp.office365.com 587 I get a valid response, so I suppose the problem is not network related, port is not blocked.

Upvotes: 8

Views: 14427

Answers (5)

moez cherif
moez cherif

Reputation: 101

In Symfony 4 this one it is work with me

#.env.local
MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&auth_mode=login&username=mymail%40outlook.com&password=MyPassword
# config/packages/mailer.yaml
framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

Upvotes: 1

tengopl
tengopl

Reputation: 463

I had similar problem, I solved it by logging into account and changing password.

My case - the account was new, and first screen after logging was with fields to input: current password, new pass, repeat pass.

My env:

MAILER_DSN=smtp://[email protected]:[email protected]:587

Upvotes: 2

Theva
Theva

Reputation: 948

Symfony 4 uses Mailer Component, if you are familier with then you can use an env variable as follows

# Office 365 Outlook
MAILER_DSN=smtp://[email protected]:[email protected]:587

AND config/packages/mailer.yml

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

The only transport that pre-installed is SMTP. no need of any other transport bundle unlike google-mailer or amazon-mailer..

it works for me.

Upvotes: 1

gondo
gondo

Reputation: 1128

You have to use encryption=tls, urlencode your USER_NAME and PASSWORD, and you should not wrap USER_NAME and PASSWORd in quotes as you did.

Let's assume these credentials and urlencode them:

USERNAME = `[email protected]` → `email%40domain.tld`
PASSWORD = `pa$$w#rd` → `pa%24%24w%23rd`

Correct config with above credentials will look like this:

MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&auth_mode=login&username=email%40domain.tld&password=pa%24%24w%23rd

Upvotes: 10

Manzolo
Manzolo

Reputation: 1959

Try this:

MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&username="[email protected]"&password="mypassword"

Upvotes: 1

Related Questions