diff --git a/core/services/emailService.py b/core/services/emailService.py index 5ecaae5..b4bc9dc 100644 --- a/core/services/emailService.py +++ b/core/services/emailService.py @@ -12,4 +12,5 @@ def __init__(self, fromMail, recipient_list, subject, message): def send(self): emailBackend = mail.get_connection() - send_mail(self.subject, self.message, self.fromMail, self.recipient_list, False, connection=emailBackend) \ No newline at end of file + result = send_mail(self.subject, self.message, self.fromMail, self.recipient_list, False, connection=emailBackend) + self.sended = result == 1 diff --git a/core/views.py b/core/views.py index f892269..86113d4 100644 --- a/core/views.py +++ b/core/views.py @@ -1,6 +1,7 @@ from django.shortcuts import render, HttpResponse from .forms import ContactMessageForm from .services.emailService import EmailService +from django.conf import settings # Create your views here. def home(request): @@ -17,11 +18,12 @@ def createContactMessage(request): form = ContactMessageForm(request.POST) if form.is_valid(): new_message = form.save() - subject = new_message.name + ' needs your attention' - service = EmailService(new_message.email, ['ruben.arre6@gmail.com'], subject, new_message.message) + fromEmail = settings.DEFAULT_FROM_EMAIL + subject = new_message.name + ', with email (' + new_message.email + ') needs your attention' + service = EmailService(fromEmail, [fromEmail], subject, new_message.message) service.send() if service.sended: print('Email sended correctly') - return render(request, "core/contact.html") \ No newline at end of file + return render(request, "core/contact.html") diff --git a/docker-compose.yml b/docker-compose.yml index e968583..803b22f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,12 @@ services: - DEBUG=True - PGSSL=False - SECRET_KEY=${DJANGO_SECRET_KEY} + - EMAIL_HOST=${EMAIL_HOST} + - EMAIL_PORT=${EMAIL_PORT} + - EMAIL_HOST_USER=${EMAIL_HOST_USER} + - EMAIL_HOST_PASSWORD=${EMAIL_HOST_PASSWORD} + - EMAIL_USE_TLS=${EMAIL_USE_TLS} + - DEFAULT_FROM_EMAIL=${DEFAULT_FROM_EMAIL} depends_on: - db diff --git a/webpersonal/settings.py b/webpersonal/settings.py index 3a52ff5..8ff982e 100644 --- a/webpersonal/settings.py +++ b/webpersonal/settings.py @@ -132,5 +132,10 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media") # Email backend -EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' -#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' if DEBUG else 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_HOST = os.environ.get('EMAIL_HOST', None) +EMAIL_PORT = int(os.environ.get('EMAIL_PORT', '0')) +EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', None) +EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', None) +EMAIL_USE_TLS = False if os.environ.get('EMAIL_USE_TLS', 'False') == 'False' else True +DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', None)