147

I have a SMTP-server, for the purpose of this question lets call it: smtp.mydomain.example.

How do I check if the SMTP-server is in working? Can I send emails manually from Linux commandline?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
balanv
  • 10,686
  • 27
  • 91
  • 137
  • @Flexo Shouldn't such questions be migrated rather than closed ? This one looks like it belongs to Serverfault – Balmipour Oct 15 '15 at 19:10
  • 1
    @Balmipour old questions can't be migrated, even by moderators. There are good reasons for this discussed on http://meta.stackexchange.com (I don't have a link to hand right now), but note that the question isn't deleted. – Flexo Oct 15 '15 at 19:25
  • After checking several posts on Meta, it seems (to me) migrating "just" needs to reopen first. Maybe this isn't possible with old questions, btw. I guess you know that better than me. Anyway, thanks for your answer. A more relevant point I've read in meta posts was that migration seems to require a "particularly valuable" question. Not that this one is useless, but it's not a jewel either. I don't understand this much, since SO is overrated over other sites like ServerFault / Linux or Superuser, but I'll keep the debate for another day (and it belongs to Meta anyway, not SO :) – Balmipour Oct 16 '15 at 14:20

4 Answers4

200

Syntax for establishing a raw network connection using telnet is this:

telnet {domain_name} {port_number}

So telnet to your SMTP server like

telnet smtp.mydomain.example 25

And copy and paste the below

helo client.mydomain.example
mail from:<sender@mydomain.example>
rcpt to:<to_email@mydomain.example>
data
From: test@mydomain.example
Subject: test mail from command line

this is test number 1
sent from linux box
.
quit

Note : Do not forgot the "." at the end which represents the end of the message. The "quit" line exits ends the session.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
balanv
  • 10,686
  • 27
  • 91
  • 137
  • 6
    You should wait for the servers's response to each command, and abort if you get and error (4xx or 5xx result code). – tripleee Aug 16 '12 at 14:01
  • 1
    You need a blank line between headers and message, apart fram that it's a working example. – martineg Aug 16 '12 at 14:03
  • 1
    @tripleee : actually when i pasted this in telnet, i can get email from smtp server. Note : Afte the "." at end, i left a line break. – balanv Aug 16 '12 at 14:22
  • Good for you. In the general case, you are behaving like a spammer, which will make some servers hang up on you. – tripleee Aug 16 '12 at 16:47
  • 19
    Instead of `hello velanapps.com` you should write `helo velanapps.com` and if you want to know which smtp extensions are available, you should write `ehlo velanapps.com`, as in [SMTP Example](http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol#SMTP_transport_example) – jgrocha May 10 '14 at 23:59
  • 4
    you can exit by typing quit – rhand May 31 '15 at 07:54
  • 2
    This answer was useful, however I omitted the first line in my tests. I stil got emails through from the smtp server. It would be helpful if you could include a little bit about what the velanapps.com domain is doing here. I am always wary when I see a domain used for something like this with no explanation as to what its doing with our data. Thanks. – Jeremy Apr 13 '17 at 08:46
  • 2
    @Jeremy Its just a sample data, i have removed it and added placeholder... – balanv May 13 '17 at 06:17
  • 2
    "I found this on the web, and added this to Stack Overflow as many end up here." Bless your heart. – Bruno Bronosky Jul 19 '17 at 17:09
  • this works only if I switch to line mode with `^]` and `l` consecutively – vladkras Mar 26 '19 at 13:57
32

The only thing about using telnet to test postfix, or other SMTP, is that you have to know the commands and syntax. Instead, just use swaks :)

thufir@dur:~$ 
thufir@dur:~$ mail -f Maildir
"/home/thufir/Maildir": 4 messages
>    1 thufir@dur.bouncem                   15/553   test Mon, 30 Dec 2013 10:15:12 -0800
     2 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 10:15:55 -0800
     3 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 10:29:57 -0800
     4 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 11:54:16 -0800
? q
Held 4 messages in /home/thufir/Maildir
thufir@dur:~$ 
thufir@dur:~$ swaks --to thufir@dur.bounceme.net
=== Trying dur.bounceme.net:25...
=== Connected to dur.bounceme.net.
<-  220 dur.bounceme.net ESMTP Postfix (Ubuntu)
 -> EHLO dur.bounceme.net
<-  250-dur.bounceme.net
<-  250-PIPELINING
<-  250-SIZE 10240000
<-  250-VRFY
<-  250-ETRN
<-  250-STARTTLS
<-  250-ENHANCEDSTATUSCODES
<-  250-8BITMIME
<-  250 DSN
 -> MAIL FROM:<thufir@dur.bounceme.net>
<-  250 2.1.0 Ok
 -> RCPT TO:<thufir@dur.bounceme.net>
<-  250 2.1.5 Ok
 -> DATA
<-  354 End data with <CR><LF>.<CR><LF>
 -> Date: Mon, 30 Dec 2013 14:33:17 -0800
 -> To: thufir@dur.bounceme.net
 -> From: thufir@dur.bounceme.net
 -> Subject: test Mon, 30 Dec 2013 14:33:17 -0800
 -> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
 -> 
 -> This is a test mailing
 -> 
 -> .
<-  250 2.0.0 Ok: queued as 52D162C3EFF
 -> QUIT
<-  221 2.0.0 Bye
=== Connection closed with remote host.
thufir@dur:~$ 
thufir@dur:~$ mail -f Maildir
"/home/thufir/Maildir": 5 messages 1 new
     1 thufir@dur.bouncem                   15/553   test Mon, 30 Dec 2013 10:15:12 -0800
     2 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 10:15:55 -0800
     3 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 10:29:57 -0800
     4 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 11:54:16 -0800
>N   5 thufir@dur.bouncem                   15/581   test Mon, 30 Dec 2013 14:33:17 -0800
? 5
Return-Path: <thufir@dur.bounceme.net>
X-Original-To: thufir@dur.bounceme.net
Delivered-To: thufir@dur.bounceme.net
Received: from dur.bounceme.net (localhost [127.0.0.1])
    by dur.bounceme.net (Postfix) with ESMTP id 52D162C3EFF
    for <thufir@dur.bounceme.net>; Mon, 30 Dec 2013 14:33:17 -0800 (PST)
Date: Mon, 30 Dec 2013 14:33:17 -0800
To: thufir@dur.bounceme.net
From: thufir@dur.bounceme.net
Subject: test Mon, 30 Dec 2013 14:33:17 -0800
X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
Message-Id: <20131230223317.52D162C3EFF@dur.bounceme.net>

This is a test mailing

New mail has arrived.
? q
Held 5 messages in /home/thufir/Maildir
thufir@dur:~$ 

It's just one easy command.

kronn
  • 925
  • 11
  • 31
Thufir
  • 8,216
  • 28
  • 125
  • 273
18
[root@piwik-dev tmp]# mail -v root@localhost
Subject: Test
Hello world
Cc:  <Ctrl+D>

root@localhost... Connecting to [127.0.0.1] via relay...
220 piwik-dev.example.com ESMTP Sendmail 8.13.8/8.13.8; Thu, 23 Aug 2012 10:49:40 -0400
>>> EHLO piwik-dev.example.com
250-piwik-dev.example.com Hello localhost.localdomain [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-DELIVERBY
250 HELP
>>> MAIL From:<root@piwik-dev.example.com> SIZE=46
250 2.1.0 <root@piwik-dev.example.com>... Sender ok
>>> RCPT To:<root@piwik-dev.example.com>
>>> DATA
250 2.1.5 <root@piwik-dev.example.com>... Recipient ok
354 Enter mail, end with "." on a line by itself
>>> .
250 2.0.0 q7NEneju002633 Message accepted for delivery
root@localhost... Sent (q7NEneju002633 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 piwik-dev.example.com closing connection
Satish
  • 16,544
  • 29
  • 93
  • 149
2

Not sure if this help or not but this is a command line tool which let you simply send test mails from a SMTP server priodically. http://code.google.com/p/woodpecker-tester/

ehsun7b
  • 4,796
  • 14
  • 59
  • 98