-1

Trying to send email message using python's smtplib via gmail:

import smtplib

msg = "\r\n".join([
    "From: " + email_host,
    "To: " + email_recipient,
    "Subject: subject",
    "",
    email_msg
])
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(email_host, email_pwd)
server.sendmail(email_host, email_recipient, msg)
server.quit()

For popular services such as yandex, yahoo and gmail it works perfect. But it's impossible to send a message to domain-based emails (login@some_domain.com).

What's the problem?

phen0menon
  • 2,354
  • 2
  • 17
  • 36

2 Answers2

0

Email services apply restrictions since there's no authentication when you're sending those emails.

E.g. https://support.asperasoft.com/hc/en-us/articles/216128488-How-to-use-Gmail-as-an-SMTP-relay-for-notification-testing

This SMTP server does not require TLS or authentication. It is restricted because emails can only be sent to other Gmail addresses or Google apps users--therefore it may only make sense for testing within an internal network that uses Gmail or Google apps. Limits apply per recipient.

I.e. these services don't mind sending emails within their own domain, but don't want malicious users sending to other domains.

richflow
  • 1,902
  • 3
  • 14
  • 21
  • The problem is that I cannot send a message to domain-based email via smtp gmail. How to find out solution? – phen0menon Nov 15 '18 at 12:31
  • depends on the service, but for example gmail will require authentication if you want to use their SMTP server: https://support.google.com/a/answer/176600?hl=en . Namely: "Your full Gmail or G Suite email address is required for authentication." and "Port 465 (SSL required) Port 587 (TLS required) Dynamic IPs allowed" – richflow Nov 15 '18 at 12:39
0

Well, searching for the answer, I found out that Google Mail service doesn't allow to send messages to domain-based emails via 587 SMTP port, because it requires to use TLS.

enter image description here

I decided to switch onto 465 SMTP port as said in this post, so it works now perfect! Also, for testing purposes, I used another Mail Service (Yandex), which supports only 465 SMTP port - it works as well.

phen0menon
  • 2,354
  • 2
  • 17
  • 36