-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from RoboTeamTwente/29-switch-to-actual-email-…
…sending-instead-of-objects Send mail using noreply account from RoboTeam Twente
- Loading branch information
Showing
2 changed files
with
26 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,3 +180,14 @@ | |
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]')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -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']) | ||
|