Sending email using office365 server with swiftmailer in symfony
Asked Answered
C

5

8

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.

Coquillage answered 10/9, 2019 at 8:53 Comment(2)
Did you check if the mailer url is parsed correctly? Since you mentioned the double quotes, I can imagine that they might be escaped at some point.Bacciferous
@Bacciferous I don't think double quotes are making a difference, I get the same error when I remove them and encode special charactersCoquillage
F
10

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
Family answered 9/10, 2019 at 15:31 Comment(1)
This did not work for me in 2023, but it pointed me in the right direction. The urlencode and GET params are key here. Thanks! So my DSN connection string looks like this now: smtp://USERNAME:[email protected]:587?encryption=tls&auth_mode=loginSiderosis
I
2

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
Impasse answered 4/1, 2021 at 15:32 Comment(0)
B
1

Try this:

MAILER_URL=smtp://smtp.office365.com:587?encryption=tls&username="[email protected]"&password="mypassword"
Bunche answered 10/9, 2019 at 17:37 Comment(6)
No luck with this combination. I get Failed to authenticate on SMTP server with username "[email protected]" using 1 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535 5.7.3 Authentication unsuccessful [VI1PR02CA0063.eurprd02.prod.outlook.com] ".Coquillage
@Mario are you sure of using [email protected], and not [email protected] ?Bunche
I've tried every possible combination both username and email address :(Coquillage
@MarioKlisanic Caution If the username, password or host contain any character considered special in a URI (such as +, @, $, #, /, :, *, !), you must encode them. See RFC 3986 for the full list of reserved characters or use the urlencode function to encode them. symfony.com/doc/4.2/email.html#configurationBunche
Try encoding $username/password like urlencode ( $username ) and copy in .env the resultBunche
like I said in a comment on my original post, I've already tried encoding my username/password and it doesn't solve the issue. Thanks anywaysCoquillage
V
1

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.

Viridescent answered 17/6, 2020 at 15:8 Comment(0)
M
1

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)%'
Monde answered 20/10, 2023 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.