Anymail Mailgun Error - django

I am trying to integrate django-anymail with mailgun. When I send a mail with django send_mail as
send_mail('Testing', 'Test dsadsadsa asdsa body', 'xyz#mailinator.com', ['xxxx#gmail.com'])
It gives an error:
AnymailRequestsAPIError: Sending a message to xxxx#gmail.com from xyz#mailinator.com
ESP API response 404:
{
"message": "Domain not found: mailinator.com"
}
How can I get rid of this?
My settings file contains this:
ANYMAIL = {
"MAILGUN_API_KEY": "key-xxxmyprivatekey",
}
EMAIL_BACKEND = "anymail.backends.mailgun.MailgunBackend"
DEFAULT_FROM_EMAIL = "no-reply#mailinator.com"

It sounds like you have not verified your sending domain in Mailgun. Anymail is reporting the API error directly from your ESP, Mailgun, which is complaining they can't find the domain you're trying to send from.
Sign into your Mailgun account and check the domains list in their dashboard. Is mg.mailinator.com listed there with a green "Active" badge? If not, add it if necessary, and then follow Mailgun's instructions to verify the domain.

Related

How to send emails from django shell with AWS ses?

I have verified my domain with AWS SES and as a result my site successfully does send password verification emails (via allauth).
I would, however, also like to be able to send emails based on local scripts. To do this I have been using django shell_plus:
from django.core.mail import send_mail
send_mail("It works!", "This will get sent through anymail",
"me#mysite.com", ["me#mysite.com"])
I get output of '1' suggesting this the email has been sent successfully but I do not receive an email
I think my config is correct given that the site does successfully send emails:
EMAIL_BACKEND = "anymail.backends.amazon_ses.EmailBackend"
ANYMAIL = {
"AMAZON_SES_CLIENT_PARAMS": {
"aws_access_key_id": AWS_ACCESS_KEY_ID,
"aws_secret_access_key": AWS_SECRET_ACCESS_KEY,
"region_name": "us-east-1",
},
}
Can anyone explain what I need to do to send emails from the terminal i.e. without being directly logged into an AWS server?
Was a basic error - I had the required settings in my production config file, but not in my dev config file

Redirect URI mismatch error from Google OAuth

I have a Flask web application which is hosting in Google Cloud Run which is hosted with https://mydomain.run.app.
Now I am trying to add google authentication to it. I have created the API under credentials in GCP. I have given https://mydomain.run.app/authorize in the redirect uri but when I tried to login from my app it throws me redirect mismatch error.
And the error shows me http://mydomain.run.app/authorize.
The mismatch is the https and http
When I tried to give http in the credentials uri it throws me
Invalid Redirect: This app has a publishing status of "In production". URI must use https:// as the scheme.
#app.route('/login/google')
def google_login():
google = oauth.create_client('google')
redirect_uri = url_for('authorize', _external=True,_scheme='https')
return google.authorize_redirect(redirect_uri)
#app.route('/authorize')
def authorize():
google = oauth.create_client('google')
token = google.authorize_access_token()
resp = google.get('userinfo')
user_info = resp.json()
user = oauth.google.userinfo()
session['profile'] = user_info
session.permanent = True
return redirect('/select')
Under Authorized redirect URIs
You should put 1 more URI :
https://mydomain.run.app/
Then check again. I have got same issue before.
your app is currently set to production in google developer console.
This means that all of the redirect uris you try to add to your project. Must be HTTPS and not HTTP you can also not use localhost
As you are trying to use http://mydomain.run.app/authorize you need to change it so that it is https://mydomain.run.app/authorize note that the first one was http:// and not https://
The error is coming because your application itself is trying to send a redirect uri of http and not https. You need to fix your application so that it is using https.

Does AWS SES work with grails Mail plugin 1.0.7?

I would like to use the AWS SES mail service using SMTP. I know it works for some versions, but I haven't gotten it to work for my application yet. I get a 530 Authentication error.
My question is in three parts:
1. Does AWS smtp work using Grails 2.3.1 with the Mail 1.0.7 plugin using java 1.7?
If it does work, how does one configure it?
If it does work, how can I get more debug information to figure what is wrong?
I've tried every configuration example variant I've come across for this. My latest is below. (It's clear I don't really understand the Mail plugin and the Java mail class, I'm just trying to suss out a working configuration incantation.)
Here is what I have in my config.groovy file:
grails {
mail {
host = 'email-smtp.us-east-1.amazonaws.com'
port = '587'
username = SMTP AWS username, not IAMS username
password= SMTP AWS password, not IAMS password
props = [ 'mail.debug': true,
'mail.smtp.host' : 'email-smtp.us-east-1.amazonaws.com',
'mail.smtp.auth': 'true',
'mail.smtp.starttls.enable': 'true',
'mail.smtp.port': '587',
'mail.smtp.starttls.required': 'true',
'mail.smtp.user' : smtp username from above,
'mail.smtp.usernme' : smtp username from above,
'mail.smtp.password': smtp password from above,
'mail.smtp.socketFactory.port': '587',
'mail.smtp.socketFactory.class':
'javax.net.ssl.SSLSocketFactory',
'mail.smtp.EnableSSL.enable':'true',
'mail.smtp.socketFactory.fallback': 'false' ]
}
}
I have successfully made direct AWS SES emails using the aws-sdk:1.9.40 plugin, by the way.
I am using the mailService.sendmail code from the Mail plugin. This is unchanged (beyond the hardcoded html text), and works fine when the configuration went to our email server.
def resp = mailService.sendMail {
from replyToEmail
replyTo replyToEmail
to user.email
subject emailSubject
html "This is the mesage text Mar 20"
}
Any help at all would be most appreciated!!

Mailgun domain not found: example.com

I am trying to setup emails with my own website. Let's say the domain name is example.com.
The name server in use is digital ocean and I also have a gmail account linked to the same (say using contact#example.com).
While setting up things with mailgun, I used mg.example.com (as they said it would also let me email using the root domain). The verification step is done and I can send email using contact#mg.example.com.
However, trying to use the root domain (contact#example.com) gives the following error:
AnymailRequestsAPIError: Sending a message to me#gmail.com from contact#example.com
ESP API response 404:
{
"message": "Domain not found: example.com"
}
How do I resolve this issue?
I got the same error when I copy-pasted the curl example from Mailgun help page.
My domain was set to EU region, and I had to set the api domain to api.eu.mailgun.net instead of api.mailgun.net.
Boom! Working! :)
I am using the EU region with Mailgun and have run into this problem myself. My implementation is a Node.js application with the mailgun-js NPM package.
EU Region Implementation:
const mailgun = require("mailgun-js");
const API_KEY = "MY_API_KEY"; // Add your API key here
const DOMAIN = "my-domain.example"; // Add your domain here
const mg = mailgun({
apiKey: API_KEY,
domain: DOMAIN,
host: "api.eu.mailgun.net" // -> Add this line for EU region domains
});
const data = {
from: "Support <support#my-domain.example>",
to: "recipient#example.com",
subject: "Hello",
text: "Testing some Mailgun awesomness!"
};
mg.messages().send(data, function(error, body) {
if (error) {
console.log(error);
} else {
console.log(body);
}
});
Further options for the mailgun() constructor can be found here.
Thought I'd share a full answer for anybody that's still confused. Additionally, Mailgun Support was kind enough to supply the following table as a reference guide:
IF:
your domain is an EU domain AND
you're using django-anymail as in Rob's answer above
THEN the ANYMAIL setting (in your Django project settings) should specify the API_URL to be the EU one, example:
ANYMAIL = {
'MAILGUN_API_KEY': '<MAILGUN_API_KEY>',
'MAILGUN_SENDER_DOMAIN': 'example.eu',
'MAILGUN_API_URL': 'https://api.eu.mailgun.net/v3' # this line saved me!
}
Before adding the MAILGUN_API_URL I was getting this error:
AnymailRequestsAPIError: Sending a message to xxx#example.com from noreply#example.eu <noreply#example.eu>
Mailgun API response 404 (NOT FOUND):
{
"message": "Domain not found: mailgun.example.eu"
}
Update 8/22/16:
Anymail has been updated to take a new MAILGUN_SENDER_DOMAIN in settings.py. See version .5+ docs.
--
Original Answer
You did not post your code for how you're sending your email, but you are probably trying to send using the simple send_mail() function:
from django.core.mail import send_mail
send_mail("Subject", "text body", "contact#abc.example",
["to#example.com"],)
When you use this method, Anymail pulls the domain out of your From address and tries to use this with Mailgun. Since your From address (abc.example) doesn't include the subdomain mg., Mailgun is confused.
Instead, you need to send the email using the EmailMultiAlternatives object and specify the Email Sender Domain like so:
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives("Subject", "text body",
"contact#abc.example", ["to#example.com"])
msg.esp_extra = {"sender_domain": "mg.abc.example"}
msg.send()
Don't forget the brackets in your To field, as this needs to be a tuple or list even if you're only sending it to one recipient.
For more information, see Anymail docs on esp_extra.
Struggled for days with correct DNS settings and finally found as #wiktor said, i needed to add "eu" to api endpoint to make it work. Its actually also documented here: https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions
Sorry for replying as an answer, dont have enough rep to add comment :(
I had the same problem: 404 error, domain not found.
The cause
The EU region selection for the domain on Mailgun
The solution
Change the region from EU back to the default of US.
Since I had not used the domain at all up to this point, I simply deleted it, re-added it, then changed my TXT, MX and CNAME records (for example, mailgun.org instead of eu.mailgun.org) at the domain registrar (which was GoDaddy in my case).
I found my fix with this change:
ANYMAIL = {
...
'MAILGUN_SENDER_DOMAIN': 'example.com', # Using the sending domain in Mailgun
}

Sending email not working on heroku

I have this function in forms.py. There is currently no email specifications in my settings.py.
def send_email(FROM_NAME,FROM,TO,SUB,MSG,EXISTING_EMAIL,EXISTING_PASSWORD):
FROMADDR = "%s <%s>" % (FROM_NAME, FROM)
LOGIN = EXISTING_EMAIL
PASSWORD = EXISTING_PASSWORD
TOADDRS = [TO]
SUBJECT = SUB
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += MSG+"\r\n"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
I call it my views.py like so
send_email('my_name','from_me#gmail.com','to_som1#gmail.com','my subject','mymessage','my_existing_email#gmail.com','password_to_existing_email')
This works locally. I have tested it with yahoomail and gmail. But when I upload to heroku it gives the error "(535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 support.google.com/mail/bin/answer.py?answer=78754 et6sm2577249qab.8')"
Can anyone help?
You want to use this:
FROMADDR = "%s <%s>" % (your_name, your_email)
You shouldn't be building emails with string interpolation, that's a good way to get your site used to send spam via header injections. See my answer here for details on how to construct emails securely.
Generally speaking, when formatting from addresses, you should use the format Display Name <email#example.com>. See RFC 5322 for details.
Have you read the page linked to in the error message?
If you're repeatedly prompted for your username and password, or if
you're getting an 'invalid credentials' or 'web login required' error,
make sure your password is correct. Keep in mind that password are
case-sensitive.
If you’re sure your password is correct, sign in to your account from
the web version of Gmail instead at http://mail.google.com
In most cases signing in from the web should resolve the issue
Here is what worked for me. After getting the error Please log in with your web browser and then try again. Learn more etc. when trying to send email from my web application, I logged in to the email via browser from my local computer.
After I logged in, there was a yellow notification bar on top which asking me if I want to allow external application access my mail. I confirmed this and Google asked me to log in to the account from the application within the next 10 mins. This will white-list the application.