How can I test a SMTP server?
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?
3 Answers 3
The tool swaks comes in handy here
Then you can run the command and will see the SMTP dialog
$ swaks --to foo@example.com --server smtp.ionos.de:587 === Trying smtp.ionos.de:587. === Connected to smtp.ionos.de. EHLO lafto ] MAIL FROM: <** 530 Authentication required ->QUIT
As you can see here, it's needs authentication, this is why we rerun with
$ swaks --to foo@example.com --server smtp.ionos.de:587 --auth LOGIN Username: foo Password: bar
Check the man page for further information
@Chi.C.J.RajeevaLochana thanks, I know testssl, but I am more concerned about the SMTP data not about the certificates
I know I can do this via telnet / openssl but this seems very complicated
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:
openssl s_client -starttls smtp -connect mail.example.com:587
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:
./testssl.sh -t smtp mail.example.com:25
You can also use GnuTLS if you have it installed:
gnutls-cli mail.example.com -p 25
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:
SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp.example.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'user@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged $mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above //Recipients $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient $mail->addAddress('ellen@example.com'); // Name is optional $mail->addReplyTo('info@example.com', 'Information'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); // Attachments $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body in bold!'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; > catch (Exception $e) < echo "Message could not be sent. Mailer Error: ErrorInfo>"; >
Even though this is not an answer to your question. You can even setup DKIM with ease in PHPMailer:
setFrom('from@example.com', 'First Last'); $mail->addAddress('whoto@example.com', 'John Doe'); $mail->Subject = 'PHPMailer mail() test'; $mail->msgHTML(file_get_contents('contents.html'), __DIR__); //This should be the same as the domain of your From address $mail->DKIM_domain = 'example.com'; //See the DKIM_gen_keys.phps script for making a key pair - //here we assume you've already done that. //Path to your private key: $mail->DKIM_private = 'dkim_private.pem'; //Set this to your own selector $mail->DKIM_selector = 'phpmailer'; //Put your private key's passphrase in here if it has one $mail->DKIM_passphrase = ''; //The identity you're signing as - usually your From address $mail->DKIM_identity = $mail->From; //Suppress listing signed header fields in signature, defaults to true for debugging purpose $mail->DKIM_copyHeaderFields = false; //Optionally you can add extra headers for signing to meet special requirements $mail->DKIM_extraHeaders = ['List-Unsubscribe', 'List-Help']; //When you send, the DKIM settings will be used to sign the message if (!$mail->send()) < echo 'Mailer Error: ' . $mail->ErrorInfo; > else
Python
(Taken from https://www.tutorialspoint.com/python3/python_sending_email.htm, because I do not want to provide links, instead I just posted the whole thing here because that page might get a 404 error anytime.)
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 −
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) Here is the detail of the parameters −
- 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.
Here is a simple way to send one e-mail using Python script. Try it once −
#!/usr/bin/python3 import smtplib sender = 'from@fromdomain.com' receivers = ['to@todomain.com'] message = """From: From Person To: To Person Subject: SMTP e-mail test This is a test e-mail message. """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email"
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 −
mail = smtplib.SMTP('smtp.gmail.com', 587)
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.
Following is an example to send the HTML content as an e-mail. Try it once −
#!/usr/bin/python3 import smtplib message = """From: From Person To: To Person MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test This is an e-mail message to be sent in HTML format This is HTML message. This is headline.
""" try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email"
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 −
#!/usr/bin/python3 import smtplib import base64 filename = "/tmp/test.txt" # Read a file and encode it into base64 format fo = open(filename, "rb") filecontent = fo.read() encodedcontent = base64.b64encode(filecontent) # base64 sender = 'me@fromdomain.net' reciever = 'admin@example.com' marker = "AUNIQUEMARKER" body =""" This is a test email to send an attachement. """ # Define the main headers. part1 = """From: From Person To: To Person Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker) # Define the message action part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s """ % (body,marker) # Define the attachment section part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s %s --%s-- """ %(filename, filename, encodedcontent, marker) message = part1 + part2 + part3 try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, reciever, message) print "Successfully sent email" except Exception: print ("Error: unable to send email")
SWAKS:
To install it:
- Ubuntu: sudo apt install swaks
- CentOS: First: sudo yum install epel-release , and sudo yum install swaks or sudo dnf install swaks
- Arch Linux: sudo pacman -S swaks
Then you can run the command and will see the SMTP dialog:
$ swaks --to foo@example.com --server smtp.example.com:587 === Trying smtp.example.com:587. === Connected to smtp.example.com. EHLO somenamehere ] MAIL FROM: <** 530 Authentication required ->QUIT
As you can see here, it's needs authentication, this is why we rerun with
$ swaks --to someone@example.com --server mail.example.com:587 --auth LOGIN Username: yourusername Password: yourpassword
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 using man 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.