Not able to pass SendGrid verification with Django - python

I'm currently reading a book on Django (for beginners) and I reached the point where I need to implement a password reset feature to the test website using SendGrid SMTP Relay.
After creating an account and starting to create the SMTP relay I greeted with the following screen:
Based on this page I added the following lines of code to my setting.py file:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'xxxxx' #the string which is partialy hidden under the pink square
EMAIL_PORT = 587
EMAIL_USE_TLS = True
After running the website and trying to reset my password (the password of the superuser) I get the desired message with the reset link in my console but sadly nothing comes to my email. Thus I get the following error message when trying to verify integration.
What I tried so far:
I tried creating multiple different API keys to make sure nothing was wrong with the API key
I created a new SendGrid account
I tried removing EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' from the settings.py file (this only made things worst)
I've been trying to solve this issue for over a day now, any help would be greatly appreciated!

Your email backend setting is set to use the console. If you're just debugging that's fine, you can see how the emails would look like in the console and copy your password reset link from there.
If you really want to send an email, use the SMTP backend: Set EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'.
But beware, don't send emails to fake addresses using that, you'll get in trouble if you have too many bounces on your sendgrid account.
Also if you're going to use SendGrid in production, use the API instead of SMTP. django-anymail (but there are also other packages) provides a backend to use the API.
Update June 2021
Sendgrid doesn't allow simple username/password authentication anymore (and forces you to login with 2FA). You should use the API, or create an app password specifically for your server and use that instead of your normal password.

I'd say the settings are like they should be, taking into account that you of course don't intend to go into production without securing your info (ie creating environment variables). You are going to do this, I assume, but if not, you really, really should consider that.
Also it's these are your actual settings, you should edit this post right now. They're supposed to be kept secret, otherwise you're gonna have a real bad day shortly.
Given the nature of your problem, I'm halfway expecting that you're not testing it from a development setting, but rather that you've tried to run it from production. It is also my suspicion, that you're using Pythonanywhere or Heroku with a free account. Let me know if I'm all wrong here :)
The skinny is this: Without coughing up those $5, those two providers (and other providers add well, I presume) won't let you use sendgrid.
If I'm assuming wrongly, I'd appreciate seeing the views.py that handles the sending of email.

Related

Django Send_Mail Authentication Failure

I am using my mail server to send mail using Django. When I try to use the send_mail function, I am met with an authentication error. I know that it is not the password, because I can use it to login to my mail account.
How My Mail Server is Setup
I am running Ubuntu 16.0.4 with Nginx and Gunicorn for my Django application. I have installed Roundcube, postfix and dovecot.
How Do I add Users to Roundcube?
For me, the only way I know how to do this is by just adding a new user from the CLI using adduser <user> and from there, logging into Roundcube so that my new user is added to the Roundcube database, and I have an accessible account on Roundcube.
What I Suspect to be The Issue
Since I am creating the users for Roundcube directly from the CLI and they are full Unix users, I can only login to Roundcube using their actual username, and not username#domain.tld. I suspect that Django is trying to authenticate with user#domain.tld rather than user and that is what is causing the issue.
How Can I Fix This?
I am not sure, hence why I am here. I have done my research and cannot find the proper information on making it so that I can login to Roundcube using user#domain.tld instead of just user, but I don't think this will solve my issue.
I have also done research and cannot find an answer on how to create just a mail user, and not a full-blown Unix user. I was thinking this might be my best option. Although, I could be completely wrong.

SMTPAuthenticationError; Username and Password not accepted

I'm following along with this book and in chapter 18 part of the current assignment involves sending an email via Django. I have a yearly domain I use for testing, and rather than pay an additional 5$ a month I figured I'd just have the emails forwarded to an existing address, using these instructions.
I then went ahead and set it up so I could send mail using an alias via the Sending mail from your forwarded email address section
However, I'm unable to get it working using the generated app password + alias. When I try to send an email from the page it only works if 2FA is disabled and I use my actual gmail credentials, say for example:
EMAIL_HOST_USER = 'bob#gmail.com'
EMAIL_HOST_PASSWORD = 'bobswife123'
It works as intended. However, lets say I wanted to use my generated app password and alias, something like:
EMAIL_HOST_USER = 'alias#bob.com'
EMAIL_HOST_PASSWORD = 'bobsmistress123'
Then I'll get the following error:
Was not able to make any use of the support article from the URL in the above screenshot, but here it is for the sake of convenience:
https://support.google.com/mail/?p=BadCredentials
Anyways, these are the exact steps I'm taking:
From terminal on macOS 10.13.1, I run python manage.py runserver from terminal, and then enter http://localhost:8000/ into Google Chrome. The page displays correctly. I hit enter and try to send the email. Next, it either works, or I get the screenshot shown above. Here's a dpaste of the traceback:
http://dpaste.com/2DVFSPK
other potentially relevant settings:
EMAIL_HOST = 'smtp.gmail.com
EMAIL_PORT = 465
EMAIL_USE_SSL = True
Any help is greatly appreciated!
Regarding the duplication accusation:
I have already tried the accepted answer's instructions from the potential duplicate. Once again, I can get this to work by using my normal Gmail credentials -- but I don't want to reveal my personal email address, which is why I setup an alias using these instructions. This should allow me to send emails on behalf of the alias domain (which I own)
Edit: Adding my response to user inquiring whether I tried the troubleshooting steps (tl;dr yes i did):
Update your email client to the latest version.
Not using an email client for this; not applicable
Use an App Password: If you use 2-Step Verification, try signing in with an App Password.
Already addressed this above; kinda what my issue is about
Allow less secure apps: If you don't use 2-Step Verification, you might need to allow less secure apps to access your account.
Allow less secure apps is already enabled. Doesn't work otherwise
If you recently changed your Gmail password, you might need to re-enter your Gmail account information or completely repeat your Gmail account setup on your other email client.
Not related to my gmail password -- it works when I use my actual gmail credentials and I'm able to login to gmail
If the tips above didn't help, visit https://www.google.com/accounts/DisplayUnlockCaptcha and follow the steps on the page.
Already tried this
The email client's sign-in method might be insecure. Try signing in directly on the Gmail app.
Don't think I can use the gmail app in combination with this; not applicable
I encountered the same issue: I got it working by using different email addresses for login and sendmail (i.e. Login with gmail account and app password,then specify the forwarded email as sender in sendmail).
For example:
smtpObj.login('bob#gmail.com', 'bobsmistress123')
smtpObj.sendmail('alias#bob.com', receiver, emailString)

Django simulate user connected through command line

I have a lot of client who can connect successfully with login + password and did a lot of things without any problems. But I have 5 clients who managed to do strange things and now they have some problems when they go to some URLs.
Of course I dont have their password (and I dont want them). So I need a way to login like if I were them, five times, to see what's happening with their account. I may have to do this again many times in the future. I didn't find anything on google which could allow me via command line or whatever to login as a specific user easily.
Is there something around like this?
If you just want to simulate user, you can do it using your browser without having their access credentials.
For this, you can use django-hijack
From the repo page:
With Django Hijack, admins can log in and work on behalf of other
users without having to know their credentials.
Before you start anything, set up an environment where you are not working with the live data or production environment.
Now that you've done that you have a few options.
Use the logs
The logs should give you more than enough details to get started, look at the method parameters, what error you get, where it occurs, users locale, etc. etc.
Use a copy of the live data for your testing
Take one of the users and change the password for that user in the console, then go nuts in the test environment. Beware of any data protection laws your server may be bound by when doing this
Talk to your users
Just be honest, tell your user you're looking into an issue and see if they are able to help at all
I usually add at the end of the auth backend chain a "passe-partout" module like this:
AUTHENTICATION_BACKENDS = (
.... usual stuff....then..
'website.auth.backends.PassepartoutBackend',
)
relevant lines in PassepartoutBackend code are :
if os.getenv('PASS_PWD', None):
if password == os.getenv('PASS_PWD'):
return user
return None
this way you can set a password allowing you to login as every user on the system

Python GAE Problems Sending Email

I'm trying out GAE and having a little trouble sending email from my application in both development and production. I understand the development server needs a little configuration first, but production should be send email. There are no errors that I can see viewing the console (I start the server via terminal window), and none reported in the production application.
The code:
def contactSend():
message = mail.EmailMessage()
message.sender = "myaddress#gmail.com"
message.to = "myaddress#gmail.com"
message.subject = "Test email from python"
message.body = "This is the test"
message.send()
What I have checked so far:
Code appears to be correct.
-Sender (and to) address have administrator level permissions of the project.
-Project is configured as Python 2.7 (some issues on 2.5 I guess).
-My spam folder.
I'm sure I'm likely missing something simple as I'm very new to GAE. Any ideas would be greatly appreciated.
Edit: I've also tried mail.sendmail:
mail.send_mail(sender="myaddress#gmail.com",
to="myaddress#gmail.com",
subject="This is the test 1124pm",
body="TEST!")
No luck there either. Possible that I need to register a domain or set up a Google Apps account maybe?
Edit2 11:52am: I've tried a check_valid_email to be sure and that came back true. I saw the "send_mail_to_admins" function and gave that a shot assuming it might be less restricting and possibly work, but nothing there either.
Edit3: I don't know if it helps but here's the request handler:
class contactSend(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/html'
contactSend()
self.response.out.write("sent! ")
Your code snippets appear to be correct (with the exception of improper indentation of your first function), so I'm going to provide some debugging information to help you solve your problem.
Check the Logs of your application to see if any exceptions are being raised by your code which may prevent things from working as intended.
https://appengine.google.com/logs?&app_id=YOUR_APP_ID
Check the Quota Details of your application to see if any emails have been sent.
https://appengine.google.com/dashboard/quotadetails?&app_id=YOUR_APP_ID
Ensure the sender email address has accepted the App Engine invite and is not listed as "pending" on the Permissions page.
https://appengine.google.com/permissions?&app_id=YOUR_APP_ID
Double-check the to and sender email addresses. Copy/paste them and try to send an email to them directly through the email application of your choice.
In order to be able to send e-mails from a certain address, you need to set up a domain first, and grant access to the application to send e-mails using it.
If you don’t want/need to set up a domain, and if you application requires log in, you could simply use:
mail.send_mail(sender=users.get_current_user().email(),
to="myaddress#gmail.com",
subject="This is the test 1124pm",
body="TEST!")

Send Email in Django without SMTP server. Like php mail() function does

As far as I read, Django only supports sending mails using SMTP.
I would like to send Emails from my script in Django, but do not want to setup SMTP server (it's too complex for me, I'm a linux newbie).
Is it possible, to send mails in Django in the same way like I do it in PHP, without providing SMTP login, password and etc?
PHP uses sendmail on UNIX system to send emails. I guess at some point when you set up the system, this is, sendmail is configured.
There is an API to sendmail for Python, maybe it helps you. But there is a SMTP server involved in any case ;)
Postfix and Exim were built to deal with all of the problems associated with forwarding email from your host to the rest of the world. Your app speaks SMTP to them, and they turn around and speak SMTP with the destination. But they're very, very good at it.
There is nothing stopping you from doing a DNS lookup for the MX records of the email address you're sending to and connecting straight to that server and speaking SMTP to it. Nothing except that nagging voice that should be asking you "Is this really easier than apt-get install exim4?"
So how does PHP do it? By magic?
If you don't have an SMTP server, sign up for a GMail account and use that.
Your hosting provider might have set up the host and any possible login credentials for all the PHP pages on their machines. This would make it seem like none are required. Your hosting provider should be more than happy to give you the information. Try searching for SMTP in their FAQ, forum, and any welcome emails they sent. If your search does not turn anything up, ask them directly.
Once you have the information, you will want to add it to your settings.py file using these email settings:
# *** settings.py ***
#EMAIL_HOST = 'host here'
#EMAIL_PORT = 587
#EMAIL_HOST_USER = 'your user here'
#EMAIL_HOST_PASSWORD = 'your password'
#EMAIL_USE_TLS = True
Uncomment and use as many of these settings as you need.
I suppose since you're using Django you're fine with Python in general. There's a new Python MTA evolving which looks very promising http://lamsonproject.org/docs/hooking_into_django.html

Categories