I'm trying to implement a SMTP server at the moment and I've question regarding the routing...
Imagine, a client (foo#bar.com) sends a mail to foo1#bar1.com, foo2#bar2.com and foo3#bar3.com.
What happens when the SMTP server sees the message? Now, I'm not sure if the following sequence is correct:
The SMTP server will put the mail into the outbox of foo#bar.com
For each recipient (except foo#bar.com) in the envelope, the SMTP server will do:
Retrieve the MX record
Connect to the SMTP server determined by the MX record
Send HELO/EHLO command
Send MAIL FROM command (foo#bar.com)
Send RCPT TO command (e.g. foo1#bar1.com)
Send DATA command
Send QUIT command
Are these steps correct? For me it makes sense that the SMTP of the sender processes all the communication, but I've found some pages (e.g. https://www.msxfaq.de/internet/envelope.htm) where a mail server only forwards the message to the next SMTP server and each SMTP server removes one recipient from envelop's rcpt-to list, e.g.
So, what's correct...?
Sender's SMTP handles all the routing
Sender's SMTP removes one recipient (in this case foo#bar.com) and then forwards the message to the next SMTP server
The steps you listed are basically correct.
The complexity arises when you compose an email and try to send it to a lot of different domains, e.g. steve#apple.com, woz#apple.com, and bill#microsoft.com.
In this case, the server must do MX lookups on each one of these domains and split the email. Steve and Woz are both at the same domain, so the server can send an email with those two recipients to the MX record for apple.com. The server must open a separate connection to the MX record for microsoft.com and attempt to send the email to Bill over that connection.
Related
Honestly, I think I have a fundamental gap in understanding how SMTP works. I can't seem to find a good explanation of what is happening behind the scenes and I think this is preventing me from being able to do what I am attempting to do.
To explain, I'm trying to setup an application which sends notifications to users by connecting to an SMTP server. Fair enough. I figure, since I'm using my own domain, I have SPF/DKIM/DMARC configured, I can add an MX record for the host I set the application up on (my SPF record has the mx keyword to authorize any hosts in my MX records to send/receive mails). Then, I can have that same host run a super lightweight SMTP server that can accept mails from the application, and send them on to recipients.
Almost crucially, I want this server to basically just run on localhost so that only this application can connect and send mails through it, but so that it can't really "receive" mails sent to my domain (I have set the MX priority very low (well, a high number) for this app server). I figure since I'm running my own SMTP server, that I don't really need to authenticate against it (it's running on localhost), just take in any mail and send it on to recipient domains.
When sending on to recipient domains... does the SMTP server need to authenticate to say, the gmail SMTP server as a user in order to send mails over there? That seems weird, since it's not a user logging into gmail to send mails, it's an SMTP server that is authorized within SPF sending mail from my domain (From address from my domain as well) to where ever the app server user's email is based (in this example, the user would be e.g., some_user#gmail.com).
I tried using python's aiosmtpd command-line and telnet to send a mail from test#MY_DOMAIN.TLD to test#MY_DOMAIN.TLD and it didn't seem to deliver the message; I figured aiosmtpd would connect to the preferred MX servers for my domain (my "real" MX's) to transfer the message, which would then put it in my inbox. That didn't seem to be the case, and I'm not sure why.
Exact repro steps, where example.com is my domain, and terminals are running on a box with a hostname listed in my MX records.
Terminal A:
$ aiosmtpd -n
Terminal B:
$ telnet localhost 8025
EHLO <example.com>
MAIL FROM: test#example.com
RCPT TO: test#example.com
DATA
FROM: Application Notifications <test#example.com>
TO: User Name <test#example.com>
SUBJECT: App Notify Test
This is a test!
.
QUIT
How do SMTP servers normally send mail between each other? Do they each get some login to each other's SMTP servers to authenticate with, and since I'm not doing that, this is a problem? Can I run a SMTP server on localhost and have it send mail out of the network without receiving mails (a no-reply service)? Is there something obvious that I'm just missing here that solves all my problems?
Thanks
It sounds like you want to run a mail transfer agent (MTA) that relays email to remote SMTP servers. An MTA will typically act as an SMTP server to receive messages, and then it will act as an SMTP client when it relays the messages to remote hosts.
MTAs generally operate in two different modes: (1) They will relay messages from authenticated users to remote hosts, and (2) they will receive messages from remote hosts to its users and store them somehow. The combination of those two modes - where the MTA will accept messages from remote hosts and relay them to different remote hosts - is called an open relay and is sure to attract spammers and place your server on spam blacklists.
aiosmtpd is not an MTA or an email relay out of the box - it is merely an SMTP server that will receive messages and do whatever with the messages you program it to do. By default it will do nothing - that is, it will receive the messages and throw them away. If you want to implement an email relay in aiosmtpd, then you need to implement the SMTP client portion of the MTA, e.g. by implementing an aiosmtpd handler that instantiates smtplib.SMTP to connect to remote hosts.
However, if all you want is an email relay, then you most likely don't need aiosmtpd at all - postfix is probably a better choice.
aiosmtpd can be a good choice if you need to implement mailing list software or perform some automation tasks based on incoming emails from e.g. cameras or scanners.
If you want to implement an email relay in aiosmtpd, then you need to ensure that both the software and your server are configured in a way that you don't relay unauthenticated messages from the outside internet.
See also: Python aiosmtpd - what is missing for an Mail-Transfer-Agent (MTA)?
So, I actually figured out what was missing here.
I need to run an SMTP server, yes, but I also needed to write code to parse the "to" domain (the recipient domain), perform a DNS request for the MX server(s) of the recipient domain, and then use the smtplib client to then send mail over to the recipient domain. Authentication is not needed to relay that message to the recipient server, authentication is only required for reading from a given inbox or authenticating a sender to send on behalf of a domain (I trust myself and myself only to send mail). I can do all this while also only listening for mail on localhost so that only my local server can use the local SMTP server for relaying messages/emails off to recipient domains.
Additionally, I don't need to have my external IP listed as an MX server since it's not accepting mail for the domain, only sending. I do need an SPF record for it though so that it is an authorized relay/sender for email from my domain.
Our email server was blocked, I need find a way to solve this.
About the outbound SMTP IP, when I send emails from my email server, if I send them to a SMTP relay on my other server, which IP the receive side will check for the reverse DNS? the email server IP or the relay server IP?
The receiving mail server will do a reverse dns check on the mail server that it received the message from. So, if your mail server sends a message through a relaying mail server, which then sends the message to the receiving mail server, the receiving mail server will do a reverse dns check on the relaying mail server.
The reverse DNS check works like so: Say the receiving mail server receives an incoming connection from 209.85.160.179. The receiving mail server will do a reverse DNS lookup on this IP, which (in this case) will resolve to mail-yk0-f179.google.com. To confirm that the sending mail server is in fact in the google.com domain (and 209.85.160.179 is not run by a spammer or spoofer who simply setup a PTR record for 209.85.160.179 that points to mail-yk0-f179.google.com), the receiving mail server then does a forward DNS lookup on mail-yk0-f179.google.com, to make sure that this FQDN does in fact resolve to 209.85.160.179 (a spammer would not be able to setup a DNS record for a FQDN in google.com's domain).
So, legitimate senders should have a PTR record setup for the IP address that their mail server sends from, and the FQDN that the PTR record points to should resolve back to this same IP. This is how spam filters identify mail from home computers that have been taken over by bots or zombies for sending spam - as most IPs used for home internet connections will not pass a reverse DNS check.
I have two websites, we'll call them a.com and b.com. I already have a mail server running on b.com. What I would like to do is have any email sent to #a.com to be automatically forwarded to b.com. I edited the only MX record in the a.com DNS settings to be...
Priority: 10
Host: #
Points To: b.com
After setting this up and giving it time to propagate, mail sent to a.com does not go through and I get a "Message not delivered" response from my email client.
Thanks for any help!
The MX-Record does only tell the webserver where to deliver mail for that specific domain. So any mail sent to user#a.com will be sent to the mailserver at b.com, that does not mean that they get forwarded to user#b.com. You will have to configure your mailserver at b.com to accept mails for the a.com domain.
Dear, I torn my hair since last 7-8 days to know the working of receiving mail server. The word "Receiving Mail Server" might sound confusing to you in the first instance. So let me first make you clear on that.
Complete Mail service works as follows:
We send email using a MTA (mail transfer agent) like "sendmail" on Linux or "Outlook" on windows.
Both these MTA deliver email to the SMTP server, which further delivers the email to the destination.
Let say we are sending email to "munendra#mydomain.com", SMTP must connect to the domain "mydomain.com" on some TCP socket, and there must be some service listening on the server "mydomain.com" to receive the emails. I want to know what this service is?? On what port does it runs??
This stage is what I mean to say the "Receiving Mail Server".
POP/IMAP comes secondary after the email delivers on the server, their primary function is to fetch the email from server to the local computer.
Thanks a lot in advance!!
That service - which listens to receive e-mails on mydomain.com - will be the local SMTP service, which listens on TCP port 25.
You send e-mail through SMTP protocol to an SMTP server which receives your e-mail from your SMTP client. The SMTP service will deliver/store it for the appropriate mailbox, ready to be picked up - over a different protocol, POP3, IMAP - by the mail client.
As I know the email message sending process looks like this:
The message is sent from sender's client to his server (say mail.com).
The mail.com server redirects the message to recipients server (say gmail.com).
Recipient's client connects to gmail.com and recieves new messages.
I'm interested in stage 2.
I thought that the servers use SMTP protocol to make this negotiation. And gmail.com server should listen to 25 port to recieve a connection from mail.com and other servers.
But then I try to connect gmail.com (exactly, Google service) on port 25, the server is not responding...
Why? What's wrong?
You need to delve into MX records a bit: Oversimplified DNS and MX record. The SMTP gateways use the MX records to route emails between each other. So, for a domain X the SMTP gateway might be mail.X : in other words, for gmail.com the mail gateway might be mail.gmail.com (example).
Mail servers do not redirect.
You have to use MX records to find a mail server. You can't just assume that there will be one on the bare domain name.
But then the mail server will either accept or reject your message. In some cases, usually only between very close and trusted friends, the mail server might RELAY your message onward to another mail server.
At one time, in the mid 90's, just about any mail server would accept a message and relay it. Many people relied on this behavior because IP packets would only travel 30 hops, and the edges of the Internet were more than 30 hops apart. For instance, Peru and Ukraine could not contact each other with IP packets, but they could both send mail to a server at aol.com and that mail server would relay it on to its destination.
It is best not to play around with email unless you read some modern up-to-date descriptions of how it works, and perhaps check the white papers at maawg.org. Also, many domains now expect strangers to submit their email via port 587 and authentication is required.