diff options
| author | Bobby <[email protected]> | 2023-01-21 21:30:25 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-01-21 21:30:25 -0500 |
| commit | 2ec768c7964e82863a85ca3a15ce4bfefb2129de (patch) | |
| tree | 9ef75d96ce029f9fe4393385f175cc58c6a3f19a | |
| parent | 2f5cb0fe049353f51ea1b1ac1f448fce7a62014f (diff) | |
| download | thatcomputerscientist-2ec768c7964e82863a85ca3a15ce4bfefb2129de.tar.xz thatcomputerscientist-2ec768c7964e82863a85ca3a15ce4bfefb2129de.zip | |
Configure Oracle Cloud Email Delivery
| -rw-r--r-- | thatcomputerscientist/settings.py | 6 | ||||
| -rw-r--r-- | users/forms.py | 10 | ||||
| -rw-r--r-- | users/mail_send.py | 69 | ||||
| -rw-r--r-- | users/views.py | 26 |
4 files changed, 97 insertions, 14 deletions
diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py index 100c398b..8c8ac200 100644 --- a/thatcomputerscientist/settings.py +++ b/thatcomputerscientist/settings.py @@ -159,7 +159,11 @@ STATICFILES_DIRS = [ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' EMAIL_USE_TLS = True -EMAIL_HOST = os.getenv('MAIL_HOST') +# EMAIL_HOST = os.getenv('MAIL_HOST') +EMAIL_HOST = os.getenv('ORACLE_SMTP_HOST') EMAIL_PORT = 587 EMAIL_HOST_USER = os.getenv('EMAIL_USER') EMAIL_HOST_PASSWORD = os.getenv('EMAIL_PASSWORD') + +USERNAME_SMTP = os.getenv('ORACLE_SMTP_USER') +PASSWORD_SMTP = os.getenv('ORACLE_SMTP_PASSWORD') diff --git a/users/forms.py b/users/forms.py index 43b9230a..8593a605 100644 --- a/users/forms.py +++ b/users/forms.py @@ -3,13 +3,13 @@ from django import forms from django.contrib.auth.models import User from users.models import UserProfile -from django.core.mail import send_mail from django.conf import settings from django.template.loader import render_to_string from django.utils.html import strip_tags from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode from .tokens import account_activation_token +from .mail_send import send_email class RegisterForm(forms.Form): username = forms.CharField(label='Username', max_length=30, min_length=4) @@ -63,9 +63,11 @@ class RegisterForm(forms.Form): 'domain': request.get_host(), }) message = strip_tags(message) - send_mail(subject, message, 'That Computer Scientist <' + settings.EMAIL_HOST_USER + '>', [user.email], fail_silently=False) - - return user + # send_mail(subject, message, 'That Computer Scientist <' + settings.EMAIL_HOST_USER + '>', [user.email], fail_silently=False) + if (send_email(sender='[email protected]', sender_name='That Computer Scientist', recipient=user.email, subject=subject, body_html=message, body_text=message)): + return user + else: + return user class UpdateUserDetailsForm(forms.Form): first_name = forms.CharField(label='First name', max_length=30, required=False, widget=forms.TextInput(attrs={'placeholder': 'First name'})) diff --git a/users/mail_send.py b/users/mail_send.py new file mode 100644 index 00000000..742f84f5 --- /dev/null +++ b/users/mail_send.py @@ -0,0 +1,69 @@ +# python script for sending SMTP configuration with Oracle Cloud Infrastructure Email Delivery +import smtplib +import email.utils +from email.message import EmailMessage +import ssl +from django.conf import settings + +def send_email(sender, sender_name, recipient, subject, body_html, body_text): + # Replace [email protected] with your "From" address. + # This address must be verified. + # this is the approved sender email + SENDER = sender + SENDERNAME = sender_name + + # Replace [email protected] with a "To" address. If your account + # is still in the sandbox, this address must be verified. + RECIPIENT = recipient + + # Replace the USERNAME_SMTP value with your Email Delivery SMTP username. + USERNAME_SMTP = settings.USERNAME_SMTP + + # Put the PASSWORD value from your Email Delivery SMTP password into the following file. + PASSWORD_SMTP = settings.PASSWORD_SMTP + + # If you're using Email Delivery in a different region, replace the HOST value with an appropriate SMTP endpoint. + # Use port 25 or 587 to connect to the SMTP endpoint. + HOST = settings.EMAIL_HOST + PORT = settings.EMAIL_PORT + + # The subject line of the email. + SUBJECT = subject + + # The email body for recipients with non-HTML email clients. + BODY_TEXT = body_text + + # The HTML body of the email. + BODY_HTML = body_html + + # create message container + msg = EmailMessage() + msg['Subject'] = SUBJECT + msg['From'] = email.utils.formataddr((SENDERNAME, SENDER)) + msg['To'] = RECIPIENT + + # make the message multi-part alternative, making the content the first part + msg.add_alternative(BODY_TEXT, subtype='text') + # this adds the additional part to the message + # According to RFC 2046, the last part of a multipart message, in this case + # the HTML message, is best and preferred. + msg.add_alternative(BODY_HTML, subtype='html') + + # Try to send the message. + try: + server = smtplib.SMTP(HOST, PORT) + server.ehlo() + # most python runtimes default to a set of trusted public CAs that will include the CA used by OCI Email Delivery. + # However, on platforms lacking that default (or with an outdated set of CAs), customers may need to provide a capath that includes our public CA. + server.starttls(context=ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=None, capath=None)) + # smtplib docs recommend calling ehlo() before & after starttls() + server.ehlo() + server.login(USERNAME_SMTP, PASSWORD_SMTP) + # our requirement is that SENDER is the same as From address set previously + server.sendmail(SENDER, RECIPIENT, msg.as_string()) + server.close() + # Display an error message if something goes wrong. + except Exception as e: + return e + else: + return True
\ No newline at end of file diff --git a/users/views.py b/users/views.py index 61724bab..eb5305b0 100644 --- a/users/views.py +++ b/users/views.py @@ -4,16 +4,15 @@ from django.contrib.auth import authenticate, login, logout, update_session_auth from django.contrib import messages from .models import UserProfile from django.contrib.auth.models import User -from django.core.mail import send_mail from django.conf import settings from django.template.loader import render_to_string from django.utils.html import strip_tags from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode -from django.contrib.sites.shortcuts import get_current_site from .tokens import account_activation_token, EmailChangeTokenGenerator from django.utils.http import urlsafe_base64_decode from .forms import UpdateUserDetailsForm +from .mail_send import send_email # Create your views here. def login_user(request): @@ -134,7 +133,6 @@ def send_verification_email(request): username = request.POST['username'] user = User.objects.get(username=username) - subject = 'Verify your email address' message = render_to_string('verification_email.html', { 'user': user.username if user.first_name is None else user.first_name, @@ -145,9 +143,13 @@ def send_verification_email(request): 'domain': request.get_host(), }) message = strip_tags(message) - send_mail(subject, message, 'That Computer Scientist <' + settings.EMAIL_HOST_USER + '>', [user.email]) - messages.success(request, 'Verification email was sent! Please check your email.', extra_tags='loginError') - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + + if (send_email(sender='[email protected]', sender_name='That Computer Scientist', recipient=user.email, subject=subject, body_html=message, body_text=message)): + messages.success(request, 'Verification email was sent! Please check your email.', extra_tags='loginError') + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + else: + messages.error(request, 'Unable to send verification email! Please try again later.', extra_tags='loginError') + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) def verify_email(request, uidb64, token): try: @@ -194,9 +196,15 @@ def send_change_user_email(request): 'domain': request.get_host(), }) message = strip_tags(message) - send_mail(subject, message, 'That Computer Scientist <' + settings.EMAIL_HOST_USER + '>', [new_email]) - messages.success(request, 'Verification email was sent! Please check your email.') - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + # send_mail(subject, message, 'That Computer Scientist <' + settings.EMAIL_HOST_USER + '>', [new_email]) + + if (send_email(sender='[email protected]', sender_name='That Computer Scientist', recipient=new_email, subject=subject, body_html=message, body_text=message)): + messages.success(request, 'Verification email was sent! Please check your email.') + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + else: + messages.error(request, 'Unable to change email! Please try again later.') + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + else: messages.error(request, 'Unable to change email! Please try again later.') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) |
