In my settings.py, I have this values:
在我的settings.py中,我有这个值:
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 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 sample@gmail.com
我们假设Profile模型中的email_address是sample@gmail.com
#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, ['example1@example.com'])
When the email was already sent, the sender_email is still user@gmail.com. Can someone teach me how to overwrite this email address? Thanks.
当电子邮件已经发送时,sender_email仍然是user@gmail.com。有人可以教我如何覆盖这个电子邮件地址吗?谢谢。
2 个解决方案
#1
9
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.
DEFAULT_FROM_EMAIL设置就是默认设置。 Django在自动发送电子邮件的地方使用它(例如向ADMINS发送错误报告)。直接调用send_mail方法时,必须提供from_email参数。即使你想要DEFAULT_FROM_EMAIL,你也必须从django.conf.settings导入它然后传入它。
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.
我最好的猜测是Gmail实际上是罪魁祸首。据我所知,Gmail不允许您指定自定义发件人,因为太多人已经尝试使用Gmail发送垃圾邮件,并且他们希望阻止这种做法。
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).
基本上,当您的电子邮件通过Gmail外发邮件服务器时,它会忽略您发送的自定义标头并从您的实际Gmail帐户用户发送。您可以通过添加要在Gmail设置中用作有效发件人的电子邮件中的自定义来解决此问题。转到“设置”,然后转到“帐户和导入”选项卡。找到标题为“将邮件发送为”的部分,然后单击“添加您拥有的其他电子邮件地址”链接。您可以添加要从那里发送的新电子邮件帐户,它将使您通过电子邮件确认地址(因此它需要是可以接收电子邮件的有效地址)。
#2
0
David, are you sure your email is coming across in your example?
大卫,你确定你的电子邮件在你的例子中出现了吗?
From the documentation: https://docs.djangoproject.com/en/dev/topics/email/ from_email: The sender's address. Both fred@example.com and Fred forms are legal. If omitted, the DEFAULT_FROM_EMAIL setting is used.
从文档:https://docs.djangoproject.com/en/dev/topics/email/ from_email:发件人的地址。 fred@example.com和Fred表格都是合法的。如果省略,则使用DEFAULT_FROM_EMAIL设置。
Furthermore: https://docs.djangoproject.com/en/dev/topics/email/#django.core.mail.send_mail Your usage looks correct:
此外:https://docs.djangoproject.com/en/dev/topics/email/#django.core.mail.send_mail您的用法看起来正确:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)
Over riding may not be the issue. Your email may just be coming across blank?
过度骑行可能不是问题。您的电子邮件可能只是空白?
Hope that helps.
希望有所帮助。
#1
9
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.
DEFAULT_FROM_EMAIL设置就是默认设置。 Django在自动发送电子邮件的地方使用它(例如向ADMINS发送错误报告)。直接调用send_mail方法时,必须提供from_email参数。即使你想要DEFAULT_FROM_EMAIL,你也必须从django.conf.settings导入它然后传入它。
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.
我最好的猜测是Gmail实际上是罪魁祸首。据我所知,Gmail不允许您指定自定义发件人,因为太多人已经尝试使用Gmail发送垃圾邮件,并且他们希望阻止这种做法。
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).
基本上,当您的电子邮件通过Gmail外发邮件服务器时,它会忽略您发送的自定义标头并从您的实际Gmail帐户用户发送。您可以通过添加要在Gmail设置中用作有效发件人的电子邮件中的自定义来解决此问题。转到“设置”,然后转到“帐户和导入”选项卡。找到标题为“将邮件发送为”的部分,然后单击“添加您拥有的其他电子邮件地址”链接。您可以添加要从那里发送的新电子邮件帐户,它将使您通过电子邮件确认地址(因此它需要是可以接收电子邮件的有效地址)。
#2
0
David, are you sure your email is coming across in your example?
大卫,你确定你的电子邮件在你的例子中出现了吗?
From the documentation: https://docs.djangoproject.com/en/dev/topics/email/ from_email: The sender's address. Both fred@example.com and Fred forms are legal. If omitted, the DEFAULT_FROM_EMAIL setting is used.
从文档:https://docs.djangoproject.com/en/dev/topics/email/ from_email:发件人的地址。 fred@example.com和Fred表格都是合法的。如果省略,则使用DEFAULT_FROM_EMAIL设置。
Furthermore: https://docs.djangoproject.com/en/dev/topics/email/#django.core.mail.send_mail Your usage looks correct:
此外:https://docs.djangoproject.com/en/dev/topics/email/#django.core.mail.send_mail您的用法看起来正确:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)
Over riding may not be the issue. Your email may just be coming across blank?
过度骑行可能不是问题。您的电子邮件可能只是空白?
Hope that helps.
希望有所帮助。