2017-04-12 9 views
0

私はdjangoの "send_mass_email"メソッドを使用して大量の電子メールを送信することをtriyにしていますが、私はドキュメントを読んでいますが、例ではプレーンテキストメッセージしか表示されません。Django send_mass_email with html content

「send_mass_emal」メソッドでHTMLメッセージを送信するにはどうすればよいですか?私は通常の電子メールのようにしようとしたが、唯一のHTMLコードをrecived。ここに私のコードです:

    for row_index in range(12, sheet.nrows): 
        if sheet.cell(rowx=row_index, colx=0).value != "": 
         template = get_template("MonthlyEmail.html") 
         context = Context({ 
          'name': str(clean_string(sheet.cell(rowx=row_index, colx=2).value)), 
          'doc_type': str(sheet.cell(rowx=row_index, colx=4).value), 
          'document': str(sheet.cell(rowx=row_index, colx=3).value), 
          'email': str(sheet.cell(rowx=row_index, colx=5).value), 
          'acc_numb': str(sheet.cell(rowx=row_index, colx=6).value), 
          'brute_salary': str(sheet.cell(rowx=row_index, colx=8).value), 
          'vacation_commision': str(sheet.cell(rowx=row_index, colx=9).value), 
          'brute_total': str(sheet.cell(rowx=row_index, colx=10).value), 
          'social_sec': str(sheet.cell(rowx=row_index, colx=14).value), 
          'isr': str(sheet.cell(rowx=row_index, colx=27).value), 
          'other_retention': str(sheet.cell(rowx=row_index, colx=13).value), 
          'net_payment': str(sheet.cell(rowx=row_index, colx=29).value) 
         }) 
         content = template.render(context) 

         messages.append(('Monthly Salary Report', content, 
                '[email protected]', 
                [str(sheet.cell(rowx=row_index, colx=5).value)])) 


       send_mass_mail_html(messages, fail_silently=False) 

答えて

0

send_mass_email()はHTMLメールをサポートしていません。しかし、Djangoのsend_mail()機能のインスピレーションfrom the codeを取ることによって、それを行うための方法があります:

connection = connection or get_connection(
    username=auth_user, 
    password=auth_password, 
    fail_silently=fail_silently, 
) 
mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection) 
if html_message: 
    mail.attach_alternative(html_message, 'text/html') 

mail.send() 

一般的な戦略は、最初の接続を作成しているようだ、その後、各電子メールのためにあなたは合格、EmailMultiAlternativesのインスタンスを作成し、送信する必要がありますそれは正確にsend_mailと同じですが、それを送信します。

1

私は、djangoドキュメントを参考にして大量のメールを使用する独自の機能を書いています。

from django.conf import settings 
from django.core.mail import EmailMultiAlternatives 
from django.template.loader import render_to_string 


def get_rendered_html(template_name, context={}): 
    html_content = render_to_string(template_name, context) 
    return html_content 


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) 


message1 = { 
    'subject': 'Subject here', 
    'text_content': 'Here is the message', 
    'from_email': '[email protected]', 
    'recipients': ['[email protected]', '[email protected]'], 
    'template': "template1.html", 
    'context': {"d1": "mydata"} 
} 

message2 = { 
    'subject': 'Subject here', 
    'text_content': 'Here is the message', 
    'from_email': '[email protected]', 
    'recipients': ['[email protected]', '[email protected]'], 
    'template': "template2.html", 
    'context': {"d2": "mydata"} 
} 

send_mass_mail([message1, message2]) 

参考:https://docs.djangoproject.com/en/1.11/_modules/django/core/mail/#send_mass_mail