How do I set the from_email var - django

I'm trying to use Amazon's SES and the Django-ses app to send emails. It works locally but fails on the server, returning the SESAddressNotVerifiedError.
Inspecting the trace revealed that it's failing because the from_email var is set to webmaster#localhost. I've looked all over to find where I set this variable to my verified email address in AWS-SES.
Does anyone know how I am supposed to change the from_email var from webmaster#localhost to myemail#myemail.com?
Thanks,
Anthony

So it turns out that what I needed to change was this setting:
DEFAULT_FROM_EMAIL = 'My Domain < myEmail#myEmail.com >'
For everyone who is using Django-Registrations this is the default, but the reference to it is in registration/models.py, line 264 (the last line) if you want to change it, which I might unless it breaks, in which case, I'll report back.
Django default_from_email name
Thanks to Ashok for this solution

Related

Django-templated-email does not send email

I am using the following package https://github.com/vintasoftware/django-templated-email in a Django app to try and send an email. I am running it inside a django management command.
When I run the command the email part outputs to the console but no email is actually sent out. The SMTP settings and such are correct as emails work fine for other areas of the application.
from templated_email import send_templated_mail
send_templated_mail(
template_name='welcome',
from_email='from#example.com',
recipient_list=['to#example.com'],
context={
'username':"test",
'full_name':"test",
'signup_date':"test"
},
)
Any help is appreciated. I suspect I have misunderstood the syntax of the package.
Signup_date must be date and username and full_name must use single quote

Sending SMTP email with Django and Sendgrid on Heroku

I'm trying to send email using SMTP and sendgrid for a Django app. I'm able to send emails on my local server, but on my heroku app I get an "SMTPServerDisconnected" error saying "connection unexpectedly closed. Is there a way to send SMTP email with sendgrid once deployed to Heroku? I can't seem to find any documentation on this.
Here are my settings for email in settings.py:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'EMAIL_HOST_USER'
EMAIL_HOST_PASSWORD = 'EMAIL_HOST_PASSWORD'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'email#email.com'
SENDGRID_API_KEY='SENDGRID_API_KEY'
SENDGRID_PASSWORD='SENDGRID_PASSWORD'
SENDGRID_USERNAME='SENDGRID_USERNAME'
Please let me know what settings you use to send SMTP email. Thanks.
You need to go into your Sendgrid dyno. Navigate to Settings>API Keys.
Click on create API Key, at the time of writing this it is a blue button in the top right corner of the page.
copy the key that they generate for you and paste it somewhere on your local machine, also navigate back to your heroku page. Navigate to your app's Settings and click on "reveal config variables". You should now see key value pairs of all the environment variables. In the Key column add "SENDGRID_API_KEY" and in the value column add the key that you copied from the Sendgrid website. At this point the following python code should work:
sg = sendgrid.SendGridAPIClient(os.environ['SENDGRID_API_KEY'])
message = Mail(from_email='example#example.com', to_emails='example#example.com',
subject='Example Subject ', html_content='<strong>and easy to do anywhere, even with Python</strong>')
response = sg.send(message)
If you keep the response variable you can wrap the code in a try except block can try to catch errors. Sorry for the formatting I am still new to posting on stack overflow.

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.

How can I get Django on Google App Engine to automatically email server 500 errors to Admins?

When my App raises a server 500 error, I'm not receiving the automatic Django email that it should be sending: https://docs.djangoproject.com/en/1.3/howto/error-reporting/
I'm using the Google App Engine Django Helper at http://code.google.com/p/google-app-engine-django/
In my settings.py file:
DEBUG = False
ADMINS = (('Support', 'Support#******.com'),)
EMAIL_HOST = ''
SERVER_EMAIL = 'Support#******.com'
In the Google App Engine Dashboard, I've added Support#**.com (The same email in my settings.py) to the admins with the role of Viewer. I've tried changing the role to Developer.
I think the problem is this line:
EMAIL_HOST = ''
Since the Django docs say
In order to send e-mail, Django
requires a few settings telling it how
to connect to your mail server. At the
very least, you’ll need to specify
EMAIL_HOST. . .
But, the there are comments in the settings.py file that came with the google-app-engine-django project that say
# Ensure that email is not sent via SMTP by default to match the standard App
# Engine SDK behaviour. If you want to send email via SMTP then add the name of
# your mailserver here.
EMAIL_HOST = ''
Make sure you specify the 'SERVER_EMAIL' (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SERVER_EMAIL) in your settings. Otherwise the emails will be sent from "root#localhost" and AppEngine won't send them.