Switch sendmail Parameters in symfony/mailer when using symfony/messenger - symfony

The local Development Machine uses msmtp for sending Mails, which works fine. BUT it does not accept /usr/sbin/sendmail -bs, it only works with /usr/sbin/sendmail -t. I can see that I could change this when using other parameters for the SendmailTransport constructor, but actually I never create an Instance of SendmailTransport - the consumer of the Messenger does this all alone.
How can I change the Sendmail Parameters when using async Mails via Messenger?

MessageHandler receives a TransportInterface as a dependency in order to send the messages, so indeed you can change it via configuration. Since once messenger is installed all messages will be sent with it, you can just override the default transport directly and it will be injected into the Handler:
# config/services_dev.yaml
services:
Symfony\Component\Mailer\Transport\TransportInterface:
class: Symfony\Component\Mailer\Transport\SendmailTransport
arguments:
- '/usr/sbin/sendmail -t'

Related

Browser client can't connect to DynamoDB in Docker container

I've been using #aws-sdk/client-dynamodb server-side (SvelteKit / NodeJS) connecting to localhost Docker container with instance of amazon/dynamodb-local:latest which works well. I used AWS CLI to configure tables, etc. I've created the client using the simplest configuration:
const client = new DynamoDBClient({ endpoint: 'http://localhost:8000' });
This works server-side, but when the same is executed client-side along with a command, I get a message that the region is missing. I've tried passing region: 'none', but then I get a message that the credentials are missing. Adding dummy credentials enables the command to execute, but I don't get an expected response. For example, sending the ListTablesCommand returns an empty array. If I do the same from the AWS CLI, I get the correct response.
Does the DynamoDB client run client-side, i.e., in the browser? Or am I missing something else?
No it doesn't run in a browser, You will need API Gateway and some backend code to connect a browser to Dynamodb.

Mailhog, Trapmailer and gmail via symfony

I got some trouble with mails generation on my Symfony app :
I use docker
when I use Gmail to send emails :
MAILER_URL=gmail://MyAddress#gmail.com:MyPw#localhost
everything works fine
but as soon as I try to intercept these mails :
======================
Like this for mailtrap
MAILER_DSN=smtp://b3f077aeceaeb6:8792a1811dd8ef#smtp.mailtrap.io:2525?encryption=tls&auth_mode=login
MAILER_URL=smtp://smtp.mailtrap.io:25&auth_mode=login&username=b3f077aeceaeb6&password=8792a1811dd8ef
or like this for mailHog
MAILER_DSN=smtp://localhost:1025
MAILER_URL="smtp://mailhog:1025?encryption=ssl&auth_mode=login&username=null&password=null"
my docker-compose.yml looks like this :
mailhog:
container_name: mailhog
restart: always
image: mailhog/mailhog:latest
ports:
-'8025:8025'
-'1025:1025'
Does anyone know how to see the mails displayed in mailhog? mailhog page is loaded correctly but nothing shows in it.
The function sending the mail is entered, so this must be a configuration problem in my opinion
Based on your post, I had a similar problem with mailhog and SwiftMailer for Symfony.
I ended up using the smtp for both MAILER_DSN and MAILER_URL as follows
MAILER_DSN=smtp://localhost:1025
MAILER_URL="smtp://localhost:1025?auth_mode=login"
As you can see the trick was to remove encryption=ssl as the mailhog does not supports it and the username=null&password=null as the software sends it as literal string "null".
Besides, in my swiftmailer.yaml and mailer.yaml I have this very simple configuration
# swiftmailer.yaml
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
# mailer.yaml
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
You need to narrow down the issue.
1. Can you send you an email via telnet or openssl (in case of STARTTLS)?
Via telnet: https://mediatemple.net/community/products/dv/204404584/sending-or-viewing-emails-using-telnet
Via OpenSSL: https://www.stevenrombauts.be/2018/12/test-smtp-with-telnet-or-openssl/
Go through the tutorial (or any on this topic) and make sure your MAILER_URL is correct (including credentials, port, etc). The last time I had some issue with email, turned out that the firewall was blocking me.
Don't be quick to rule out networking issues.
2. Once you confirmed that you can send an email via Telnet/OpenSSL:
you need to go level up and try to configure the URL in Symfony and attempt sending via swiftmailer:send command. This may reveal some odd configuration issues in Symfony and you can fix them accordingly
3. Repeat (1) and (2) but from Docker container.
Just exec into Docker and do it all over again. This may reveal some issues in Docker network configuration, which you can fix.
Please update your question with more findings and we could probably help you further...

Symfony Messenger does not consume messages until stop/consume

On Symfony 5.1, I created two very basic message classes.
The first updates the database and then it calls the other to send a mail via SwiftMailer.
Both of them arrive to the consumer command as they are logged by the bin/console messenger:consume async -vv command, but while the class that updates the database is handled, the other remains suspended.
The fact is that if I stop and restart the consume command, this message becomes handled by the consumer and so the mail is sent.
I have tried with Redis and RabbitMQ transports, with the very same results.
This is the messenger configuration:
framework:
messenger:
transports:
async:
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
routing:
'App\Message\FollowRequestMessage': async
'App\Message\EmailMessage': async
This is the MESSENGER_TRANSPORT_DSN in .env:
MESSENGER_TRANSPORT_DSN=amqp://guest:guest#127.0.0.1:5672/%2f/messages
UPDATE
I defined another transport to handle the FollowRequest and Email messages separately, by duplicating the "async" transport config in the yaml messenger configuration.
This has fixed the issue, but I don't know if it the right way to handle the case.
So, it is correct to create a different transport for each kind of message? Or is it a workaround because of an error in something else?

Find out if you are running in a command and the command parameters in Symfony

We have a service that can be called from a Symfony command and from a normal web request. Is there a way to find out if the service was called from a command or from a web request? If so, if it was called from a command, is there a way to find out the parameters that were used when running the command?
In symfony Console,
the command line context does not know about your VirtualHost or domain name
It means that you can check the request scheme, host, base_url and base path since these request properties have no values in the console context unless you configure them (https://symfony.com/doc/current/console/request_context.html#configuring-the-request-context-globally)
Hi you can use this to know if the service is used from the cli, if it runs with apache you will get this apache2handler
if(php_sapi_name() === 'cli') {
//some code
}
https://www.php.net/manual/en/function.php-sapi-name.php

Consumer Error Handling in Symfony Messenger / RabbitMQ

I'm using the new Symfony Messenger Component 4.1 and RabbitMQ 3.6.10-1 to queue and asynchronously send email and SMS notifications from my Symfony 4.1 web application. My Messenger configuration (messenger.yaml) looks like this:
framework:
messenger:
transports:
amqp: '%env(MESSENGER_TRANSPORT_DSN_NOTIFICATIONS)%'
routing:
'App\NotificationBundle\Entity\NotificationQueueEntry': amqp
When a new notification is to be sent, I queue it like this:
use Symfony\Component\Messenger\MessageBusInterface;
// ...
$notificationQueueEntry = new NotificationQueueEntry();
// [Set notification details such as recipients, subject, and message]
$this->messageBus->dispatch($notificationQueueEntry);
Then I start the consumer like this on the command line:
$ bin/console messenger:consume-messages
I have implemented a SendNotificationHandler service where the actual delivery happens. The service configuration:
App\NotificationBundle\MessageHandler\SendNotificationHandler:
arguments:
- '#App\NotificationBundle\Service\NotificationQueueService'
tags: [ messenger.message_handler ]
And the class:
class SendNotificationHandler
{
public function __invoke(NotificationQueueEntry $entry): void
{
$this->notificationQueueService->sendNotification($entry);
}
}
Until this point, everything works smoothly and the notifications get delivered.
Now my question: It may happen that an email or SMS cannot be delivered due to a (temporary) network failure. In such a case, I would like my system to retry the delivery after a specified amount of time, up to a specified maximum number of retries. What is the way to go to achieve this?
I have read about Dead Letter Exchanges, however, I could not find any documentation or example on how to integrate this with the Symfony Messenger Component.
What you need to do is tell RabbitMQ, that the message is rejected instead of acknowledged. By default the messenger will take care of this inside the AmqpReceiver. As you can see there, if you throw an exception that implements the RejectMessageExceptionInterface inside your handler, the message will automatically be rejected for you.
You could also "simulate" this behaviour with custom middleware. I created something like it, in a small demo application. The mechanism consists of a middleware that wraps the (serialized) original message inside a new RetryMessage and sends it via a custom message bus to a different queue, used as a dead letter exchange. The handler for that message will then unpack the RetryMessage (getting the original message and deserializing it) and transmit it over the default bus:
See:
RetryMessage
RetryMiddleware
messenger.yaml
RetryMessageHandler
This is a basic setup which rejects the message and allows you to consume it again instantly(!). You probably want to add additional information such as headers for timestamps when delaying the consumption to improve on this. For this you should look at writing your own receiver, middleware and/or handler.

Resources