Hey guys I am trying to make my contact form in django 2.1 which will send emails to me from the sender but I don,t know why it,s not sending me the email I was supposed to receive
views.py
def index(request):
queryset = Post.objects.filter(featured = True)
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
subject = request.POST.get('subject')
message = request.POST.get('message')
subject = 'Message from ubaidparvaiz.com recieved'
from_email = settings.DEFAULT_FROM_EMAIL
to_email = [settings.DEFAULT_FROM_EMAIL]
context = {'user':name,
'email':email,
'message':message,
'subject':subject
}
contact_message = get_template('app/contact_message.txt').render(context)
send_mail(subject,message,from_email,to_email,fail_silently = True)
return redirect('index')
return render(request,'app/index.html',{'object':queryset})
settings.py
SEND_GRID_API_KEY = 'API key'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'ubaidparvaiz'
EMAIL_HOST_PASSWORD = '.py/r^admin1'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'ubaidparvez4#gmail.com'
ACCOUNT_EMAIL_SUBJECT_PREFIX = 'Contact email received from my blog'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
contact_message.txt
You recieved a message,Don,t panic ..Keep it normal
The sender,s name : {{user}}
They sender,s email : {{email}}
The sender,s subject : {{subject}}
This is what he states : {{message}}
Thank You
I recommend you check your spam folder. Gmail usually filters emails coming from untrusted sources.
Related
Problem is related to host mail address as it cannot be changed and because of fixed host name, the sender’s mail address is not being updated after updating it several times
The problem with SendinBlue is that it does not allow us to use attachments with mail and ignores the attachments every time while sending an email
While using react for frontend, it saved the data from mails and does not send the mail from the backend through API.
def send_mail(request):
# form = emailssForm()
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
message = request.POST.get('message')
subject = request.POST.get('subject')
from_email = request.POST.get('from_email')
# file = request.POST.get('file')
template = loader.get_template('contact_form.txt')
context = {
'name': name,
'email': email,
'message': message,
'subject': subject,
'from_email':from_email,
# 'file' : file,
}
message = template.render(context)
email = EmailMultiAlternatives(
"Uthara Print", email,message,
"Compny " + "Uthara Print",
['pandayharsh472#gmail.com',email]
)
# msg = EmailMultiAlternatives(
# subject,message,name,[from_email],
# )
email.content_subtype = 'html'
file = request.FILES['file']
email.attach(file.name, file.read(), file.content_type)
email.send()
messages.success(request, 'Message sent successfully ! I will ansewr you as soon as possibile....')
return HttpResponseRedirect('/')
SETTING.PY.............................................................
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'GMAIL_HOST_USER'
EMAIL_HOST_PASSWORD = 'PASSWORD'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False`
I have created the below django contact form for the contact page of my website. The problem with the form is that when a user fills and clicks the submit button, it show a success message but the email is not sent. i mean i don't receive message in my box
Can someone please check and tell me where I went wrong.
forms.py :
from django import forms
from django.forms import ModelForm
# contact form
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True )
contact_email = forms.EmailField(required=True )
title = forms.CharField(required=True)
content = forms.CharField(required=True )
views.py :
def contact(request):
Contact_Form = ContactForm
if request.method == 'POST':
form = Contact_Form(data=request.POST)
if form.is_valid():
contact_name = request.POST.get('contact_name')
contact_email = request.POST.get('contact_email')
contact_content = request.POST.get('content')
title = request.POST.get('title')
template = loader.get_template('website_primary_html_pages/contact_form.txt')
context = {
'contact_name' : contact_name,
'contact_email' : contact_email,
'title' : title,
'contact_content' : contact_content,
}
content = template.render(context)
email = EmailMessage(
"Hey , There are new Message from blog",
content,
"Creative web" + '',
['contact#techpediaa.com'],
headers = { 'Reply To': contact_email }
)
email.send()
return redirect('Success')
return render(request, 'website_primary_html_pages/contact_us.html', {'form':Contact_Form })
#end contact form
settings.py :
#MY EMAIL SETTING
DEFAULT_FROM_EMAIL = 'contact#techpediaa.com'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
when user fills all fields it show message send success but i don't received any message why
i am using namecheap as a hosting
my new settings in settings.py :
#MY EMAIL SETTING
EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'contact.techpediaa.com'
EMAIL_USE_TLS=False
EMAIL_PORT = 587
EMAIL_HOST_USER = 'contact#techpediaa.com'
EMAIL_HOST_PASSWORD = 'mypass'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Yes you can use this for Cpanel.
The processes will be the same for Cpanel as google. You will need to create a app password in the Cpanel
EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'your_mail.your_domain_of_Cpanel'
EMAIL_USE_TLS=True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email'
EMAIL_HOST_PASSWORD = 'app_password'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Try it now
If EMAIL_USE_TLS is getting any kind error then edit it False
You don't get an email in the inbox, because you're using django.core.mail.backends.console.EmailBackend - as you can see it will just output email in the console. Do you see it there?
SMTP Backend is what you need.
settings.py:
EMAIL_HOST = 'smtp.mail.yahoo.com'
SMTP_PORT = 456
EMAIL_HOST_USER = 'my_email#yahoo.com'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_USE_SSL = True
the method:
def contact(*args,**kwargs):
contactform = ContactForm(request.POST, prefix='contactform')
send_mail(
'Contact from website by '+contactform.name,
contactform.message,
settings.EMAIL_HOST_USER,
['saxoya8501#emailhost99.com'],
fail_silently=False,
)
It does not send any email. In the logs I get:
"GET /?contactform-name=myname&contactform-email=aaaa%40gnm.com&contactform-message=aaaaaaaaaaaaaa HTTP/1.1" 200 20808
The form that I am using is this:
class ContactForm(forms.Form):
prefix = 'contactform'
name = forms.CharField(max_length=50)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea(attrs={'class':'materialize-textarea'}))
Any suggestions on how to get it to work?
This might be an issue because of your Gmail security. Give permission in your gmail account settings to allow unsecure apps
I would like to send an email to all teachers who teach subjects related to a new question (whether it is created by the admin panel or the question creation form) so that they can approve the question. How to do this?
models.py
class Usuario(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,verbose_name="Usuário")
USUARIO_CHOICES = (
(UM,'Aluno'),
(DOIS,'Professor'),
)
tipoUsuario = models.IntegerField('Tipo de Usuário',choices=USUARIO_CHOICES,default=1)
class Questao(models.Model):
aOpcao = models.CharField(max_length=500,null=False,blank=True,verbose_name="Letra a")
bOpcao = models.CharField(max_length=500,null=False,blank=True,verbose_name="Letra b")
cOpcao = models.CharField(max_length=500,null=False,blank=True,verbose_name="Letra c")
dOpcao = models.CharField(max_length=500,null=False,blank=True,verbose_name="Letra d")
eOpcao = models.CharField(max_length=500,null=False,blank=True,verbose_name="Letra e")
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail#outlook.com'
EMAIL_HOST_PASSWORD = 'Mypassword'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'MySite <myemail#outlook.com>'
views.py
def add_questoes(request):
if request.method == 'POST': # If the form has been submitted...
form = QuestaoForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save()
subject = 'Questão adicionada'
message = 'Há uma nova questão adicionada'
recepient = str(sub['Email'].value())
send_mail(subject,
message, EMAIL_HOST_USER, [recepient], fail_silently = False)
return redirect('/') # Redirect after POST
else:
form = QuestaoForm() # An unbound form
return render(request, 'polls/add_question.html', {'form': form})
forms.py
class QuestaoForm(forms.ModelForm):
aOpcao = forms.CharField(max_length=500,label="Letra a")
bOpcao = forms.CharField(max_length=500,label="Letra b")
cOpcao= forms.CharField(max_length=500,label="Letra c")
dOpcao = forms.CharField(max_length=500,label="Letra d")
eOpcao = forms.CharField(max_length=500,label="Letra e")
class Meta:
model = Questao
fields = '__all__'
Take a look at this documentation:
https://docs.djangoproject.com/en/2.2/ref/signals/
Django post save signals are a great way to execute code after specific DB operation. In your case I would think you would need to create a post_save signal which triggers the sending of the email.
I use:
from django.core.mail import send_mail
in my app. Yesterday my app normally sent emails without any errors. Today I get "SMTPAuthenticationError". Below is a part of my settings.py:
EMAIL_HOST = 'poczta.interia.pl'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'username'
EMAIL_ADRESS = 'email_adress'
EMAIL_HOST_PASSWORD = 'password'
This is my email function:
def send_activation_email(self):
subject = 'SUBJECT'
message = 'MESSAGE'
from_email = settings.EMAIL_ADRESS
to_email = self.email
send_mail(
subject,
message,
from_email,
[to_email],
fail_silently=False,
)
I don't have any idea what is going on. Yesterday it was fine, today not. Any ideas?