Skip to content

Symfony Amazon SES Mailer does not work(Authentication issue) #36956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
neok opened this issue May 25, 2020 · 19 comments
Closed

Symfony Amazon SES Mailer does not work(Authentication issue) #36956

neok opened this issue May 25, 2020 · 19 comments

Comments

@neok
Copy link

neok commented May 25, 2020

Symfony version(s) affected: Symfony 5.0.8

Description
Symfony Amazon SES mailer not sending emails.
Deprecated signature is used, we should migrate to Signature Version 4 signing process

Unable to send an email: The security token included in the request is invalid. (code 0).

Response from amazon

   +"Type": "Sender"
    +"Code": "InvalidClientTokenId"
    +"Message": "The security token included in the request is invalid."

it seems like they do not support V3 Authentication anymore, or something else. I tried with multiple different credentials and still, it does not work.

How to reproduce

        $email = (new Email())
            ->from('hello@mydomian.com')
            ->to(new Address('myemail@gmail.com'))
            ->subject('Thanks for signing up!')

            ->text('Hello')
            ->html('Hello');

        $this->mailer->send($email);

Config

framework:
    mailer:
        dsn: 'ses+api://KEY:SECRET@default?region=eu-central-1'

Possible Solution
Change Authentication to v4

Additional context
I tried to send emails with
amazon PHP sdk

       $this->client = new SesClient([
            'credentials' => [
                'key' => 'KEY',
                'secret' => 'SECRET'
            ],
            'version' => 'latest',
            'region'  => 'eu-central-1'
        ]);

it does work, they are using V4 authorization(for more than two years).

@stof
Copy link
Member

stof commented May 25, 2020

Symfony 5.1 already includes a new implementation of the SES transport, which uses the v4 signature rather than the v2 one.

So I'm closing this issue as being already solved by the next Symfony version.

@stof stof closed this as completed May 25, 2020
@stof
Copy link
Member

stof commented May 25, 2020

Note that support for the v2 signature depends on the region. Some newer regions support only v4.

@lnorby
Copy link

lnorby commented Jul 17, 2020

I tried it with Symfony 5.1 and got the same error. I checked the source code and it seems to me that it still uses the v2 signature.
$auth = sprintf('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s', $this->accessKey, $this->getSignature($date));

@neok
Copy link
Author

neok commented Nov 12, 2020

If you still facing this issue, you can fix it by installing a new lib composer require async-aws/ses
and use ses+api
But make sure to urlencode any special characters that you have in your secret key.

@ingfdoaguirre
Copy link

If you still facing this issue, you can fix it by installing a new lib composer require async-aws/ses
and use ses+api
But make sure to urlencode any special characters that you have in your secret key.

This is the best solution

@kundukundu
Copy link

This worked for me:
ses+smtp://$AWS_MAILER_USER:$WS_MAILER_PASSWORD@default?region=eu-west-1
using symfony/amazon-mailer and after urlencode the password.

@719media
Copy link

since symfony 4.4 is still the last LTS, shouldn't this issue be resolved for symfony/amazon-mailer 4.4 ?

@719media
Copy link

Sorry to blow up the closed thread, but the reason I commented above is because amazon is currently phasing out signature 2 on SES by March 27 or so, so this current LTS 4.4 will NO LONGER WORK with ses, so I would think it should be fixed in 4.4

@fabpot
Copy link
Member

fabpot commented Feb 25, 2021

Unfortunately, there is no way we can fix it in 4.4 as we never add a new feature in patch releases (and supporting this new feature is non trivial anyway). So, the only possibilities I see here is to upgrade to a newer version of Symfony or to switch to another email provider (or maybe try to upgrade only symfony/amazon-mailer to 5.2).

@719media
Copy link

OK.
I have upgraded ONLY symfony/amazon-mailer to 5.2, even though it seems iffy based on version pinning, it worked for my application. Thank you for the reply.

OskarStark added a commit to symfony/symfony-docs that referenced this issue Mar 16, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

add a warning about Amazon SES signature

We need to warn users of Symfony 4.4 that they can't use symfony/amazon-mailer anymore.
They need to upgrade to 5.1 or newer.
See symfony/symfony#36956 (comment)

Commits
-------

518926f add a warning about Amazon SES signature
@garak
Copy link
Contributor

garak commented May 6, 2021

Sorry to bump old issue, but I just hit this problem again (even if I have a similar configuration working).
So, is there something else I can check, after the following?

  • Symfony 4.4
  • symfony/amazon-mailer v5.2.0
  • async-aws/ses 1.4.0
  • properly configured env vars, with credentials checked in a smtp test tool, password urlencoded

The error is the infamous "The request signature we calculated does not match the signature you provided" if I use ses:// or ses+api:// prefix, or "535 Authentication Credentials Invalid" if I use ses+smtp://

The only difference I can see for config is that I have a transports key under framework.mailer (with a couple of named configuration, all pointing to configured env vars) instead of dsn key, can this be somehow relevant?

@derrabus
Copy link
Member

derrabus commented May 6, 2021

@garak Can you please bump amazon-mailer to the latest 5.2 release?

@garak
Copy link
Contributor

garak commented May 7, 2021

@derrabus thanks for your suggestion: tried with amazon-mailer 5.2.6, no luck :-(

@jderusse
Copy link
Member

jderusse commented May 7, 2021

If you use DSN, could you please check that the credentials are encoded?

AWS, often generates KEYs and SECRETs with special chars like + and /. When you copy-paste these credentials in the DSN (ie. ses://mykey:my+secret@default) the + is url_decoded and interpreted like a space ( ) as a result the credential used to signs the payload is invalid.

@garak
Copy link
Contributor

garak commented May 7, 2021

@jderusse already mentioned above: "password urlencoded"

@jderusse
Copy link
Member

jderusse commented May 7, 2021

Having "535 Authentication Credentials Invalid" if I use ses+smtp:// let me think this is related to the env var or credentials and not only the amazon-mailer bridge

After using the SMTP transport in debug mode, the SentMessage object contain a debug property that should contain all the command executed, maybe it can help to troubleshoot what's going on

@garak
Copy link
Contributor

garak commented May 7, 2021

I now tried again with ses+smtp://, but forcing region by appending ?region=eu-central-1. It's working for now, but I', not sure if I can keep it like so (or if it's better using ses+api://)

@mpoiriert
Copy link

Just in case someone as issue this is what I am doing (in pseudo codish way).

$SES_SECRET = urlencode(exec("(echo -n \"\x02\"; echo -n 'SendRawEmail' | openssl dgst -sha256 -hmac \${SES_SECRET} -binary) | openssl enc -base64"));

MAILER_DSN= sprintf(
        'smtp://%s:%s@email-smtp.%s.amazonaws.com:587?encryption=tls&auth_mode=login',
        urlencode(getenv('SES_SMTP_ACCESS_KEY')),
        $SES_SECRET,
        getenv('REGION')
    )

Since it's using simple smtp transport you don't need to install the symfony/amazon-mailer

There might be some things specific to our infra (I am not handling it) but this would give a decent starting point

@msklenica
Copy link

This worked for me: ses+smtp://$AWS_MAILER_USER:$WS_MAILER_PASSWORD@default?region=eu-west-1 using symfony/amazon-mailer and after urlencode the password.

The missing puzzle piece was urlencode the password, as it contained special char "/".

@xabbuh xabbuh added the Mailer label Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests