Connecting to Gmail SMTP via socket returns different responses per server - sockets

I am trying to understand why opening a socket to Gmail's server via SSL provides different responses for different servers. This is stopping me from connecting to Gmail for sending out emails.
Below is a very simple script:
<?php
$smtp_connect = fsockopen('ssl://smtp.googlemail.com', 465,
$errno,
$errstr,
300);
var_dump(fgets($smtp_connect, 512));
?>
On server 1 [no ssl cert installed], the output of the above code is: string(57) "220 smtp.googlemail.com ESMTP s89sm726209qkl.44 - gsmtp
"
On server 2 [ssl cert installed], the output of the above code is: string(71) "220-mycompany.pro.com ESMTP Exim 4.87 #1 Wed, 26 Oct 2016 07:42:49 -0400
"
Can anyone explain why this happens, and how can I make server 2 have the same behavior as server 1?
Update: I have also tried smtp.gmail.com, the output is same as using smtp.googlemail.com.

The line you get the the SMTP welcome message. It is perfectly normal that different servers give a different welcome message. It is even normal that the response to commands you send is different. This means that when communicating with an SMTP server you should not assume fixed strings as response but implement proper parsing of SMTP. See the SMTP standard (RFC 2821) for details about this protocol.

Related

Error when trying to send mail over smtp with Office 365 account

An application, which obviously uses Chilkat plugin, does not correctly send E-mails anymore using an Office 365 account. After a migration from on-premise exchange to Microsoft 365, the account and server settings were changed at end of March this year. Furthermore, client authentication needed to be disabled for the mailbox. After that, sending E-mails from the application had worked. Now, it again doesn't. Yesterday, one E-mail could successfully be sent after a computer restart - the next ones failed again.
I'm stuck to find the cause of the error and also I don't know, what data exactly is sent from the application during the connection, which makes the analysis more difficult.
I found this post mentioning that entries for TLS 1.0 need to be in the registry. Those were missing, but there were entries for TLS 1.2, so I didn't assume this is an issue, espccially because the entries concerning cryptography (also mentioned in the post) did already exist. In the meantime, I have added them nonetheless.
Below is the Chilkat Log, showing that - after the server is ready - the clientHandshake2 fails with error code 0x2746.
The support person of the application he couldn't find any problem and asked concerning that error code - what i assumed he should be able to examine. When I talked to him on the phone, I found out that he doesn't know more about Chilkat than I do. He mentioned an interesting point, which need to be double checked: As far as he was informed from the user directly - or how he had understood it - the error doesn't show up, if the user chooses another "Mandant" (client) in the application and sends E-mails from there.
Can someone help identify the cause of the error?
SendEmail:
DllDate: May 25 2017
ChilkatVersion: 9.5.0.68
UnlockPrefix: WERNERMAILQ
Architecture: Little Endian; 32-bit
Language: Visual C++ 2017 (32-bit)
VerboseLogging: 0
sendEmailInner:
renderToMime:
createEmailForSending:
Auto-generating Message-ID
--createEmailForSending
renderToMime: Elapsed time: 0 millisec
--renderToMime
sendMimeInner:
ensureSmtpSession:
ensureSmtpConnection:
SmtpHost: smtp.office365.com
SmtpPort: 587
SmtpUsername: edith.beer#halten.ch
SmtpSsl: 0
StartTLS: 1
smtpConnect:
smtpHostname: smtp.office365.com
smtpPort: 587
connectionIsReady:
Need new SMTP connection
--connectionIsReady
smtpSocketConnect:
socketOptions:
SO_SNDBUF: 262144
SO_RCVBUF: 4194304
TCP_NODELAY: 1
SO_KEEPALIVE: 1
--socketOptions
--smtpSocketConnect
smtpGreeting:
readSmtpResponse:
SmtpCmdResp: 220 AM0PR02CA0163.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 23 Aug 2021 11:51:12 +0000
--readSmtpResponse
--smtpGreeting
startTLS:
sendCmdToSmtp:
SmtpCmdSent: EHLO EWGH-N-FV<CRLF>
--sendCmdToSmtp
readSmtpResponse:
SmtpCmdResp: 250-AM0PR02CA0163.outlook.office365.com Hello [213.221.219.37]
SmtpCmdResp: 250-SIZE 157286400
SmtpCmdResp: 250-PIPELINING
SmtpCmdResp: 250-DSN
SmtpCmdResp: 250-ENHANCEDSTATUSCODES
SmtpCmdResp: 250-STARTTLS
SmtpCmdResp: 250-8BITMIME
SmtpCmdResp: 250-BINARYMIME
SmtpCmdResp: 250-CHUNKING
SmtpCmdResp: 250 SMTPUTF8
--readSmtpResponse
sendCmdToSmtp:
SmtpCmdSent: STARTTLS<CRLF>
--sendCmdToSmtp
readSmtpResponse:
SmtpCmdResp: 220 2.0.0 SMTP server ready
--readSmtpResponse
clientHandshake:
clientHandshake2:
readHandshakeMessages:
WindowsError: Eine vorhandene Verbindung wurde vom Remotehost geschlossen.
WindowsErrorCode: 0x2746
maxToReceive: 5
Failed to receive data on the TCP socket
Failed to read beginning of SSL/TLS record.
b: 0
dbSize: 0
nReadNBytes: 0
idleTimeoutMs: 30000
--readHandshakeMessages
--clientHandshake2
--clientHandshake
Client handshake failed. (1)
connectionClosed: 0
Failed to establish TLS connection.
--startTLS
--smtpConnect
--ensureSmtpConnection
--ensureSmtpSession
--sendMimeInner
--sendEmailInner
Failed.
--SendEmail
--ChilkatLog
23.08.2021 13:48:27 -F- Beim Versand der Lohnabrechnung per Email an Mitarbeiter(in) Nadine Aeschlimann ist ein Fehler aufgetreten!
You are using an old version of Chilkat. Try testing with the latest version.
In the meantime we could solve it.
The reason was that the outgoing requests were blocked by the firewall. Strangely, a few of them went through, but not all. This is the reason why a network issue was initially not in the focus of our investigation.

Mail delivery failed: returning message to sender (No such User here)

I have migrated my website and the email records to a new server (other provider). Everything was ok except that now when I want to send a message from my email (my email direction is the same), one of my clients can not receive my messages. I chatted with my client and his mails are ok, he is receiving mails without problems, as he said.
I reported the problem to my Hosting provider and they have changed the mail Exchanger from remote to local but it didn't finish with the problem. Someone knows what could be happening?
This is part of the message that appears:
"
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
peter#thisismyclientsdirection.com
No Such User Here
peter#thisismyclientsotherdirection.com
No Such User Here
Reporting-MTA: dns; cherry.theserversite.pro
Action: failed
Final-Recipient: rfc822;peter#thisismyclientsdirection.com
Status: 5.0.0
Action: failed
Final-Recipient: rfc822;peter#thisismyclientsotherdirection.com
Status: 5.0.0
Return-path: <comercial#mydomain.com>
Received: from [71.13.252.126] (port=58531 helo=[10.145.123.217])
by cherry.theserversite.pro with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
(Exim 4.93)
(envelope-from <comercial#mydomain.com>)
id 1UZ6w-00EaD5-0h; Mon, 19 Oct 2020 13:38:33 -0400
To: peter#thisismyclientsdirection.com, peter#thisismyclientsotherdirection.com
From: comercial#mydomain.com
Subject: =?UTF-8?Q?Reenv=c3=ado_-_cotizaciones_mantenimiento?=
MIME-Version: 1.0
Content-Type: multipart/mixed;
Content-Language: es-ES
X-Antivirus: Avast (VPS 201019-2, 19/10/2020), Outbound message
X-Antivirus-Status: Clean
X-Exim-DSN-Information: Due to administrative limits only headers are returned
"
Thanks,
I found the solution. The problem was that the other websites (the domains of my client) are inside my server and in the Email Routing section (it is in the Cpanel) of those websites, the domains were targeted as "Local server" or not targeted. Despite it, the email server for those domains don't were in my server then the system was confused. I just changed the target to "Remote servers" for both domains (the domains of my client) and the problem disappear.
I hope this explanation could be useful for other developer.
Anyway thank you,
TheJohnny

Telnet SMTP send mail timeout

I want to use telnet to send an e-mail with SMTP. It seemed that I had connect to the mail service successfully. And the message had been put in the queue too. But why did the SMTP service failed to send the message to the received address?
When I searched the solution by the internet, someone had said that firewall should paid it. But I have no idea about the relationship between the firewall or antivirus software and telnet.
This is my command:
220 smtp-5-124.smtpsmail.fmail.xd.sinanode.com ESMTP
helo a
250 smtp-5-124.smtpsmail.fmail.xd.sinanode.com
auth login
334 VXNlcd5hbWU6
a25vd215aGV2341241fasYS5jbg==
334 UGFzczdvcmQ6
UmF5V2adfa23adsfZzgyNA==
235 OK Authenticated
mail from: <knowmyheart#sina.cn>
502 unimplemented (#5.5.1)
mail from:<knowmyheart#sina.cn>
553 Envolope sender mismatch with login user..
mail from:<knowmyheart#sina.cn>
250 ok
rcpt to:<517447201#qq.com>
250 ok
data
354 End data with <CR><LF>.<CR><LF>
subject: test
to:<517447201#qq.com>
from:<knowmyheart#sina.cn>
Testttttttttttttt!
.
250 ok queue id 6744281442287
451 Timeout.
By the way, I tried it both on Windows 10 and Ubuntu-15.10. And both of them failed with the same question.
Finally, I found where I was wrong.
In my previous case,I failed to send a e-mail with the above code.I had succeeded in connecting with the SMTP server, and everything worked except the "Timeout" response. Actually, the "Timeout" didn't mean I failed to send a e-mail but lose the TCP connection. Hours Later, I login my e-mail, which was used for sending, finding that I receive a mail. The mail told me the e-mail I sent hours before with telnet was given back, failing to post. So it meant that the reason was hardly because of my telnet client, but in server or something else.
Finally, I found that it was because of my mail format. In my previous code, after I sending keyword -- "data", I immediately send the content.
data
354 End data with <CR><LF>.<CR><LF>
subject: test
to:<517447201#qq.com>
from:<knowmyheart#sina.cn>
Testttttttttttttt!
.
250 ok queue id 6744281442287
When I changed it to this, it worked.
data
354 End data with <CR><LF>.<CR><LF>
subject: test
to:<517447201#qq.com>
from:<knowmyheart#sina.cn>
Testttttttttttttt!
.
250 ok queue id 6744281442287
Notice that the difference between the codes is the empty line before the content in this mail. "subject", "to" and "from" are the headers. Between the header and the content, there must be an empty line.
Similarly, the headers in mail can be ignored. The code is like this:
data
354 End data with <CR><LF>.<CR><LF>
Testttttttttttttt!
.
250 ok queue id 6744281442287
451 Timeout.
Even if there is a "Timeout", it affect nothing but break the tcp connection between localhost and smtp server.

Postfix transport: deliver different domains via different relayhosts

Postfix version 2.11.0
What I'm trying to accomplish is to have mail destined for a particular domain to be relayed through SendGrid but all other outgoing mail to be sent from my local server directly. I have this configuration in /etc/postfix/transport:
example.com smtp:[smtp.sendgrid.net]:587
.example.com smtp:[smtp.sendgrid.net]:587
* :
My understanding from all the searches I've done and reading the Postfix docs is that this should work but if I send to any other domain, for example gmail.com it's still getting relayed through SendGrid.
An example from /var/log/mail.log:
Oct 26 16:15:46 myhost postfix/smtp[25783]: A75F0C04F9: to=<PRIVACY_MASKED#gmail.com>, relay=smtp.sendgrid.net[108.168.190.108]:587, delay=11, delays=11/0.02/0.22/0.03, dsn=2.0.0, status=sent (250 Ok: queued as iTwf5zmCQQSgz_I6sVpSSA)
Is there some other configuration I'm missing?
Ok, now I see the error of my ways. In sasl_passwd I had this:
smtp.sendgrid.net USERNAME:PASSWD
But in transport I had this:
example.com smtp:[smtp.sendgrid.com]:587
The difference is .net versus .com.
Changed it to .net and works!

How configure roundcube to work with imaps?

I recently installed Postfix, Dovecot to setup a mail server on my own VPS ( using this tutorial: Email with Postfix, Dovecot, Mysql)
Imaps server uses port 993 for Authentication, and Postfix uses port 25 to send mails.
In this tutorial, users stored in a Database ( so imaps use mysql to authenticate users).
i'm sure every thing works fine with imaps and postfix , because few days ago i installed Kmail client (on my linux) and receive mails from my server. sending mails also works fine, i sent a mail to Gmail and google received it without a problem (in my "Gmail inbox" not spam folder)
So to get to my Emails from a web mail client, i installed Roundcube on /var/www/mail directory.
I configured Roundcube many times. but each time it gives me this Error:
IMAP Error: Login failed for [me#mydomain] from X.x.X.x . Empty
startup greeting (localhost:993) in
/var/www/mm/program/lib/Roundcube/rcube_imap.php on line 184 (POST
/mm/?_task=login?_task=login&_action=login)
When i do log in from roundcube, imap server says ( in /var/log/mail.log ):
May 20 07:05:16 my-server dovecot: imap-login: Disconnected (no auth
attempts): rip=::1, lip=::1, TLS handshaking: Disconnected
Here is my roundcube config file :
$config['db_dsnw'] = 'mysql://roundcubeuser:myPassword#localhost/roundcubemail';
// ----------------------------------
// IMAP
// ----------------------------------
$config['debug_level'] = 13;
$config['default_host'] = 'ssl://127.0.0.1';
$config['default_port'] = 993;
// ----------------------------------
// SMTP
// ----------------------------------
$config['smtp_server'] = 'ssl://localhost';
What's the problem? i really have no idea what is happening !
Thank you.
I'm using postfix + dovecot + roundcube a few months now and it's working for me. In my configuration, postfix rejects plaintext sessions, so roundcube has to connect with ssl - and it's working.
This is from my main.inc.php. I don't remember editing anything here, it's just the initial config created during the installation.
Now that I'm looking at it, default_port doesn't make any sense, I think it's just ignored.
// To use SSL/TLS connection, enter hostname with prefix ssl:// or tls://
// Supported replacement variables:
// %n - http hostname ($_SERVER['SERVER_NAME'])
// %d - domain (http hostname without the first part)
// %s - domain name after the '#' from e-mail address provided at login screen
// For example %n = mail.domain.tld, %d = domain.tld
// TCP port used for IMAP connections
$rcmail_config['default_port'] = 143;
$rcmail_config['default_host'] = array("ssl://localhost:993");
// TCP port used for IMAP connections
$rcmail_config['default_port'] = 143;
In case the other answer does not work, this is what worked for me. My config.inc.php now contains:
$config['default_host'] = 'ssl://localhost';
$config['default_port'] = 993;
NOTE: using tls://localhost did not work for me. I had to specify ssl:// as the URI scheme.
Via PhpMyAdmin, I also ran this SQL command (all my user accounts are on the same machine that runs RoundCube):
UPDATE `rc_users` SET `mail_host`='ssl://localhost'
I got the port number 993 from running sudo netstat -tulnp in order to determine the port on which Dovecot was listening.