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.
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).
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,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With