我正在使用Django,anymail API& mailgun从我的网站发送电子邮件。
我有一个用户可以订阅的表单, 目前,当电子邮件发送到订阅者的电子邮件地址时,FROM地址默认为他们和我的电子邮件域的组合。
作为例外:
用户输入test@test.com,收到来自test=test.com@mail.mydomain.com的电子邮件,而不是我指定的info@mydomain.com
我很确定问题出在我的views.py中,但我不确定如何解决。
views.py
def send_email(subject, html_content, text_content=None, from_email=None, recipients=[], attachments=[], bcc=[], cc=[]):
# send email to user with attachment
if not from_email:
from_email = settings.DEFAULT_FROM_EMAIL
if not text_content:
text_content = ''
email = EmailMultiAlternatives(
subject, text_content, from_email, recipients, bcc=bcc, cc=cc
)
email.attach_alternative(html_content, "text/html")
for attachment in attachments:
# Example: email.attach('design.png', img_data, 'image/png')
email.attach(*attachment)
email.send()
def send_mass_mail(data_list):
for data in data_list:
template = data.pop('template')
context = data.pop('context')
html_content = get_rendered_html(template, context)
data.update({'html_content': html_content})
send_email(**data)
# Contact Form - home.html
def HomePage(request, template='home.html'):
form = ContactForm(request.POST or None)
if form.is_valid():
from_email = form.cleaned_data['from_email']
# Create message1 to send to poster
message1 = {
'subject': 'Test',
'from_email': from_email,
'recipients': [from_email],
'template': "marketing/welcome.html",
'context': {
"from_email": from_email,
}
}
# Create message1 to send to website admin
message2 = {
'subject': 'Test - Contact',
'from_email': from_email,
'recipients': ['info@mydomain.com'],
'template': "marketing/admin.html",
'context': {
"from_email": from_email,
}
}
try:
send_mass_mail([message1, message2])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('/thanks/')
context = {
"form": form,
}
return render(request, template, context)
答案 0 :(得分:0)
您好像使用了用户的from_email
,而不是info@mydomain.com
。在两个地方你有:
'from_email': from_email,
许多邮件提供商将限制您可以发送邮件的地址(阻止您使用其他人的地址发送垃圾邮件。
因此,您无法使用用户提供的from_email
。通常的方法是使用您自己的地址作为from_email
,但设置一个回复标题,以便任何回复转到用户指定的地址。