Reputation: 885
I'm trying to send images in a letter, but it doesn’t work out for me. I tuned {{ STATIC_URL }}
but it did not help... How i can fix this?
In settings: STATIC_URL = 'http://10.0.50.42:8103/static/'
In HTML: <img src="{{ STATIC_URL }}dc/images/banner2.png">
With this setting, I started to receive a letter, but the images are not displayed there.
Upvotes: 0
Views: 167
Reputation: 872
In my knowledge you cannot just put the link of image in the template that is rendered in the email and expect that it will work. As you are sending the image it still resides on your machine/server. The image must be saved as in, in a model and then you can you use that Model object to send as attachment.
Lets assume the file model is just below:
class ImageFile(models.Model):
file = models.ImageField(upload_to='Images',verbose_name='File Path', max_length=400, null=True)
and the send_mail function looks like:
def send_mail():
file_obj = ImageFile.objects.get(file='banner2.png')
file_url = file_obj.file.path
msg = EmailMessage(subject=subject, body=email_body, from_email=[frommail],
to=[tomail], bcc=[bcc mails])
msg.attach_file(file_url)
msg.send()
Upvotes: 0
Reputation: 1394
First of all you have to load static files using below command as first line of html...
{% load static %}
then use static as below in your <img>
...
<img src="{% static "dc/images/banner2.png" %}">
Upvotes: 1