In the majority of the projects, you will need to automatically send emails to your users or to yourself. For my project, I decided to use an external SMTP service. I thought it would be fast and convenient, without any problems with settings.
Oh, I was wrong.
There are a lot of services available for that. For my project, I decided to use Yandex.Mail mainly because of the price. Google wants you to pay for each mailbox, and Yandex does not.
I registered my domain on Yandex. Keep in mind that you need already registered Yandex email for that (or you can create a new one). I already had one. Then I added all the necessary DNS records to verify the ownership and set up DKIM. There are detailed instructions on the Yandex website. I will not duplicate it here because they can change something and everything that I wrote will become outdated.
I use Django in my project, so I tried to send an email with Django built-in function. I used settings from the official documentation but sending via smtp.yandex did not work. And there was silence in the logs.
Settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.yandex.ru"
EMAIL_PORT = 465
EMAIL_HOST_USER = os.environ['EMAIL_USER']
EMAIL_HOST_PASSWORD = os.environ['EMAIL_PASSWORD']
EMAIL_USE_TLS = True
I didn’t have much time to dig into it, so I decided to use Postfix instead. The set up was simple and everything looked fine. But after a couple of days, I discovered a problem with sending emails to Gmail emails. I tried to fix the error by following the instructions on the google site, but it didn’t help. After a couple of attempts, I decided to not use Postfix too - I was concerned that Google will blacklist the project email.
At this stage, my only option was to use Sendmail. Fast and convenient, no pain, remember? I can write a separate post full of pain and suffering about setting up Sendmail. I’ve spent a couple of days to make it work but without any success. Desperately, I had to return where it all started — Django.
The problem — it didn’t send an email and logs were empty—still was there. I decided to use telnet to check what is going on. And when I tried to send an email with my login/password I saw the authentication error. But how could it happen? I was sure the login and password are correct!
At this stage, my friend Ilya helped me. He suggested isolating the problem. We created a new Django project with one view method in it, which was just sending an email. For test purposes, we decided to use another email. And suddenly it worked fine! We compared the settings and found some difference:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.yandex.ru"
EMAIL_PORT = 465
EMAIL_HOST_USER = os.environ['EMAIL_USER']
EMAIL_HOST_PASSWORD = os.environ['EMAIL_PASSWORD']
EMAIL_USE_TLS = False # Here was True
EMAIL_USE_SSL = True # I added this parameter
I updated settings in my project and did another try. Still no luck. However, it looked like settings change enabled logging, so I started to see error messages. It was also an authentication issue, similar to one I identified using telnet.
I remembered that some time ago I created a password for applications for my Yandex email. It is a second password which you can use to access mail from the phone. And I used the same email to create an organization in Yandex.Mail! I removed the password for applications and it solved the problem.
In the end, I made some conclusions:
- To debug something weird, try to isolate the problem as much as possible.
- Don’t hesitate to ask for help.