Tell me, please, how can I send letters from any addressee? I have the following settings in .settings.py

EMAIL_HOST = 'smtp.yandex.ru' EMAIL_HOST_USER = 'yandexUser' EMAIL_HOST_PASSWORD = 'secretPassword' EMAIL_PORT = 587 EMAIL_USE_TLS = True 

Next in the shell, the following code works

send_mail('Django mail', 'This e-mail was send with Django.', 'yandexUser@ya.ru', ['mySecondMail@gmail.com'], fail_silently=False)

But this does not work

send_mail('Django mail', 'This e-mail was send with Django.', 'info@any-site.ru', ['mySecondMail@gmail.com'], fail_silently=False)

I have no problems with php, I also use Yandex or Google as a mail server, and I substitute any address as an addressee.


It turned out to do this:

 import smtplib def sendMail(email_from, email_to, subject, text): server = smtplib.SMTP("smtp.yandex.ru", 587) server.ehlo() server.starttls() server.login("myYandexAccount", "o_MyPassword") message = "\r\n".join([ \ "From: {}".format(email_from), \ "To: {}".format(email_to), \ "Subject: {}".format(subject), \ "", \ "{}".format(text) \ ]) server.sendmail("myYandexAccount@ya.ru", email_to, message) server.quit() sendMail( 'email_from@gmail.com', 'email_to@gmail.com', 'subject message', 'body text here...' ) 
  • What does "not working" mean? What error gives? - Sergey Gornostaev
  • send_mail('Django mail', 'This e-mail was send with Django.', 'test@gmail.com', ['myemail@gmail.com'], fail_silently=False) - wedoca
  • Traceback (most recent call last): ... smtplib.SMTPSenderRefused: (553, b'5.7.1 Sender address rejected: not owned by auth user.', 'test@gmail.com') - wedoca
  • 2
    There are no errors in the code. The server responds that it will not accept the letter, because the user through whom authorization was carried out does not own the address from which the sending attempt is made. - Sergey Gornostaev
  • So I understood. Is there any possibility to send letters from Django to any addressee? On my php site with this there are no problems at all. I also use the account in Yandex, and in the field "sender" I specify any address, and everything works fine. - wedoca

1 answer 1

Having a little rummaged in source codes found the solution

 from django.core.mail import EmailMessage msg = EmailMessage( subject=u'Тема письма', body=u'тело сообщения тут', from_email='yandexUser@ya.ru', to=('email_to@gmail.com',), headers={'From': 'email_from@me.com'} ) msg.content_subtype = 'html' msg.send()