ui / django-post_office

A Django app that allows you to send email asynchronously in Django. Supports HTML email, database backed templates and logging.
MIT License
1k stars 270 forks source link

Multilingual Email Templates + inline images #328

Open mick912 opened 4 years ago

mick912 commented 4 years ago

How to send multilingual email templates with inline images?

jrief commented 4 years ago

Please explain, how your current attempt to create multilingual emails doesn't work.

mick912 commented 4 years ago

I created templates by this way template = EmailTemplate.objects.create( name='hello', subject='Hello world!', ) indonesian_template = template.translated_templates.create( language='id', subject='Halo Dunia!' )

And I`m trying to send inline images by this way https://github.com/ui/django-post_office#inlined-images But I see there is an only HTML file(located on templates dir) supported. I want to send inline images with EmailTemplate object. And my question is How to do it?

jrief commented 4 years ago

HTML templates for emails must be edited inside the Django Admin below Home › Post Office › Email Templates There in its detail view, you must add templates for each language not being the default. I hope this helps.

mick912 commented 4 years ago

Okay, thanks! I know where I can edit the templates.

setting.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(os.path.dirname(treebeard.__file__), "admin", "templates"),
            os.path.join(BASE_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
    {
        'BACKEND': 'post_office.template.backends.post_office.PostOfficeTemplates',
        'APP_DIRS': True,
        'DIRS': [],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.template.context_processors.request',
            ]
        }
    }
]

POST_OFFICE = {
    'TEMPLATE_ENGINE': 'post_office',
    'DEFAULT_PRIORITY': 'now',
    'LOG_LEVEL': 1,
}

my template is in Home › Post Office › Email Templates

{% load post_office %}
<img style="width: 100%" src="{% inline_image line.product.image.crop.137x145 %}" alt="">

trying to send

mail.send(
                seller.email,
                template='order_waiting',
                context={
                    'order': order,
                    'line': line,
                    'user': seller
                }
            )

And I got the exception

assert hasattr(context.template, '_attached_images'), \
        "You must use template engine 'post_office' when rendering images using templatetag 'inline_image'."
mick912 commented 4 years ago

To send an email containing HTML with inlined images, but without a plain text body, use this code snippet:

from django.core.mail import EmailMultiAlternatives

subject, from_email, to_email = "Hello", "no-reply@example.com", "john@example.com"
template = get_template('email-template-name.html', using='post_office')
context = {...}
html = template.render(context)
email_message = EmailMultiAlternatives(subject, html, from_email, [to_email])
email_message.content_subtype = 'html'
template.attach_related(email_message)
email_message.send()

How Can I use templates from DB here?