Đây là cách tôi làm nhiệm vụ (kiểm tra mã):
from html2text import html2text
from django.utils.translation import ugettext as _
from django.core.mail import EmailMultiAlternatives, get_connection
def create_message(subject, message_plain, message_html, email_from, email_to,
custom_headers=None, attachments=None):
"""Build a multipart message containing a multipart alternative for text (plain, HTML) plus
all the attached files.
"""
if not message_plain and not message_html:
raise ValueError(_('Either message_plain or message_html should be not None'))
if not message_plain:
message_plain = html2text(message_html)
return {'subject': subject, 'body': message_plain, 'from_email': email_from, 'to': email_to,
'attachments': attachments or(), 'headers': custom_headers or {}}
def send_mass_html_mail(datatuple):
"""send mass EmailMultiAlternatives emails
see: http://stackoverflow.com/questions/7583801/send-mass-emails-with-emailmultialternatives
datatuple = ((subject, msg_plain, msg_html, email_from, email_to, custom_headers, attachments),)
"""
connection = get_connection()
messages = []
for subject, message_plain, message_html, email_from, email_to, custom_headers, attachments in datatuple:
msg = EmailMultiAlternatives(
**create_message(subject, message_plain, message_html, email_from, email_to, custom_headers, attachments))
if message_html:
msg.attach_alternative(message_html, 'text/html')
messages.append(msg)
return connection.send_messages(messages)
Nguồn
2015-11-05 15:42:29
Cám ơn câu trả lời của bạn, nhưng mỗi người dùng có thể xem tất cả các người dùng khác trong trường "TO". Tôi chỉ muốn một người nhận mỗi thư. – gustavgans
Sử dụng 'bcc' ví dụ: msg = EmailMultiAlternatives ("subject", text_content, "từ @ bla", ["đến @ bla"], bcc = ["to2 @ bla", "to3 @ bla"], kết nối = kết nối) – msanders
ok hoạt động, cảm ơn rất nhiều. – gustavgans