django related to host mail - django-views

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`

Related

Django Create post and send mail

after saving my variables in views.py that I have forwarded, can I send them as mail while saving the same fields? My mail sending codes are below but I didn't know how to do it.
def gcreate(request):
if request.method == 'POST':
gmember = gunluk(
adsoyad=request.POST['adsoyad'],
adsoyad2=request.POST['adsoyad2'],
vardiya=request.POST['vardiya'],
aciklama=request.POST['aciklama'],
incident=request.POST['incident'],
alinanaksiyon=request.POST['alinanaksiyon'],
ulasilmayanekip=request.POST['ulasilmayanekip'],
ulasilmayanbilgisi=request.POST['ulasilmayanbilgisi'],)
try:
gmember.full_clean()
except ValidationError as e:
pass
send_mail(
'test',
'testmessage',
'xx#xx.com',
['xx#xx.com'],
fail_silently=False
)
gmember.save()
messages.success(request, 'Ekleme İşlemi Başarılı!')
return redirect('/gunlukistakibi')
else:
return render(request, 'gcreate.html')
Did you check the Gmail less secure apps settings? Turn on 'less secure app access'.
from django.core.mail import send_mail
from django.conf import settings
def gcreate(request):
if request.method == 'POST':
gmember = gunluk(
adsoyad=request.POST['adsoyad'],
adsoyad2=request.POST['adsoyad2'],
vardiya=request.POST['vardiya'],
aciklama=request.POST['aciklama'],
incident=request.POST['incident'],
alinanaksiyon=request.POST['alinanaksiyon'],
ulasilmayanekip=request.POST['ulasilmayanekip'],
ulasilmayanbilgisi=request.POST['ulasilmayanbilgisi'],)
try:
gmember.full_clean()
except ValidationError as e:
pass
send_mail(
'Subject',
'Message.',
settings.EMAIL_HOST_USER,
['to#example.com'],
)
gmember.save()
messages.success(request, 'Ekleme İşlemi Başarılı!')
return redirect('/gunlukistakibi')
else:
return render(request, 'gcreate.html')
In settings.py put the following
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com' # else your smtp provider and Less Secure App should be allowed
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your email address'
EMAIL_HOST_PASSWORD = 'your email address password'

contact form send email success but no message in my email box - django

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.

Email not being sent using Django send_mail()

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 am not getting emails from my contact form

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.

Django - sending emails (SMTPAuthenticationError)

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?