Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the from address in django email (sent through Gmail)

Tags:

django

gmail

In my settings.py, I have this values:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_USE_TLS = True

Then in my views, I am getting an email address from my models like for example:

#models.py
class Profile(models.Model):
    name = models.CharField(...)
    email_address = models.EmailField()

Let's assume that the email_address in the Profile model is [email protected]

#views
def send_email(request,profile_id):
   profile = Profile.objects.get(pk=profile_id)
   email = profile.email_address

so when i will send a mail,

send_mail('subject', 'content', email, ['[email protected]'])

When the email was already sent, the sender_email is still [email protected]. Can someone teach me how to overwrite this email address? Thanks.

like image 852
david Avatar asked Dec 07 '11 16:12

david


2 Answers

The DEFAULT_FROM_EMAIL setting is just that, the default. Django uses this in places where email is sent automatically (such as error reports to ADMINS). When you call the send_mail method directly, you must provide the from_email parameter. Even if you wanted the DEFAULT_FROM_EMAIL, you would have to import it from django.conf.settings and then pass it in.

My best guess is that Gmail is actually the culprit here. To the best of my knowledge, Gmail does not let you specify a custom sender because too many people already try to use Gmail to send out spam, and they want to discourage that practice.

Basically, when your email goes through the Gmail outgoing mail servers, it's disregarding the custom headers you sent and sending it from your actual Gmail account user. You might be able to get around this by adding the custom from email you want to use as a valid sender in Gmail's settings. Go to Settings and then the "Accounts and Import" tab. Find the section entitled, "Send mail as" and click the "Add another email address you own" link. You can add a new email account you want to send from there, and it will make you confirm the address via email (so it needs to be a valid address that can receive email).

like image 66
Chris Pratt Avatar answered Nov 15 '22 01:11

Chris Pratt


first you need to remove EMAIL_HOST_USER & EMAIL_HOST_PASSWORD in your setting.py and use auth_user and auth_password

auth_user:: If EMAIL_HOST_USER has not been specified, or you want to override it, this username will be used to authenticate to the SMTP server.

auth_password:: If EMAIL_HOST_PASSWORD has not been specified, this password will be used to authenticate to the SMTP server.

mail.send_mail(
        subject='here your subject',
        message='message',
        from_email='[email protected]',
        recipient_list=[to_email],
        auth_user = '[email protected]',
        auth_password = 'password',
        fail_silently=True,

)
like image 27
Sohel Baig Mirza Avatar answered Nov 15 '22 02:11

Sohel Baig Mirza