How to send email as alias using Django - python

Background:
I have a site I'm building using Django and I've encountered the following conundrum. I have a backend email that we will call luckyducky#gmail.com. Now I also have a domain email that forwards directly to luckyduck#gmail.com which is called support#luckyducky.com.
Currently I have my Django Email setting set up as follows:
EMAIL_HOST_USER="luckyducky#gmail.com"
EMAIL_HOST_PASSWORD="password"
This works fine, but I want to have the email sent out from 'support#luckyducky.com'.
Note:
Including the Python community as a whole as maybe the issue can be resolved outside of Django mail wrapper functions.
Question:
How can I set up this redirection? Meaning, how can I send out emails under the alias 'support#luckyducky.com' when my true email configured in the Django backend is luckyducky#gmail.com ?

settings.EMAIL_HOST_USER is only used to authenticate with the SMTP server. The third argument of django.core.mail.send_mail() is the From: address.

Taken from django documentation
In two lines:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'support#luckyducky.com',
['to#example.com'], fail_silently=False)
Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS and EMAIL_USE_SSL settings control whether a secure connection is used.

Related

Using Outlook SMTP server to send email through a contact form but unable to send as I need the server to be able to accept multiple "From" addresses

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

Django and AWS Simple Email Service [duplicate]

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.

Django mail_admins vs send_mail

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.

Django Email settings without using From Address

Here is my settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "mygmail#gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"
EMAIL_PORT = 587
and view.py code
From='someemail#mail.com'
send_mail(Subject,Message, From, [To])
every time when a mail is sent it shows mygmail#gmail.com ,but i need it show someemail#mail.com ,how to fix this??
Did you add your "someemail#mail.com" to your gmail account?
Google have a procedure to allow their SMTP to send email as an other, they require you can find a code they send to this given email.
Otherwise it's normal.
(Security way to avoid malicious users tu use google's SMTP to send email as "barack.obama#whitehouse.gov")
Google only allows you to send email from addresses you have registered on your Gmail account. So someemail#mail.com will not work unless that's a registered gmail account. Though I suspect you are trying to use it as mask from some user in your app to send mail as themselves.
An alternate way to do it is to use something other than Gmail (such as Mailgun, Sendgrid, etc). For best practices, send the email from a legitimate registered email address, but utilize the reply-to field for custom emails.

send mail with Django using EmailMessage class

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.

Categories