Skip to content

Instantly share code, notes, and snippets.

@carlosalvarez91
Last active December 4, 2019 00:35
Show Gist options
  • Save carlosalvarez91/0ce69058e16491931c9a4142f4670ab9 to your computer and use it in GitHub Desktop.
Save carlosalvarez91/0ce69058e16491931c9a4142f4670ab9 to your computer and use it in GitHub Desktop.
Send verification email
#settings.py -------------------------------------------------------------------------------------
#https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'# ?
EMAIL_HOST_USER = 'whateveremail@test.com'
EMAIL_HOST_PASSWORD = 'Yourpassword12345'
EMAIL_PORT = 587 #?
SITE_NAME = "The Name of your app"
#verify_account_email.txt------------------------------------------------------------------------
{% load i18n %}{% blocktrans %}Thanks so much for joining {{site_name}}! To finish signing up, you just need to confirm that we got your email right.
If you did NOT request to reset your password just IGNORE this email.
Click the link below to confirm your email.{% endblocktrans %}
{{ site_url }}/{{site_dir}}?token={{temp_key}}
#schema.py--------------------------------------------------------------------------------------------------
from django.core.mail import EmailMessage
from django.contrib.auth import get_user_model
from django.conf import settings
from django.template.loader import render_to_string
from graphql_jwt.utils import jwt_encode, jwt_payload, jwt_decode
import graphene
from graphene_django import DjangoObjectType
class UserSchema(DjangoObjectType):
class Meta:
model = get_user_model()
class Query(graphene.ObjectType):
send_verification_email = graphene.Field(UserSchema, email=graphene.String(required=True))
def resolve_send_verification_email(self,info, email):
try:
user = get_user_model().objects.get(email__iexact=email, is_active=True)
except UserrModel.DoesNotExist:
raise GraphQLError('No User with this email!')
#https://django-graphql-jwt.domake.io/en/stable/settings.html#pyjwt
# Note: Try to use the email in the payload, instead of the username
#payload = {'email':email} -> not valid
payload = jwt_payload(user, context=None)
token = jwt_encode(payload, context=None)
print('token:.............. ' + token)
welcome = "Welcome to " + settings.SITE_NAME
header = _(welcome)
message = render_to_string("verify_account_email.txt", {
"user": user,
"temp_key": token,
"site_url": settings.CLIENT_URL,
"site_dir": "login",
"site_name": settings.SITE_NAME
})
emailtosend = EmailMessage(header, message, to=[email])
emailtosend.send()
return user
'''
query sendVerificationEmail{
sendVerificationEmail(email:"test@mailintor.com"){
email
}
}
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment