How to configure MAILER_URL in .env file of Symfony 4 to send e-mails via sendmail with Swift_Mailer?
Asked Answered
M

4

6

I am working on a Symfony 4 app using Swift_Mailer to send e-mails with. Since there is no possibility in my case to use SMTP (don't ask why…) I have to use something like sendmail.

By default the config of Swift Mailer is done in Symfony's .env file in URL notation in an option named MAILER_URL. The default value is "null://localhost" which doesn't send mails at all.

All I could find for what the value has to be is an example for Gmail or for SMTP in general as documented in the Symfony docs as well as in the sample .env as generated by Composer.

Default content of .env:

# …

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

# …

What I don't know and therefore what my question is:

What do I need to do to make sendmail work with this?

I already tried something like:

MAILER_URL=sendmail://localhost

and

MAILER_URL=sendmail://my-domain.com

but without success so far.

When using the console command

bin/console swiftmailer:email:send

I even get an "[OK] 1 emails were successfully sent." as result, but in fact no e-mail is sent.
(… No spooling, by the way. bin/console swiftmailer:spool:send returns "0 emails sent".) Mail delivery seems to be interupted or so, since the mails won't arraive at my mail account, also my SPAM is empty.

Directly invoking the sendmail command on the other hand does work. (My test mails arrive at my SPAM though, but still: The mails are sent.)

Again, how do I have to configure the MAILER_URL for Swift_Mailer in my .env in Symfony 4 to use sendmail?

Is it possible at all?

Moua answered 23/7, 2018 at 16:25 Comment(1)
i've found this on symfony 2.Try to adapt to .env variables !Paulettapaulette
M
8

Alright, the notation already was correct. So this one is valid for using sendmail:

MAILER_URL=sendmail://my-domain.com

My actual problem was that spooling of mails was active. I commented out the spool entry from my swiftmailer.yaml config.

Having a look into the Symfony Profiler helped me a lot here, by the way.

… My mails are still not arriving, but I am sure this has nothing to do with the MAILER_URL. So, my question is answered.

Moua answered 24/7, 2018 at 11:43 Comment(3)
As soon as I edit "switftmailer.yaml" all pages with contact forms go blanc. Could somebody elaborate how to do?Basanite
@Basanite Maybe you should ask new question for that. To me it sounds like it could be … well … anything. Invalid YAML syntax, for instance. Or any other kind of misconfiguration. Blank pages however are pretty often just 500 Internal server errors. Those are usally logged. → Watch your error logs, please. I hope, this helps.Moua
Thanks for your answer. Yeah I should ask a new question. I spend many hours trying to integrate Symfony with Postfix. The servers log is irritatingly empty and I just removed the line from the yaml file which was the last line in there anyways. I am a bit in despair since I believe this is a task of probably an hour max and so far I spent many hours :( Enough lamentation for now. :)Basanite
O
2

commenting spool in yaml fixed issue for me

Opisthognathous answered 6/3, 2019 at 10:19 Comment(1)
As soon as I edit "switftmailer.yaml" all pages with contact forms go blanc. Could somebody elaborate how to do?Basanite
P
1

Try this config:

swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
transport: 'sendmail'
command: '/usr/sbin/sendmail -oi -t'

Path to file:

/my_sf4_project/config/packages/swiftmailer.yaml

I'm using Symfony 4.3.1 and SwiftMailer 6.2.0. You can read in SwiftMailer bundle documentation that:

/**
 * Start the standalone SMTP session if running in -bs mode.
 */

and then:

/**
 * Set the command to invoke.
 *
 * If using -t mode you are strongly advised to include -oi or -i in the flags.
 * For example: /usr/sbin/sendmail -oi -t
 * Swift will append a -f<sender> flag if one is not present.
 *
 * The recommended mode is "-bs" since it is interactive and failure notifications
 * are hence possible.
 *
 * @param string $command
 *
 * @return $this
 */

Path to file with these adnotations:

vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SendmailTransport.php

Before changing into '/usr/sbin/sendmail -oi -t' i was receiving error from SwiftMailer:

app.ERROR: Exception occurred while flushing email queue: Expected response code 220 but got an empty response [] []

After changing sendmail parameters now i'm sending mails successfully.

Packer answered 21/8, 2019 at 13:9 Comment(1)
Of course my .env config is: MAILER_URL=sendmail://my-domain.comPacker
Y
0

Not sure I got your problem correctly but in my case I use a Gmail account and here is the 23rd line of my .env file

MAILER_URL=gmail://[email protected]:myemailpassword@localhost

Have you already installed the SwiftMailer by composer require symfony/swiftmailer-bundle ? (Sorry for my english)

Yeorgi answered 23/7, 2018 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.