我嘗試向多個收件人發送電子郵件,但我做錯了。我正在使用 for 回圈從用戶那里獲取電子郵件地址。如果我列印這些電子郵件,它們的格式是這樣的:'[email protected]'而且我有不止一個。但是,如果我嘗試向他們發送電子郵件,則只有一個用戶會收到它。
如果重繪 urls.py 中的 html,它每次都會發送郵件。
視圖.py
from django.shortcuts import render
from django.core.mail import send_mass_mail
from somemodels.models import Project
import datetime
import calendar
def emails(request):
today = datetime.date.today()
weekday = today.weekday()
month = datetime.date.today().month
year = datetime.date.today().year
cal = calendar.monthrange(year, month)[1]
firstday = datetime.date.today().replace(day=1)
subject='hello'
message='how are you?'
from_email='[email protected]'
for p in Project.objects.raw('SELECT * FROM somemodels_project INNER JOIN auth_user ON auth_user.id = othermodel_project.permitted_users'):
recipient_list = p.email,
print(recipient_list)
if (today == firstday):
messages = [(subject, message, from_email, [recipient]) for recipient in recipient_list]
send_mass_mail(messages)
print('Successfully sent')
else:
print('Not sent')
return render(request, 'performance/emails.html')
網址.py
app_name = 'email'
urlpatterns = [
path('emails/', login_required(views.emails), name='emails'),
]
uj5u.com熱心網友回復:
獲取要向其發送電子郵件的人員串列
https://docs.djangoproject.com/en/4.0/topics/db/sql/
recipient_list = Project.objects.raw(...)
如果您的訊息沒有改變,您也可以使用
send_mail(
subject,
message,
from_email,
[r.email for r in recipient_list],
)
如果您想使用群發郵件,因為它更有效 https://docs.djangoproject.com/en/4.0/topics/email/
messages = [(subject, message, from_email, [r.email]) for r in recipient_list]
send_mass_mail(messages)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/455016.html
