I received SMTP server information and credentials and want to test if they work.
How can I easily test a SMTP connection on Linux using the command line?
I know I can do this via telnet / openssl but this seems very complicated.
So how to check a SMTP server?
The tool
swaks
comes in handy hereOn Ubuntu it's
Then you can run the command and will see the SMTP dialog
As you can see here, it's needs authentication, this is why we rerun with
Check the man page for further information
It is pretty easy, you can just google SMTP Commands, and you could use them without an issue. As you have answered your own question, you can use SWAKS. Here's some alternative options.
These are some SMTP commands:
Each command is used in a normal communication sequence between two servers through the SMTP protocol, in order to deliver emails.
HELO
It’s the first SMTP command: is starts the conversation identifying the sender server and is generally followed by its domain name.
EHLO
An alternative command to start the conversation, underlying that the server is using the Extended SMTP protocol.
MAIL FROM
With this SMTP command the operations begin: the sender states the source email address in the “From” field and actually starts the email transfer.
RCPT TO
It identifies the recipient of the email; if there are more than one, the command is simply repeated address by address.
SIZE
This SMTP command informs the remote server about the estimated size (in terms of bytes) of the attached email. It can also be used to report the maximum size of a message to be accepted by the server.
DATA
With the DATA command the email content begins to be transferred; it’s generally followed by a 354 reply code given by the server, giving the permission to start the actual transmission.
VRFY
The server is asked to verify whether a particular email address or username actually exists.
TURN
This command is used to invert roles between the client and the server, without the need to run a new connaction.
AUTH
With the AUTH command, the client authenticates itself to the server, giving its username and password. It’s another layer of security to guarantee a proper transmission.
RSET
It communicates the server that the ongoing email transmission is going to be terminated, though the SMTP conversation won’t be closed (like in the case of QUIT).
EXPN
This SMTP command asks for a confirmation about the identification of a mailing list.
HELP
It’s a client’s request for some information that can be useful for the a successful transfer of the email.
QUIT
It terminates the SMTP conversation.
OpenSSL, testssl.sh & GnuTLS
You can use
openssl s_client
, by running a command like:You can also use a tool called testssl.sh for testing SSL/TLS on your SMTP server, obviously even if it is locally hosted. Once you have downloaded it, extract it and go inside the testssl.sh folder and run:
You can also use
GnuTLS
if you have it installed:Telnet
If your SMTP server does not have SSL/TLS, you can use
telnet
. Telnet is the most basic tool for this, but it doesn't support SSL/TLS.PHPMailer
If you use PHP, you can use PHPMailer:
Even though this is not an answer to your question. You can even setup DKIM with ease in PHPMailer:
Python
Python provides
smtplib
module, which defines an SMTP client session object that can be used to send mails to any Internet machine with an SMTP or ESMTP listener daemon.Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail −
host − This is the host running your SMTP server. You can specifiy IP address of the host or a domain name like example.com. This is an optional argument.
port − If you are providing host argument, then you need to specify a port, where SMTP server is listening. Usually this port would be 25.
local_hostname − If your SMTP server is running on your local machine, then you can specify just localhost the option.
An SMTP object has an instance method called
sendmail
, which is typically used to do the work of mailing a message. It takes three parameters −The sender − A string with the address of the sender.
The receivers − A list of strings, one for each recipient.
The message − A message as a string formatted as specified in the various RFCs.
Example
Here is a simple way to send one e-mail using Python script. Try it once −
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and a Subject header, separated from the body of the e-mail with a blank line.
To send the mail you use smtpObj to connect to the SMTP server on the local machine. Then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these are not always used to route the mail).
If you are not running an SMTP server on your local machine, you can the use smtplib client to communicate with a remote SMTP server. Unless you are using a webmail service (such as gmail or Yahoo! Mail), your e-mail provider must have provided you with the outgoing mail server details that you can supply them, as follows −
Sending an HTML e-mail using Python When you send a text message using Python, then all the content is treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to the HTML syntax. However, Python provides an option to send an HTML message as actual HTML message.
While sending an e-mail message, you can specify a Mime version, content type and the character set to send an HTML e-mail.
Example
Following is an example to send the HTML content as an e-mail. Try it once −
Sending Attachments as an E-mail To send an e-mail with mixed content requires setting the Content-type header to multipart/mixed. Then, the text and the attachment sections can be specified within boundaries.
A boundary is started with two hyphens followed by a unique number, which cannot appear in the message part of the e-mail. A final boundary denoting the e-mail's final section must also end with two hyphens.
The attached files should be encoded with the
pack("m")
function to have base 64 encoding before transmission.Example Following is an example, which sends a file
/tmp/test.txt
as an attachment. Try it once −SWAKS:
To install it:
sudo apt install swaks
sudo yum install epel-release
, andsudo yum install swaks
orsudo dnf install swaks
sudo pacman -S swaks
Then you can run the command and will see the SMTP dialog:
As you can see here, it's needs authentication, this is why we rerun with
You can also use AUTH PLAIN by using
--auth PLAIN
, depending upon the method the server supports. Check the man page for further information by usingman swaks
.MXToolBox
You can use MXToolBox's Email Server Test for some testing that might be useful sometimes, but you cannot specify what you want to do with it though. So, you are better using the above stuff.
Or, Just use the
mail
command...It can be test in multiple ways: