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'