N
N
New_account2018-08-20 12:52:53
Python
New_account, 2018-08-20 12:52:53

SMTPSenderRefused error. How to fix?

I am writing a bulk mailing program. Here is the code

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText

print('Войдите в свой аккаунт')
username = input("Username: ")
password = input("Password: ")
mail_sender = username
mail_receiver_input = input("Введите путь к файлу с адресами получателей: ")
header = input("Введите тему письма: ")
message_input = input("Введите путь к файлу с образцом письма: ")
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.ehlo()
try:
    server.login(username, password)
except smtplib.SMTPAuthenticationError:
    print("Неверный пароль или логин")

try:
    for mail_receiver in open(mail_receiver_input, 'r'):
        message = open(message_input, 'r').read()
        mag = MIMEText(message, 'html', 'utf-8')
        mag["Subject"] = header
        mag['From'] = mail_sender
        mag['To'] = mail_receiver
        server.sendmail(mail_sender, mail_receiver, mag.as_string())
except FileNotFoundError:
    print("Не найдено файла или папки")
server.quit()

With a small number of emails, everything works fine, but if you feed her 100+ emails, this error pops up
smtplib.SMTPSenderRefused: (421, b'4.7.0 Try again later, closing connection. (MAIL) p89-v6sm20290047wrc.97 - gsmtp', '[email protected]')

Off. The documentation says that the error means "Sender's address is rejected". I dug into the Gmail settings and found this item. 5b7a8f19c5f16654723358.png
The switch is in the "Do not limit" position. So what's the problem? To whom it is not difficult I ask to answer my question.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tema_sun, 2018-08-20
@tema_sun

Imap has nothing to do with it. Gmail will not let you send a lot of emails and will interfere with this in every possible way. Use a dedicated service.
By the way, if you configure DKIM, SPF and DMARC normally, then you can send using regular sendmail.

A
Artem Melnykov, 2019-03-03
@NickProgramm

Go to the site solkogan.ru there is an article about sending mail through python where the author painted everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question