Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 878
Arvid Avatar asked Jul 23 '18 16:07

Arvid


People also ask

How to send Mail in Symfony?

Sending a message in Symfony env file. To send a message, we need to use the Mailer class. In this example we are including both HTML and plain text parts, adding a PDF file, and embedding an image: // src/Controller/MailerController.

What is Symfony mailer?

Symfony's Mailer & Mime components form a powerful system for creating and sending emails - complete with support for multipart messages, Twig integration, CSS inlining, file attachments and a lot more. Get them installed with: $ composer require symfony/mailer.

What is Mailer in email?

1 : one that mails. 2 : a container for mailing something. 3 : something (such as an advertisement) sent by mail.

What is swift mailer?

Swift Mailer is a component based library for sending e-mails from PHP applications.


3 Answers

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.

like image 175
Arvid Avatar answered Sep 18 '22 16:09

Arvid


commenting spool in yaml fixed issue for me

like image 24
Stipic Avatar answered Sep 21 '22 16:09

Stipic


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.

like image 38
mxmar Avatar answered Sep 17 '22 16:09

mxmar