Invalid address when sending mail via Django - python

I am trying to send mail via Django, but have an error: (501, b'5.1.7 Invalid address', '"emea\\\\sss1ss1"')
My EMAIL_HOST_USER = r'emea\xss1ss1'. As you can see my user consist \x so I am using r''. How to fix it, the real EMAIL_settings are correct.
my view.py
def home(request):
subject = 'Test mailing'
message = 'Hello world!!!'
email_from = settings.EMAIL_HOST_USER
recipient_list = ['smth#smth.com']
send_mail(subject,message,email_from,recipient_list)
return HttpResponse('Test mailing')

well if you want to send an email using Django, first you have to set some variables in settings.py.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com' # for zoho mail service. use smtp.gmail.com for Gmail.
EMAIL_HOST_USER = 'something#something.com'
EMAIL_HOST_PASSWORD = 'something'
DEFAULT_FROM_EMAIL = f'Your Name {EMAIL_HOST_USER}'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
then import send_mail function and settings:
from django.core.mail import send_mail
from your_project import settings
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user_email, ],)

Related

Not able to send email to sendgrid python

my send_mail code
from rest_framework.views import APIView
from middleware.response import success, bad_request
from user.serializer.dao import SendEmailDao
from core.models import EmailLeadLogs
from util.email import send_mail
class SendEmailView(APIView):
def post(self, request):
attributes = SendEmailDao(data=request.data)
if not attributes.is_valid():
return bad_request(attributes.errors)
email_lead_logs = EmailLeadLogs(**attributes.data["meta"])
email_lead_logs.subject = attributes.data["subject"]
email_lead_logs.save()
send_mail(attributes.data["email"].split(","), attributes.data["subject"], attributes.data["message"])
return success({}, "email send successfully", True)
This is the error i am getting, I am trying to use it from drf
error
It's important to make sure your email settings are wired up correctly in settings.py. Here are the options I use when configuring Sendgrid (you may not need them all):
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = 'myemail#email.com'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_EMAIL_SUBJECT_PREFIX = 'Hello'
ACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'optional'
EMAIL_HOST_PASSWORD is the API key that Sendgrid gives you.

Error in sending email using gmail smtp in Django app

I set up everything as the Django documentation but when I test sending an email.It works but When I check my inbox I find out that I sent and revived from to the same email address.
Here is the settings.py
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "myemail#gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"
And the views.py
...
send_mail( 'subject', 'message', 'user#gmail.com', ['myemail#gmail.com',], fail_silently=False, )
...
But I received an email from myemail#gmail.com to me(myemail#gmail.com).
Im not sure if you did this already but if youre using gmail then you will need to do the following.
Sign in to your Google Admin console (Sign in using an administrator account)
Click Security > Less secure apps.
Select Allow users to manage their access to less secure apps.
Click Save.25
Also you should use environment variables for the email address if you are live already eg:
EMAIL_HOST_USER = os.environ.get('MY_EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('MY_EMAIL_PASS')
If its all working and you are just getting the mail to the wrong address can you send on the document you followed?
In terms of you settings.py that looks fine to me.

SMTPAuthentication error when sending confirmation email in Django

Getting the error message
SMTPAuthenticationError at /accounts/signup/teacher/ (530, b'Must
issue a STARTTLS command first')
when trying to test the signup process in my django app. What should happen is that a user who signs in should receive a confirmation email after signing up.
Here are my settings
EMAIL_BACKED = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_PORT = 587
EMAIL_USER_TLS = True
EMAIL_HOST_USER = '***#***.com'
EMAIL_HOST_PASSWORD = '***'
I am using privateemail from namecheap to send the confirmation emails

google SMTP is giving error on sending mail

I am using python Django to send mail to a user when user login and using django.core.mail(send mail) but it's working on localhost and successfully sending emails to the user but when I host my site on Heroku it's giving some errors.
settings.py
# email config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # During deployment only
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_PORT = 587
EMAIL_HOST_USER = config('EMAIL_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_USER_PASSWORD')
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'Masjid Matrimonial Team <noreply#example.com>'
Views.py
currenttime=datetime.now()
subject = "New Login Alert"
message="You have loged into your account on "+ currenttime.strftime('%A %d %B,%Y %I:%M %p')
recipient_list = [useris.appuser.email]
send_mail( subject, message, email_from, recipient_list )email_from = settings.EMAIL_HOST_USER
SMTP Error code 534 5.7.14 means Authorization error.
Try to activate sending via less secure apps via https://myaccount.google.com/lesssecureapps

Send email with Sendgrid in Django

I'm trying to send emails from a Django app using Sendgrid. I've tried many configurations, but I still doesn't get any email on my Gmail account. You can see my configurations bellow:
settings.py:
SEND_GRID_API_KEY = 'APIGENERATEDONSENDGRID'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'mySENDGRIDusername'
EMAIL_HOST_PASSWORD = 'myEMAILpassword' #sendgrid email
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'MYEMAIL' #sendgrig email
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ContactForm
from django.core.mail import send_mail
from django.conf import settings
from django.template.loader import get_template
def index(request):
return render(request, 'index.html')
def contact(request):
success = False
form = ContactForm(request.POST)
if request.method == 'POST':
name = request.POST.get("name")
email = request.POST.get("email")
message = request.POST.get("message")
subject = 'Contact from MYSITE'
from_email = settings.DEFAULT_FROM_EMAIL
to_email = [settings.DEFAULT_FROM_EMAIL]
message = 'Name: {0}\nEmail:{1}\n{2}'.format(name, email, message)
send_mail(subject, message, from_email, to_email, fail_silently=True)
success = True
else:
form = ContactForm()
context = {
'form': form,
'success': success
}
return render(request, 'contact.html',context)
Do you guys know what could be happening? I can get the email locally and I can see it in the terminal, but no email can be sent at all.
I struggled a bit when trying to set up the sendgrid with Django using SMTP. What worked for me is the following:
settings.py
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'sendgrid-api-key' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'your-email#example.com' # this is the sendgrid email
I used this configuration, and it worked properly, and I strongly suggest using environment variables to fill EMAIL_HOST_PASSWORD and DEFAULT_FROM_EMAIL, so the code will be as follows:
import os # this should be at the top of the file
# ...
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "")
Then, when sending an email I used the following code:
from django.conf import settings
from django.core.mail import send_mail
send_mail('This is the title of the email',
'This is the message you want to send',
settings.DEFAULT_FROM_EMAIL,
[
settings.EMAIL_HOST_USER, # add more emails to this list of you want to
]
)
Try adding these to right bellow the your declaration in settings
SENDGRID_SANDBOX_MODE_IN_DEBUG=False
SENDGRID_ECHO_TO_STDOUT=False
DO this settings on your settings.py file
EMAIL_BACKEND = 'sendgrid_backend.SendgridBackend'
SENDGRID_SANDBOX_MODE_IN_DEBUG = False
FROM_EMAIL = 'youremail#gmail.com' # replace with your address
SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
dont use debug true if you run this on your local server.
This is working for me, I think the SendGrid guide might be missing the DEFAULT_FROM_EMAIL which seems necessary if you haven't set it up on their website.

Categories