2017-04-12 30 views
0

我正在用django的「send_mass_email」方法發送大量電子郵件,我已閱讀文檔,但示例僅顯示純文本消息。Django帶html內容的send_mass_email

我怎樣才能發送與「send_mass_emal」方法的HTML消息?我嘗試像一個普通的電子郵件,但只收到HTML代碼。這裏是我的代碼:

    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電子郵件。但是有一個辦法,採取靈感from the code Django的send_mail()功能來做到這一點:

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