Skip to content

Commit

Permalink
Merge pull request #30 from RoboTeamTwente/29-switch-to-actual-email-…
Browse files Browse the repository at this point in the history
…sending-instead-of-objects

Send mail using noreply account from RoboTeam Twente
  • Loading branch information
supertom01 authored Mar 17, 2023
2 parents 8b97d47 + 65ddaa7 commit a17f881
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
11 changes: 11 additions & 0 deletions future_factory_website/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,14 @@
# Email
if DEBUG:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
else:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

# Admins that should receive error mails
ADMINS = [('Tom Meulenkamp', '[email protected]')]
20 changes: 15 additions & 5 deletions main_site/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import datetime
from smtplib import SMTPException

from django.contrib import messages
from django.core.mail import send_mail, BadHeaderError, mail_admins
from django.shortcuts import redirect
from django.urls import reverse
from django.views import View
from django.views.generic import TemplateView

from events.models import Event
from future_factory_website.forms import ContactForm
from main_site.models import Email, PressPicture
from main_site.models import PressPicture
from news_articles.models import NewsArticle
from teams.models import Team

Expand Down Expand Up @@ -50,10 +52,18 @@ def post(self, request, slug=None):
contact_form = ContactForm(request.POST)

if contact_form.is_valid():
Email.objects.create(email_sender=contact_form.cleaned_data['sender_mail'],
recipient=self.team,
message=contact_form.cleaned_data['message'])
messages.success(request, message="Your email was successfully send")
message = f"You got a new message from the Future Factory website!\n\nFrom: {contact_form.cleaned_data['sender_mail']}\n\nMessage: {contact_form.cleaned_data['message']}"
try:
send_mail(subject="[Future Factory Website] General contact form",
from_email='[email protected]',
recipient_list=[self.team.contact_mail if self.team else '[email protected]'],
message=message)
messages.success(request, message="Your email was successfully send")
except SMTPException as e:
messages.error(request, message="The server cannot send your email. Please try again later.")
mail_admins(subject="[Contact Form] Failed sending email", message=str(e))
except BadHeaderError:
messages.error(request, message="Your email seems to be malformed, please try again.")
else:
for field in contact_form.errors:
messages.error(request, message=contact_form.errors.get_json_data()[field][0]['message'])
Expand Down

0 comments on commit a17f881

Please sign in to comment.