Django-allauth: send welcome email on signup (without verification) - django

How can I send a welcome email to a user who signs up on a django app(using django-allauth). If I set ACCOUNT_EMAIL_VERIFICATION = ("mandatory"), it works fine, and the user gets a verification email. But since I dont require any email verification, so the user should simply signup and get a welcome email.
settings.py-
ACCOUNT_AUTHENTICATION_METHOD = ("email")
ACCOUNT_EMAIL_VERIFICATION = ("none")
ACCOUNT_SIGNUP_PASSWORD_VERIFICATION = False
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
EMAIL_CONFIRMATION_SIGNUP = True
ACCOUNT_EMAIL_REQUIRED =True
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = '/'
ACCOUNT_LOGOUT_ON_GET =False
ACCOUNT_LOGOUT_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = (ACCOUNT_EMAIL_REQUIRED)
SOCIALACCOUNT_AUTO_SIGNUP = True
SOCIALACCOUNT_AVATAR_SUPPORT = ( 'avatar' in INSTALLED_APPS)
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '##'
EMAIL_HOST_PASSWORD = '##'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Is there any setting I missed which sends the welcome signup mail? Or do I have to pass it through my views? Cant seem to figure out the way for this. Any help would be great. Thanks.

No there is no such settings, but you can listen to a user_signed_up signal, which will have the user and request in parameters. Once it received send an email to the user.
Put the below code some where in models.py file:
from allauth.account.signals import user_signed_up
from django.dispatch import receiver
#receiver(user_signed_up, dispatch_uid="some.unique.string.id.for.allauth.user_signed_up")
def user_signed_up_(request, user, **kwargs):
# user signed up now send email
# send email part - do your self

allauth indeed only sends confirmation mails. But, it does differentiate between the first (signup) confirmation mail and following ones (e.g. when the user adds a second email address).
For this purpose allauth has a "email confirmation at signup"
template (account/email/email_confirmation_signup_message.txt, account/email/email_confirmation_signup_subject.txt).
When using the builtin templates this hybrid confirmation/signup/welcome mail is identical to the regular email confirmation template, but you can override it and put your welcome message in there. Furthermore, set ACCOUNT_EMAIL_VERIFICATION to "optional".
If all of this does not fit your needs, then you can hookup to the user_signed_up signal and send a plain welcome mail yourself.

Related

Django contact form: email not sending

I'm currently building a contact form for my website in Django.
The only problem is that the email is not being sent when the form is submitted.
This is the code:
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "c******o#gmail.com"
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = '587'
views.py:
sender_name = form.cleaned_data['nome_completo']
sender_email = form.cleaned_data['email']
message = "{0} has sent you a new message:\n\n{1}".format(sender_name, form.cleaned_data['messaggio'])
send_mail('New Enquiry', message, sender_email, ['c******o#gmail.com'])
My thought:
Since I'm in a virtualenv, when I submit the form, the terminal displays that it was successfully submitted, and gets me back the code right, maybe the email is not sent because of that, or maybe because I'm using a gmail account? Thanks!
Since you are using an SMTP server, why not use the following backend:
django.core.mail.backends.smtp.EmailBackend
Also, if you are using Gmail, make sure of the following:
Two-Factor authentication is enabled on account
You are using an app password.
You can find these on the security tab on your google account page:
https://myaccount.google.com/security
So the issue is with EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
This outputs emails to your console for debugging instead of actually sending emails. (See https://docs.djangoproject.com/en/3.1/topics/email/#console-backend)
Don't set EMAIL_BACKEND to anything for production as it defaults to django.core.mail.backends.smtp.EmailBackend. Then your EMAIL_HOST settings will take effect and the email should be sent out.

Could not send email in django rest framework

I am developing password reset part of the project. I am using django_rest_passwordreset to get the password reset. I am using mailjet smtp. I could not send the email to the user.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'in-v3.mailjet.com'
# EMAIL_PORT = 465
EMAIL_PORT = 587
EMAIL_USE_TLS = True
# EMAIL_USE_SSL = True
EMAIL_HOST_USER = '5e4329460b3c88f1d24d19c3e7374548aa213da%asasd1asd'
EMAIL_HOST_PASSWORD = 'a6c5ab2515d6ae761253a396453530ba$42asasdasdaasdasd'
If I change the EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' to the EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' it is printing it to the console. I have no idea why it is not working.
the part of the code where I am trying to send an email.
#receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
# send an e-mail to the user
context = {
'current_user': reset_password_token.user,
'username': reset_password_token.user.firstname,
'email': reset_password_token.user.email,
'reset_password_url': "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)
}
# just checking if it works
send_mail('Hello from something', 'hello there', 'abdukhashimov#yandex.ru',
[reset_password_token.user.email, ], fail_silently=False)
# render email text
email_html_message = render_to_string('user_reset_password.html', context)
email_plaintext_message = render_to_string(
'user_reset_password.txt', context)
msg = EmailMultiAlternatives(
# title:
"Password Reset for {title}".format(title="Some website title"),
# message:
email_plaintext_message,
# from:
"noreply#somehost.local",
# to:
[reset_password_token.user.email]
)
msg.attach_alternative(email_html_message, "text/html")
msg.send()
I came up with a different solution. I used the google smtp service. I have followed the steps from this kinsta.com - steps to setup up google smtp.
Step1: The very first thing you will need to do is ensure that you have 2-step verification enabled on your primary Gmail account. Important: If you don’t do this you will get an invalid password error further below when trying to authenticate your email address. So first go and enable 2-step verification.
Step2: Next, you will need to generate an App password. You then use the app password in place of your personal Gmail password further below. This is the only way this process will work.
Step3: Now back in Gmail, go to settings, and can click on “Accounts and Import.” Then click on “Add another email address you own.”. Basically to the gmail and sign in with your account and go to the settings.
Step4: Enter your additional business name and business email that is on the custom domain.
(Extra Info). I usually use yandex mail and added it then it generated the followings.
Step5: It will then send an email confirmation code to the email you just added. You will need to click the link in the email to confirm it or manually enter the code (this proves that you are in fact the owner of the additional email account). And that’s it!
From my experience, you might need to tweak some settings from google if it did not work for you. For example, I read from other source, you might need to allow less secure apps from google. I have not done it as I was using yandex mail, I guess.
In case if you are not sure what to put in settings.py
EMAIL_HOST = 'smtp.yandex.ru' # in my case
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'added account'
EMAIL_HOST_PASSWORD = 'your password'
Credits to kinsta.com

'from_email' not showing with 'send_mail' smtp

I have set up smtp with gmail. When I use send_mail the from email is not showing up in the account receiving the email.
Django settings.py
# DEFAULT_FROM_EMAIL = 'sendTo#gmail.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'sendTo#gmail.com'
EMAIL_HOST_PASSWORD = '**********'
EMAIL_USE_TLS = True
Using
$ python manage.py shell
I send the mail as follows,
>>> from django.core.mail import send_mail
>>> send_mail('subject is', 'message is and is not 12342', 'fromEmail#gmail.com', ['sendTo#gmail.com'])
1
>>>
I am receiving this email in my gmail account, (which is the same gmail account used for the smtp), but the from email is showing up as the sendTo#gmail.com and should be fromEmail#gmail.com
When you send emails through google's SMTP servers you cannot change the from email field. It uses the same address that you provided for authentication.
If you want to change it you have to use either your own mail server or one of the numerous mail apis/servers available. Sendgrid, MailGun etc come to mind.
I solved the problem using this view:
def contact(request):
form = ContactForm(data=request.POST or None)
if form.is_valid():
subject = form.cleaned_data['sujet']
message = form.cleaned_data['message']
sender = form.cleaned_data['envoyeur']
msg_mail = str(message) + " " + str(sender)
send_mail(sujet, msg_mail, sender, ['corbin.julien#gmail.com'], fail_silently=False)
return render(request, 'blog/contact.html', locals())
I actually append the email of the sender to the message. You could even remove the sender argument from send_mail. You just have to make your EmailField mandatory to make sure you'll get the sender's email address.

from_email is not working in django EmailMessage and using EMAIL_HOST_USER instead

I am trying to use django's builtin Email class EmailMessage to send an email with attachment like below
def send_email(path, from_email, to_list, file_name):
subject = 'Output for csv file %s'%(file_name,)
body = 'Please find the attached output file your csv input file %s'%(file_name,)
message = EmailMessage(subject, body, from_email, to_list)
message.attach_file(path)
message.send()
send_email('/home/user/hello.csv', 'myself#gmail.com', ['client#gmail.com'], 'functional_test')
And my Email settings in settings.py are
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'actual#myhost.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
So from the above settings the EMAIL_HOST_USER was actual#myhost.com and when i am calling my send_email function i am specifying from_email as myself#gmail.com.
So when i received the email i am receiving it from actual#myhost.com instead of myself#gmail.com even though we specified different from_email.
Is there anything that i can do to receive the mails with from address as myself#gmail.com ?
Got the same issue, #HaydenCrocker is right.
It’s Gmail’s restrictionists, need to change another email server if really want to customers from_email.
if you are using Gmail, you need to add the email you want use as alias
Follow instructions below
https://support.google.com/a/answer/33327?product_name=UnuFlow&hl=en&visit_id=638041629555716720-783343126&rd=1&src=supportwidget0&hl=en
You have to add DEFAULT_FROM_EMAIL, see the documentation.

Is there a way to use django send_mail but the sender is based on a specific field in a django model?

Currently,I am using gmail's smtp and Django send_mail.
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'sample#gmail.com'
EMAIL_HOST_PASSWORD = 'samplepass'
EMAIL_USE_TLS = True
Whenever I use django's send mail, the sender is always based on the EMAIL_HOST_USER (in this case, sample#gmail.com).
What I want to happen is to modify the sender so that it will not use sample#gmail.com, instead it will use an email from the contact_email_address field in my Business_Profile Model.
#models.py
class Business_Profile(models.Model):
...
contact_email_address = models.EmailField()
...
Is there a way for this to happen? I don't want to add an email_address_password field in my Business_Profile class.
If it's not possible, I am open to suggestions such as using an email server. I have a production server running on nginx and I've heard of postfix. Can I achieve what I want using postfix? Or are there better and easy-touse mail servers instead of postfix?
Thanks for reading. This is my first time to ask a question.
You can give the sender address as a parameter to send_mail():
from django.core.mail import send_mail
profile = BusinessProfile.objects.get(pk=1)
send_mail('Subject here', 'Here is the message.', profile.contact_email_address,
['to#example.com'], fail_silently=False)
Read more at https://docs.djangoproject.com/en/dev/topics/email/.