Skip to content

Commit

Permalink
Added SMTP support for production env
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben69695 committed Jun 23, 2022
1 parent edbc0b2 commit 22b3dbf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/services/emailService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
result = send_mail(self.subject, self.message, self.fromMail, self.recipient_list, False, connection=emailBackend)
self.sended = result == 1
8 changes: 5 additions & 3 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
return render(request, "core/contact.html")
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions webpersonal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 22b3dbf

Please sign in to comment.