How to redefine address from django email address (sent via Gmail)

In my settings.py, I have the following values:

EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = ' user@gmail.com ' EMAIL_HOST_PASSWORD = 'pass' EMAIL_USE_TLS = True 

Then in my views, I get an email address from my models, for example, for example:

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

Assume email_address in the Profile model sample@gmail.com

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

so when I send a letter

 send_mail('subject', 'content', email, [' example1@example.com ']) 

When an email has already been sent, sender_email anyway user@gmail.com. Can someone teach me how to rewrite this email address? Thanks.

+6
source share
2 answers

The value DEFAULT_FROM_EMAIL is the default value. Django uses this in places where emails are sent automatically (for example, error reports prior to ADMINS ). When you call the send_mail method directly, you must specify the from_email parameter. Even if you want DEFAULT_FROM_EMAIL , you will have to import it from django.conf.settings , and then pass it.

My best guess is that Gmail is actually the culprit. As far as I know, Gmail does not allow you to specify a custom sender, because too many people are already trying to use Gmail to send spam, and they want to discourage this practice.

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

+8
source

David, are you sure your email is in your example?

From the documentation: https://docs.djangoproject.com/en/dev/topics/email/ from_email: sender address. Both fred@example.com and Fred forms are legal. If this parameter is omitted, the DEFAULT_FROM_EMAIL parameter is used.

Also: https://docs.djangoproject.com/en/dev/topics/email/#django.core.mail.send_mail Your use looks right:

 from django.core.mail import send_mail send_mail('Subject here', 'Here is the message.', ' from@example.com ', [' to@example.com '], fail_silently=False) 

Behind the top there can be no question. Perhaps your email address may be blank?

Hope this helps.

0
source

Source: https://habr.com/ru/post/903307/


All Articles