Your understanding of how mail works is roughly correct. Some additional notes that may clear things up:
-
SMTP is used for two distinct purposes. You seem to be confusing these two.:
-
The first use, typically called “submission”, is to send a mail from an MUA (Mail User Agent, your mail program, Outlook, Thunderbird, …) to an MTA (Mail Transfer Agent, typically called “mail server”). MTAs are run by your ISP, or by mail-providers such as GMail. Typically, their use is restricted by either IP address (only customers of said ISP can use it), or username/password.
-
The second use is to send mail from one MTA to another MTA. This part is, usually, wide open, since you are probably willing to accept inbound mail from anyone. This is also the location where anti-spam measures are taken.
-
In order to send a mail, you need, at least, the second part of SMTP: the ability to talk to another MTA to deliver the mail.
The typical way to send mails is to compose the mail in your application, then send it off to an MTA mail server for delivery. Depending on your setup, that MTA can be either installed on the same machine as your Python code is running on (localhost), or can be a more “central” mail server (possibly requiring authentication).
“Your” MTA will take care of all the nasty details of delivering mail such as:
-
Doing DNS lookups to find out the MTA’s to contact to relay the mail. This includes MX-lookup, but also other fallback mechanisms such as A-records.
-
Retrying delivery, if the first attempt fails temporarily
-
Generating a bounce message, if the message fails permanently
-
Make multiple copies of the message, in case of multiple recipients on different domains
-
Signing the message with DKIM to reduce the chance of it being marked as SPAM.
-
…
You could, of course, re-implement all these features within your own Python code, and effectively combine an MTA with your application, but I strongly advise against it. Mail is surprisingly hard to get right…
Bottom line: Try to send the mail via SMTP to the mail server of your provider or another mail service. If that is not possible: think really hard if you want to run your own mail server. Being marked as a spammer happens easily; getting removed from spam-lists is much harder. Don’t re-implement SMTP-code in your application.