I can get my django application to properly report server errors by email using the obvious settings :
DEBUG = False
ADMINS = (('name','email'),)
MANAGERS = (('name','email'),)
EMAIL_HOST = 'ip address'
EMAIL_HOST_USER = 'user'
EMAIL_PORT = 25
EMAIL_HOST_PASSWORD = 'pwd'
SERVER_EMAIL = 'server-email'
Yet for some reason, when I try to use send_email() it returns either Authentication errors if I try without TLS, or "You don't have permission to send as this sender" if I do use TLS. I can't understand why error reporting works, and send_email doesn't... it uses the same settings by default.
Any suggestions ?
There are two settings which affect the 'from' email address for different types of emails.
SERVER_EMAIL - used for sending error emails to ADMINS and MANAGERS.
DEFAULT_FROM_EMAIL - used for sending regular emails
It looks as if you have already set SERVER_EMAIL, so make sure you have set DEFAULT_FROM_EMAIL as well.
Related
So I have a Django app that sends confirmation emails to users who want to register their account. It looks something like this
views.py
def send_activation_email(user, request):
current_site = get_current_site(request)
email_subject = "Activation Email"
context = {"user": user,
"domain": current_site,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': generate_token.make_token(user)
}
email_body = render_to_string('email/activate.html',context)
email = EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_FROM_USER, to=[user.email])
email.send()
and with settings.py looking like this
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_FROM_USER = 'verification#ianisdo.xyz'
EMAIL_HOST_PASSWORD = '[redacted]'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
The problem is that, when I try to send a email when the user creates an account, I get this error
SMTPRecipientsRefused at /register/
{'ianis.donica#gmail.com': (554, b'5.7.1 <ianis.donica#gmail.com>: Relay access denied')}
What I know for sure is:
that it's not a problem with the private namecheap email, from which
I can still send emails just not from my website.
I also know the problem is not due to gmail not liking my email, as
the same error when the email is sent to a yahoo.com domain.
I also know that the issue is not with the settings.py not connecting
to the views.py
I also know that all the details are entered correctly
From my knowledge and talking to namecheap support, the issue is most likely caused by the header of the email but to my knowledge I can't seem to find anything wrong with it.
If someone is able to help me out, I would really appreciate it. I have had issues sending emails for about 9 days now
I think you need to add a value for EMAIL_HOST_USER in your settings.py. Without it, Django won't attempt to authenticate. Here's the relevant bit from the docs:
EMAIL_HOST_USER
Default: '' (Empty string)
Username to use for the SMTP server defined in EMAIL_HOST. If empty, Django won’t attempt authentication.
The username should match the password setting you're already providing (i.e., a valid namecheap private email account).
Currently using Django as the chosen framework for my project and I have implemented a contact form and my main goal is for users to complete the contact form and the admin of the site (me) get an email, which shows me the details of their enquiry.
I am trying to use the Outlook SMTP server and these are my current settings in settings.py:
EMAIL_HOST = 'smtp.office365.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '<my_emailAddress>'
EMAIL_HOST_PASSWORD = os.environ.get('OUTLOOK_PASSWORD')
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = '<my_emailAddress>'
However, whenever I complete the form and send the request to the server I am receiving the following error code:
(554, b'5.2.252 SendAsDenied; <my_emailAddress> not allowed to send as <inputtedForm_emailAddress>; STOREDRV.Submission.Exception:
SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.
I am looking for some help surrounding this issue - it would be greatly appreciated.
Recently I had same issue with Outlook.
Looks like Outlook is not accepting anymore sending emails from unsecure apps.
Your next alternative is to use gmail but first enable unsecure apps in gmail setting.
Everything should be the same, change only smtp to
EMAIL_HOST=smtp.gmail.com
This question already has answers here:
Amazon SES SMTP with Django
(8 answers)
Closed 10 months ago.
I'm attempting to get a django site up and running and I'm trying to enable django's standard password reset service.
My site is hosted by AWS EC2, so I figured I would use AWS SES for my email service. However, I can't get the smtp connection to work. Any idea on how to solve for the following error:
Exception Type: SMTPSenderRefused
Exception Value: (530, b'Authentication required', 'example#example.com')
I've looked at the following, but I'd prefer to stay with django's built-in email back-ends if possible:
https://github.com/django-ses/django-ses
https://github.com/azavea/django-amazon-ses
Amazon SES SMTP with Django
Email Settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_PORT = 587 # (I've also tried 25, 465, & 2587)
EMAIL_HOST_USER = ''
EMAIL_PASSWORD = ''
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'example#example.com' # but my real email address
My EMAIL_HOST_USER and EMAIL_PASSWORD are set to my SMTP credentials I obtained by following the instructions at: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html
Also, I was sure to verify my DEFAULT_FROM_EMAIL and my domain following these instructions: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-addresses-and-domains.html
And DKIM is enabled and my nameservers have been updated.
Any help would really be appreciated.
UPDATE:
I was able to get the django email send working with the django-ses 3rd party library, but I'm still not able to figure out why the standard backend doesn't work via SMTP.
I think there is definitely something wrong with my code as I went so far as to switch email clients from AWS SES to Zoho mail and I'm still receiving the same 530 Authentication required error.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'support#example.com'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'support#example.com'
EMAIL_PASSWORD = ''
If it is asking for Authentication, please double-check the following:
Your SMTP settings
The email that you're trying to send mails from belongs to the verified domain. e.g. if you have verified example.com but you try sending emails from something#gmail.com, it is going to ask for gmail auth credentials.
I am trying to send an email using the EmailMessage class provided by Django. My code in views.py is as follows:
email = EmailMessage('Hello', 'My_message ', 'my#mail.com', [email])
email.attach_file('file.jpg')
email.send()
I get an [Errno 111] Connection refused. What could be the source of this error? Are there any additional settings that I should be aware of when trying to send an email with an attachment in Django? Thanks a lot
[edit] I forgot to mention i am working on the development server provided by django
most likely, Django can't connect to a mail server on localhost. If you look here, look for EMAIL_HOST, EMAIL_HOST_USER and EMAIL_HOST_PASSWORD
The easiest way to set the email for testing purposes in Django would be to set it to a gmail account with SMTP.
In settings.py just add this.
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'user#gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
For production you will need some sort of mail server installed on your server, or connect to a remote mail server.
Hope this helps in some way.
Like Chris Curvey mentioned, you'll have to configure your mail settings as is described here: http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#available-settings
Which mail host to use depends on your connection. If you are at work, then there should be a system administrator who can tell you which information to use. If you're at your own home, then your ISP probably has a mail server that you're supposed to use.
If you have a functioning email client on your computer, then you should also be able to see the proper settings in "Outgoing Mail" or something to that effect.
ADMINS = [("alex", "alex#mydomain.com"), ("matt", "matt#mydomain.com")]
DEBUG = False
How come Django doesn't email me errors? I set up the email settings correctly, and everything else can send email just fine.
EMAIL_HOST = "mail.blah.com"
EMAIL_PORT = 25
EMAIL_HOST_USER = "blah"
EMAIL_HOST_PASSWORD = "blah"
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "blah#blah.com"
I want to be emailed all errors that occurred. (even though I have my own try /except blocks everywhere).
From django documentation on error-reporting:
By default, Django will send e-mail
from root#localhost. However, some
mail providers reject all e-mail from
this address. To use a different
sender address, modify the
SERVER_EMAIL setting.
Maybe this will help you.
One thing I've found that helps me debug the email subsystem in django is a setup that displays the email content (headers and all) to standard out. I've summarized below, but you can also find more info about in the django documentation (link below). Not sure if it will help you in your case, but it might help debug the issue ... help you see what's being sent.
Set the following in settings.py
EMAIL_HOST = "localhost"
EMAIL_PORT = 1025
Run the following to host the test webserver
python -m smtpd -n -c DebuggingServer localhost:1025
Django documentation:
http://docs.djangoproject.com/en/dev/topics/email/