1

I'm trying to send email from Django. My Django settings configurations are as follows:

# SMTP Settings
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "[email protected]" # my email address goes here.
EMAIL_HOST_PASSWORD = "my_generated_password" # generated password
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "[email protected]"

However when I'm trying to send email from it either using celery or directly from views it says "[Errno 61] Connection refused".

N.B: I'm using a mac matchine for the development. Is there any security reason for send email using Mac.

Views Code Sample:

def send_mail_to_all(request):

    send_mail('Celery Test Mail Subject', 
        "Test Mail Body", 
        from_email='[email protected]',
        recipient_list=['[email protected]'],
        fail_silently=False
        )

    # send_mail_func.delay()
    return HttpResponse('Sent')

Celery Task Schedular Code:

@shared_task(bind=True)
def send_mail_func(self):
    users = get_user_model().objects.all()
    for user in users:
        mail_subject = "Celery Testing"
        message = f"This is the celery testing message at {datetime.now()}"
        to_email = user.email
        send_mail(mail_subject, 
        message, 
        from_email=settings.EMAIL_HOST_USER,
        recipient_list=[to_email,],
        fail_silently=False
        )
    return 'Done'

1 Answer 1

0

This can be due to your security preferences in Google.

Please try changing your Google Account settings like follows:

  • Go to your Google Account settings
  • Find Security -> Account permissions -> Access for less secure apps
  • Enable let less secure apps sign in option.

https://support.google.com/accounts/answer/6010255

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.