Django email - python

I am using the Gmail SMTP server to send out emails from users of my website.
These are the default settings in my settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example#example.com'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
If I want a user to send an email, I am overriding these settings and sending the email using Django's email sending methods. When an exception occurs in the system, I receive an email from the example#example.com. Sometimes I receive an email from some logged in user. Which could also possibly mean that when a user receives an email sent from my website it has a sent address different from the actual user.
What should be done to avoid this situation?

Django only uses settings.DEFAULT_FROM_EMAIL when any of the mail sending functions pass None or empty string as the sender address. This can be verified in django/core/mail.py.
When there is an unhandled exception Django calls the mail_admins() function in django/core/mail.py which always uses settings.SERVER_EMAIL and is only sent to addresses listed in settings.ADMINS. This can also be verified in django/core/mail.py.
The only other place Django itself sends e-mails is if settings.SEND_BROKEN_LINK_EMAILS is True, then CommonMiddleware will send mail to all addresses listed in settings.MANAGERS and the e-mail sender is settings.SERVER_EMAIL.
Therefore, the only time a regular user will receive e-mail from your site is when you call send_mail(). So, always pass a real address as the from_mail argument and you will avoid users receiving email from settings.SERVER_EMAIL or settings.DEFAULT_FROM_EMAIL.
Side note: django-registration is at least one example of a Django pluggable that will send mail from settings.DEFAULT_FROM_EMAIL so in cases like this you need to make sure it is a proper e-mail address such as support#yoursite.com or webmaster#yoursite.com.

Related

How did Django send_mail work without EMAIL_HOST_PASSWORD?

I tested Django send_mail() function which I needed for a project. I had the EMAIL_HOST set to the SMTP server provider my company uses, EMAIL_PORT to 587, EMAIL_USE_TLS to True. Both EMAIL_HOST_USER and EMAIL_HOST_PASSWORD was set to "" (empty string).
Then I used the send_mail as:
from djanog.core import mail
mail.send_mail(
"Django mail sample",
"Body",
"myemail#example.com",
["myemail#example.com"]
)
But calling the view actually sent the email to my mail from my mail, but I didn't provide any password at all. I also set recipient_list to my colleague's mail, it worked. Then I tried to change the from_email to my colleague's mail it didn't work.
I have my mail setup in Thunderbird in my system.
So how did Django sent the mail without my email's password? Was it because of my setup in Thunderbird? What happened exactly?
Update:
My colleague tried the same in his system, which doesn't have any mail configured. When running the result was an SMTPRecipientsRefused.
I used both my Phone's network as Hotspot and my company's Hotspot both worked. But in my colleague's system nothing worked.
When setting EMAIL_USER_HOST and EMAIL_HOST_PASSWORD in my colleague's system it worked.

Email Not Sending in AWS SES

Settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_HOST_USER = config('SMTP_USER')
EMAIL_HOST_PASSWORD = config('SMTP_PW')
EMAIL_PORT = 587
VERIFIED_IDENTITY = config('VERIFIED_IDENTITY')
I have a verified email in my AWS SES and I am trying to send a message. I get a sent message everytime I use my contact form but no message actually gets sent.
if 'contactForm' in request.POST:
#print('Contact form')
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
contact_form.save()
email_subject = f'New contact {contact_form.cleaned_data["email"]}: {contact_form.cleaned_data["subject"]}'
email_message = contact_form.cleaned_data['message']
print(email_subject,email_message)
try:
send_mail(email_subject, email_message,settings.VERIFIED_IDENTITY,[settings.VERIFIED_IDENTITY])
except BadHeaderError:
print('bad')
In my opinion the issue might be related to DNS SPF record. See below the link related.
"A sender policy framework (SPF) record is a type of DNS TXT record that lists all the servers authorized to send emails from a particular domain."
https://www.cloudflare.com/learning/dns/dns-records/dns-spf-record/#:~:text=A%20sender%20policy%20framework%20(SPF,Domain%20Name%20System%20(DNS).
https://docs.aws.amazon.com/ses/latest/dg/send-email-authentication-spf.html
To set up SPF, you publish a TXT record to the DNS configuration for your domain. This record contains a list of the servers that you authorize to send email from your domain. When an email provider receives a message from your domain, it checks the DNS records for your domain to make sure that the email was sent from an authorized server.
Regards,
Ed.

Django not sending error messages to email

I used to be able to do this, not sure why it seems to no longer work. I want Django to email me whenever an error occurs like 500 server error, which would be useful since the email usually includes the detailed error description.
Here's one error I'm letting persist for now shown in chrome's terminal as I try and get this feature to work.
here's what I have in my settings file
ADMINS = ['<my_email>']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = SERVER_EMAIL = '<server_email>'
EMAIL_HOST_PASSWORD = "xxxxxx" #
EMAIL_USE_TLS = True
so even though this is an error it doesn't send any emails to my mail.
I can however send emails from the shell using
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'<server_email>',
['<my_email>'],
fail_silently=False,
)
from the same server
ADMINS settings need to be a list of tuples with name and email in each tuple.
Example : ADMINS = [('John', 'john#example.com'), ('Mary', 'mary#example.com')]
Reference : https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-ADMINS

Django Rest Framework Send verification email

Is there any way to send a verification email. when an user registers through drf (Django Rest framework). I have this code in my User model:
from django.core.mail import send_mail
from config import settings
def email_user(self, subject, message, from_email=settings.DEFAULT_FROM_EMAIL, **kwargs):
send_mail(subject, message, from_email, [self.email], fail_silently=False, **kwargs)
and in my settings.py:
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = '`132312123'
EMAIL_HOST_USER = 'mimic#gmail.com'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
But email not being sent, also I want to verification email service, so user will be activated when they click on the link. How to accomplish these?
If you look here:
Console backend
Instead of sending out real emails the console backend just writes the
emails that would be sent to the standard output. By default, the
console backend writes to stdout. You can use a different stream-like
object by providing the stream keyword argument when constructing the
connection.
Also:
This backend is not intended for use in production – it is provided as
a convenience that can be used during development.
Try this one instead:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

How do I configure django settings to send email to Ibm lotus Notes

I have an html page with a form where I collect some data like mail_sender, mesage_to_send, subject etc.
When a user click on a button that information is submitted to a views.py function:
from django.core.mail import send_mail
def register_new_request(request):
email_person = request.POST.get('email_person', False)
send_mail('Test', 'Here is the message.', email_person,['tosomeone#example.com'], fail_silently=False)
return HttpResponseRedirect("/")
How i configure in my settings.py to send email to lotus notes via that function?
After some research i found that are some variables that need to be configured like :
EMAIL_USE_TLS = True
EMAIL_HOST = ''
EMAIL_PORT = ''
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
Can tell me someone how exactly should look that variables? Thanks !
django.core.email sends SMTP mail. There is absolutely nothing special about sending SMTP email to Lotus Notes users. It looks exactly like sending email to any other email user on the Internet.
If your real question is about sending to a Lotus Notes user from within that user's internal network, presumably by submitting your SMTP message directly to a Lotus Domino server, then you need to talk to the administrator(s) responsible for the servers and find out (a) if SMTP is enabled on at least one internal Domino server, (b) what host and port to use to connect via SMTP to the Domino server, (c) is SSL/TLS supported or required for the SMTP connection to the Domino server (d) whether authentication is required, and if yes what user name and password your code should use.

Categories

Resources