OK, it is a shitty post about something you already know, is like the SUID one. Things that I should know but for some odd reason I never spend enough time to "get it" and it has been a grey area for me.

That's the case of sending mails from one of my VPS, without configuring any MTA, any SMPT server or anything. Just as it comes. The idea is to be able to do something like:

$ echo 'Hello world' | mail -s 'Mail from your server' test@jcarreras.es

And receive a mail on my mailbox "inbox". Easy right? Well, maybe.

[First test]

test@jcarreras.es                                                       
~  
[...]  
** test@jcarreras.es R=nonlocal: Mailing to remote domains not
supported  
[...]  
Completed

Ok, great. By default I cannot send mails to the outside world.

[Reconfiguring exim]

Run:

  • General type of configuration: internet site; mail is sent and received directly using SMTP
  • System mail name: (Put YOUR  FQDN here ) mia.jcarreras.es
  • SMTP listening on: 127.0.0.1 ; ::1
  • Other destinations for wich the mail is accepted:  (again, FQDN) mia.jcarreras.es
  • Domain to relay mail for: (leave it empty)
  • Machine to relay mail for: (leave it empty again)
  • Keep number of DNS-queries minimal: No
  • Deliver mehtod for local mail: Maildir format in home directory (not important)
  • Split configuration into small files: No (don't know what the hell is that)
  • Root and postmaster mail recipient: (empty or you can put your user, if you want)

[Second test]

$ echo "HelloWorld" | mail -v -s "This is a test"
test@jcarreras.es                                                           
~  
<= inedit@mia.jcarreras.es U=inedit P=local S=485  
[...]  
R: dnslookup for test@jcarreras.es  
T: remote_smtp for test@jcarreras.es  
Connecting to in1-smtp.messagingengine.com [66.111.4.71]:25 ...
connected  
SMTP<< 220 mx2.messagingengine.com ESMTP . No UCE permitted.  
SMTP>> EHLO localhost  
SMTP<< 250-mx2.messagingengine.com  
250-PIPELINING  
250-SIZE 71000000  
250-STARTTLS  
250-ENHANCEDSTATUSCODES  
250 8BITMIME  
SMTP>> STARTTLS  
SMTP<< 220 2.0.0 Ready to start TLS  
SMTP>> EHLO localhost  
SMTP<< 250-mx2.messagingengine.com  
250-PIPELINING  
250-SIZE 71000000  
250-ENHANCEDSTATUSCODES  
250 8BITMIME  
SMTP>> MAIL FROM:<inedit@mia.jcarreras.es> SIZE=1523  
SMTP>> RCPT TO:<test@jcarreras.es>  
SMTP>> DATA  
SMTP<< 250 2.1.0 Ok  
SMTP<< 451 4.7.1 <test@jcarreras.es>: Recipient address rejected:
Temporary deferral, try again soon  
LOG: MAIN  
SMTP error from remote mail server after RCPT TO:<test@jcarreras.es>:
host in1-smtp.messagingengine.com

451 4.7.1 <test@jcarreras.es>: Recipient address rejected: Temporary
deferral, try again soon

[...]

Faaaail again. As you can see the last line is an SMTP error "Recipient address rejected". Crap.

[/etc/hosts]

What can it be? The /etc/hosts file! For some reason, it has to exist an entry for you FQDN in this file, so you need to add an entry such as:

127.0.0.1   mia.jcarreras.es

[Test three]

inedit@mia> delivering
1VEPzD-0001LI-5q                                                                                                    
~  
R: dnslookup for test@jcarreras.es  
T: remote_smtp for test@jcarreras.es  
Connecting to in1-smtp.messagingengine.com [66.111.4.73]:25 ...
connected  
SMTP>> EHLO localhost  
SMTP<< 250-mx4.messagingengine.com  
250-PIPELINING  
250-SIZE 71000000  
250-STARTTLS  
250-ENHANCEDSTATUSCODES  
250 8BITMIME  
SMTP>> STARTTLS  
SMTP<< 220 2.0.0 Ready to start TLS  
SMTP>> EHLO localhost  
SMTP<< 250-mx4.messagingengine.com  
250-PIPELINING  
250-SIZE 71000000  
250-ENHANCEDSTATUSCODES  
250 8BITMIME  
SMTP>> MAIL FROM:<inedit@mia.jcarreras.es> SIZE=1523  
SMTP>> RCPT TO:<contact@jcarreras.es>  
SMTP>> DATA  
SMTP<< 250 2.1.0 Ok  
SMTP<< 250 2.1.5 Ok  
SMTP<< 354 End data with <CR><LF>.<CR><LF>  
SMTP>> writing message and terminating "."  
SMTP<< 250 2.0.0 Ok: queued as 32D1B3C0EEB  
SMTP>> QUIT  
LOG: MAIN  
=> contact@jcarreras.es R=dnslookup T=remote_smtp
H=in1-smtp.messagingengine.com [66.111.4.73]
X=TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128
DN="C=NO,ST=Oslo,L=Oslo,O=Opera Software ASA,CN=*.messagingengine.com"  
LOG: MAIN  
Completed

Yeeeeeeeeeeeey!! It woooorks!!!!! Now in my inbox I have one maaaail from (surprise surprise) myseeelf!!! Yeeeey!!!

[Bonus extra track]

You know what? All the tests you have done so far trying to sending mails are NOT failed attemps. I mean, yes, they are failed attempts but Exim detects that an error has occurred and puts the mail in a mail queue. That means that you will receive a lot of mails from yourself, one for every single failed attempt to send a message. That's not funny. That's not funny aaat all.

If you want to delete this mails you can (being root) execute [1]:

# Lists the message queue:

$ exim -bp

# Removes one message from the queue

$ exim -Mrm {message-id}

# Remove aaaall messages from the queue

$ exim -bp | exiqgrep -i | xargs exim -Mrm

Okay fellas, take care!

[1] This commands are from http://www.cyberciti.biz/faq/exim-remove-all-messages-from-the-mail-queue/