2

Is it possible to send an email directly from my computer to a Hotmail or Gmail (or any other) recipient, without connecting to a third-party sending server (SMTP)?

i.e. would it be possible that my computer directly connects to gmail.com if the recipient is example@gmail.com, etc.

I tried this:

import smtplib

def mailsend(FROM, TO, SUBJECT, TEXT):
    message = "From: %s\nTo: %s\nSubject: %s\n%s" % (FROM, TO, SUBJECT, TEXT)
    server = smtplib.SMTP('localhost')
    server.sendmail(FROM, TO, message)
    server.quit()

mailsend("test@example.com", "example@gmail.com", "Hello", "First email")

but I get

Errno 10061 : No connection could be made because the target machine...

(the reason is probably that localhost doesn't have any email-sending server, but my question is: is it possible to send emails without such an email-sending server, but directly to the recipient's server?)

How to do a "direct-to-MX" mail in Python? (as described here)

Notes:

  • I'm running it from Windows, I don't have a local SMTP / Sendmail / Postfix.

  • I know it's usually a bad idea to do this (the recipient will very likely reject the email I send, because I don't have any DKIM, SPF, etc.), and I do know that it's a very difficult task to run a good-delivery-rate mail server, but I just wanted to know if, technically, it's possible

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 2
    No. You can run your own SMTP Server, or use GMAILs SMTP server if you have an account with gmail. – Grantly Jan 09 '18 at 13:20
  • As said above. But note, that if you run your own SMTP server (for example on the local machine), gmail or other recipient can flag your email as spam, or simple decline the connection. – Laszlowaty Jan 09 '18 at 13:28
  • @Grantly I have a Gmail account, and I *could* connect to their SMTP and use this usual method, but my question was: is it possible to make a Python script connect directly the recipient's server without third-party SMTP. – Basj Jan 09 '18 at 13:29
  • 2
    @Grantly: you do not know what you are talking about. –  Jan 09 '18 at 13:29
  • I believe you can implement SMTP (https://tools.ietf.org/html/rfc5321) protocol using python. So in theory it is possible. – Laszlowaty Jan 09 '18 at 13:30
  • @Laszlowaty You don't have to -- the standard library module `smtplib` which the OP is already using already does exactly that. – tripleee Jan 09 '18 at 13:32
  • @Laszlowaty Do you think that maybe I could use https://stackoverflow.com/a/2691249/1422096? – Basj Jan 09 '18 at 13:32
  • No, that's a completely different thing. A sink server is something you use for testing which simply accepts and discards the messages you send. – tripleee Jan 09 '18 at 13:33
  • @tripleee: that's what I thought, but `smtplib` seems to be a client, not a SMTP server. – Basj Jan 09 '18 at 13:33
  • Your question asks about sending, not receiving. That's the role of a client in SMTP. – tripleee Jan 09 '18 at 13:33
  • Possible duplicate of [What is the proper way to send an email \*directly\* using the SMTP protocol? (bypass MTA on my end)](https://stackoverflow.com/questions/1691439/what-is-the-proper-way-to-send-an-email-directly-using-the-smtp-protocol-byp) – tripleee Jan 09 '18 at 13:34
  • The proposed duplicate talks about many things which `smtplib` already handles for you so it's way too detailed. But the gist of the answers is that while this is *technically* doable, there is probably no good way to do it in today's world unless you want to spend all of your time specializing in operating a successful mail delivery service. – tripleee Jan 09 '18 at 13:35
  • @tripleee: It's not really a duplicate, this question here is about implementation in Python of a "direct-to-MX" email transmission, as [described here](https://stackoverflow.com/a/1691482/1422096) – Basj Jan 09 '18 at 13:40
  • 2
    Well if you replace `localhost` with a proper MX lookup as indicated in that answer, the answer is "technically yes" but there is nothing really specific about Python in your question and so I continue to think you should just accept the duplicate, also because no answer you receive here will probably tell you anything you cannot find in answers to that question. – tripleee Jan 09 '18 at 13:42
  • The missing piece is another duplicate; https://stackoverflow.com/questions/4336849/mx-record-lookup-and-check – tripleee Jan 09 '18 at 13:44
  • And of course the inductive proof: Some MTAs done in Python, some with fairly robust MX selection, retry logic, queue handling for pending messages, etc. https://stackoverflow.com/questions/784201/is-there-a-python-mta-mail-transfer-agent – tripleee Jan 09 '18 at 13:47
  • @tripleee Thanks to the missing piece you pointed me, I finally managed to do it, see here: https://stackoverflow.com/a/48169788/1422096, but the bad news is that the recipient server is rejecting me ;) `Recipient address rejected: Access denied`. – Basj Jan 09 '18 at 13:58

1 Answers1

2

If you used the MX of the recpient's domain instead of 'localhost', your code would technically "work."

BUT: you should not do this! Sending email involves far more than just one SMTP connection. Use the smtp relay of your internet provider or a similar server - everything else is a really bad idea.

The code would be the same, but instead of 'localhost' or the recipient's mx, you would use your own mx for every connection.

BTW: you are missing an extra \n (newline) after the subject header.