phppostfix-mtasymfony-mailer

Troubleshoting migration from swiftmailer to symfony mailer, errors


I upgraded to php 8.2 and from swiftmailer (which is archived) to the new recommended symfony mailer (without using symfony but as standalone)

I was creating swift mailer very simply like this

      $this->mailer = new Swift_Mailer(
                (new Swift_SmtpTransport(
                    env('MAIL_HOST'),
                    env('MAIL_PORT')
                ))
                    ->setUsername(env('MAIL_USERNAME'))
                    ->setPassword(env('MAIL_PASSWORD'))
            )

MAIL_PORT=25, simple smtp. Environmental variables are the same before and after library change.

My new code with symfony mailer is

            $this->mailer = new \Symfony\Component\Mailer\Mailer(
                (new \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport(
                    env('MAIL_HOST'),
                    (int)env('MAIL_PORT'),
                ))
                    ->setUsername(env('MAIL_USERNAME'))
                    ->setPassword(env('MAIL_PASSWORD'))
            ),

I try to send emails but they fail.

My mailserver logs are I changed the ip to x.x.x.secret and also sanitized some other data.


==> /var/log/syslog <==
Sep  7 16:10:21 ip-10-40-3-158 postfix/smtpd[4032884]: warning: hostname somehost.com does not resolve to address x.x.x.secret

==> /var/log/mail.log <==
Sep  7 16:10:21 ip-10-40-3-158 postfix/smtpd[4032884]: warning: hostname somehost.com does not resolve to address x.x.x.secret

==> /var/log/syslog <==
Sep  7 16:10:21 ip-10-40-3-158 postfix/smtpd[4032884]: connect from unknown[x.x.x.secret]

==> /var/log/mail.log <==
Sep  7 16:10:21 ip-10-40-3-158 postfix/smtpd[4032884]: connect from unknown[x.x.x.secret]

==> /var/log/syslog <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: SSL_accept error from unknown[x.x.x.secret]: -1

==> /var/log/mail.log <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: SSL_accept error from unknown[x.x.x.secret]: -1

==> /var/log/syslog <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: warning: TLS library problem: error:14094418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca:../ssl/record/rec_layer_s3.c:1543:SSL alert number 48:

==> /var/log/mail.log <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: warning: TLS library problem: error:14094418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca:../ssl/record/rec_layer_s3.c:1543:SSL alert number 48:

==> /var/log/syslog <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: lost connection after STARTTLS from unknown[x.x.x.secret]

==> /var/log/mail.log <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: lost connection after STARTTLS from unknown[x.x.x.secret]

==> /var/log/syslog <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: disconnect from unknown[x.x.x.secret] ehlo=1 starttls=0/1 commands=1/2

==> /var/log/mail.log <==
Sep  7 16:10:22 ip-10-40-3-158 postfix/smtpd[4032884]: disconnect from unknown[x.x.x.secret] ehlo=1 starttls=0/1 commands=1/2

The change was as simple as above. Created the mailer like that, then am sending it in php via

    /**
     * @param array $headers
     */
    public function sendRaw(string $content, string $subject, EmailAddress $from, EmailAddress $to, array $headers = []): void
    {
        $message = (new Email())
            ->subject($subject)
            ->from(new Address($from->getEmail(), $from->getName()))
            ->to($to->getEmail(), $to->getEmail())
            ->text($content);

        $this->mailer->send($message);
    }

If I rollback my code to swiftmailer it works. (with exactly same mailserver and no other change)

Mailserver uses postfix and dovecot.

Dovecot auth config is

auth_mechanisms = plain login

Again no changes there, one library works the other not (probably due to some config that I need to figure out)


Solution

  • I solved this by adding 'verify_peer' => 0,

    to the dsn query string.

    The error that was thrown by the symfony mailer

     Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
    error:0A000086:SSL routines::certificate verify failed;
    
    

    This made me research and disable peer verification.

    To do this I changed my code to

    <?php
    // other class stuff
    
    $this->mailer = new \Symfony\Component\Mailer\Mailer(
        \Symfony\Component\Mailer\Transport::fromDsn(
            formSmtpConnectionString(
                env('MAIL_HOST'),
                (int)env('MAIL_PORT'),
                env('MAIL_USERNAME'),
                env('MAIL_PASSWORD'),
                [
                    'verify_peer' => 0,
                ],
            ),
        )
    );
    
    
    function formSmtpConnectionString(
        string $host,
        int    $port,
        string $username,
        string $password,
        array  $options = []
    ): string
    {
        $qp = [];
    
        if (array_key_exists('verify_peer', $options)) {
            $qp['verify_peer'] = $options['verify_peer'] ?? null === 0 ? 0 : 1;
        }
    
        return sprintf(
            'smtp://%s:%s@%s:%s%s',
            urlencode($username),
            urlencode($password),
            urlencode($host),
            $port,
            count($qp) === 0 ? '' : sprintf('?%', http_build_query($qp)),
        );
    }