Shopware: Unable to connect with STARTTLS without encryption - php

In my .env file I have:
MAILER_URL="smtp://localhost:25?encryption=&auth_mode="
But if I send a mail the log shows:
Could not send mail: Failed sending mail to following recipients: {{ recipients }} with Error: Unable to connect with STARTTLS. Error Code:0 Template data ...
Looks like it tries to use TLS but I don't want to. It worked before. Maybe it got broken by an update.
Looking at the documentation I also tried encryption=null with same result.
Debugging in vendor/shopware/core/Content/Mail/Service/MailerTransportFactory.php:36 I made sure my MAILER_URL was used.
I am using shopware 6.4.1.1 with symfony/mailer v5.2.10
How can I get mails working again?

By default, the SMTP transport in the Symfony Mailer performs TLS peer verification. You can disable this verification by setting the parameter verify_peer to 0, for example:
MAILER_URL="smtp://localhost:25?encryption=&auth_mode=&verify_peer=0"

Related

How do I use STARTTLS with swiftmailer in php?

this is my first question here so bare with me.
How do I use STARTTLS with swiftmailer in php?
I am currently on Laravel 7.x running on PHP 7.3.
I need to connect to the Office365 SMTP server, which unforunately only support STARTTLS.
I cannot use another provider than Office365 due to another company infrastructure, so my options are limited.
My problem is that when i give STARTTLS as the mail encryption and attempt to connect to / send mails trough the SMTP server, I recieve the following error:
[2020-06-25 11:15:21] stage.ERROR: Connection could not be established with host smtp.office365.com :stream_socket_client(): unable to connect to starttls://smtp.office365.com:587 (Unable to find the socket transport "starttls" - did you forget to enable it when you configured PHP?) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.office365.com :stream_socket_client(): unable to connect to starttls://smtp.office365.com:587 (Unable to find the socket transport "starttls" - did you forget to enable it when you configured PHP?) at /home/forge/censoreddomain.com/releases/20200615132510/src/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:269)
My .env config mail section:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=XXXX#XXXX.se
MAIL_PASSWORD=XXXXXXXtXVF
MAIL_ENCRYPTION=starttls
Try to change "MAIL_ENCRYPTION=starttls" to "MAIL_ENCRYPTION=tls"

Using custom SMTP server with Laravel / swiftmailer fails, same settings work in Thunderbird

I'm trying to send an email using SwiftMailer (which is what Laravel uses by default):
$transport = (new Swift_SmtpTransport('smtp.my-server.com', 465, "tls"))
->setAuthMode('PLAIN')
->setUsername('my-username')
->setPassword('my-password');
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['address#my-domain.com' => 'My Name'])
->setTo(['my-gmail-address#gmail.com'])
->setBody('Here is the message itself');
$mailer->send($message);
echo "\nMail sent! \n\n";
And running this gives me:
Expected response code 220 but got an empty response
Using the same settings in Thunderbird (an email client) works:
I'm able to send emails with these settings in Thunderbird. (from the same system I'm attempting to use swiftmailer)
So how can I debug this? Is there some default setting that I need to change in swiftmailer to make this work? Is there some way I can get more debug information?
My SMTP server doesn't even log an authentication attempt. It's like swiftmailer isn't even trying to connect to my smtp server!
UPDATE: I tried using Swift_Plugins_LoggerPlugin to get more debugging info, but it was no help:
++ Starting Swift_SmtpTransport
!! Expected response code 220 but got an empty response (code: 0)
TLDR; use ssl instead of tls when using a port that forces ssl/tls. Swiftmailer's "tls" actually isn't tls, it's starttls.
It seems that swiftmailer's "tls" encryption option is mislabeled! My server only accepts ssl/tls on port 465, and I was able to connect to it over Thunderbird, but swiftmailer was unable to.
When I switched to port 587, which only supports starttls on my server, it worked, even though the encryption is set to tls (which should NOT be the same as starttls).
Looking at the source files confirmed my suspicion:
if ($this->params['tls']) {
try {
$this->executeCommand("STARTTLS\r\n", [220]);
...
when it's set to tls, it actually means starttls. :P
So if you want to use ssl/tls, do not use the swiftmailer encryption option "tls" 😅
When I changed the encryption to ssl, it worked on the port that enforced ssl/tls. So the answer is to use ssl.
EDIT: I went to open an issue on the swiftmail github, but the issue was already there in "open" status. So I wasn't the only one to be bitten by this. 😛

PearMailer, PHP7, and OpenSSL unable to connect to gmail with unknown error

I'm building a website, and part of it needs to send emails (one for account password reset links, the other for general user-admin contact). I've been using PearMailer to send emails through a GMail account, and that's worked fine...until now. For some reason, it's stopped sending emails, and it's giving me this very unhelpful error:
[message] => Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )
openSSL is enabled, and all the credentials I'm using are correct (I can manually sign in to that GMail account using them).
This is the code I'm using, where $config holds the account credentials, and the $_POST data is from the Contact Us form (ignore the lack of sanitization on it while I'm developing):
require_once "Mail.php";
$params=array(
"host"=>"ssl://smtp.gmail.com",
"port"=>465,
"auth"=>true,
"username"=>$config["mailFrom"],
"password"=>$config["mailPass"]
);
$headers=array(
"To"=>$config["mailFrom"],
"From"=>$_POST['user']." <".$_POST["reply-to"].">",
"Content-type"=>"text/html",
"Subject"=>$_POST['subject']
);
$mail=Mail::factory("smtp", $params);
$sent=$mail->send($config["mailFrom"], $headers, htmlentities($_POST['body']));
It should be sending my GMail account an email from itself, but instead, it's giving me the error I mentioned above. After changing the to and from addresses, no matter what, it still gives me that error. Which is especially strange, since almost identical code is what I've been using to send password reset links, and that was working fine until just now; of course, it too is now failing to connect.
I can't find anything saying Google has changed its settings, and I absolutely haven't gone over the 2,000 messages per day limit that smtp.gmail.com is supposed to allow, so what's going on here?
My solution: switched from PearMailer to SwiftMailer, and in that, I'm setting the SSL options verify_peer and verify_peer_name to false using $transport->setStreamOptions. That seems to send mail just fine now. (Without those options disabled, apparently GMail's SSL certificates aren't compatible with newer versions of PHP. Go figure.)
EDIT:
I'm not really sure if this will work for you, try:
$params=array(
"host"=>"smtp.gmail.com",
"port"=>587,
"auth"=>true,
"username"=>$config["mailFrom"],
"password"=>$config["mailPass"]
);
This will use TLS instead of SSL. Give it a try.

Server return error Unable to connect SMTP server when use ssl

Hi guis I have been migrating an application to new server, is an Ubuntu server but on the new server I have some problem to send emails using Cakephp 2.0.
If the host on cake mail config is localhost I have the following error:
SMTP Error: 530 5.7.0 Must issue a STARTTLS command first
Search on the internet the solutions may be is config SMTP ssl://mydomain.com.br on php.ini or using ssl host like this ssl://mydomain.com.br on cake mail config. On the first option nothing change, error persist and with host ssl on cake mail config I have the following error:
Unable to connect to SMTP server.
I make a simples test using only php mail() function and the email is been sent.
On this server I config postfix and dovecot may be this can be the problem?
you can use 'PHPMailer'.
it's simple and easy.
https://github.com/PHPMailer/PHPMailer

smtp relay - gmail - swiftmailer: Expected response code 220 but got code ""

I am using gmail as an smtp server for my swiftmailer class.
however I am getting
Expected response code 220 but got code "", with message "" in "\classes\Swift\Transport\AbstractSmtpTransport.php"
What does this mean?
I think it probably means you sent a blank line to the smtp server.
Make sure none of your commands have line feeds in them.
Can you log the transaction and paste a copy of the log?
see here for details about logging
http://swiftmailer.org/docs/logger-plugin
I got this when I had the wrong setting in the factories.yml file as below.
class: Swift_SendmailTransport
But it should be the following.
class: Swift_SmtpTransport
For users of smtp-relay.gmail.com, if you use localhost/127.0.0.1 as domain during developments, you probably need to change the domain name to use in EHLO command to begin the transaction.
For SwiftMailer Symfony bundle (since 2.4.0), you can set the local_domain config parameter:
// config/packages/dev/swiftmailer.yaml
swiftmailer:
...
local_domain: dev.mydomain.tld

Categories