-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
relative Urls in Email Links #8
Comments
Unfortunately this is a problem with Wagtail, if you have a single Site, all links inserted using richtext edit handler are relative instead of absolute. You can get around this by adding another dummy Site via the admin that isn't active. A better solution would be to use a hook to alter the richtext link handlers so they are always absolute or create a richtext field specific for emails that handles this? |
my solution was to add custom link handlers to wagtail_hooks
|
I'm not sure about making this the default approach for the plugin, this will affect all richtext fields, event outside of emails. This implementations also assumes we want the first site root path which isn't always correct, you could call |
good advice. i changed it as you suggested. but then i have to move it to editor.py. should i provide that code to you ? |
I was having an issue with relative URLs in emails also. While I'm sure there is a better way, my solution was to update the content variable in # birdsong/backends/smtp.py
from wagtail.core.models import Site # new
class SMTPEmailBackend(BaseEmailBackend):
def send_campaign(self, request, campaign, contacts, test_send=False):
# ...
root_url = Site.objects.get(is_default_site=True).root_url # new
for contact in contacts:
content = (
render_to_string(
campaign.get_template(request),
campaign.get_context(request, contact),
)
# replace relative link and document urls with absolute urls
.replace('href="/', f'href="{root_url}/') # new
# replace relative image urls with absolute urls
.replace('src="/', f'src="{root_url}/') # new
)
# ... Perhaps there is a better way to do this outside of |
I did it in the template: from django import template
from bs4 import BeautifulSoup
register = template.Library()
@register.filter
def absolutize_urls(value, request):
"""
Absolutize URL's in the given content using `request` from context.
"""
soup = BeautifulSoup(value, 'html.parser')
for tag in soup.find_all('a', href=True):
tag['href'] = request.build_absolute_uri(tag['href'])
for tag in soup.find_all('img', src=True):
tag['src'] = request.build_absolute_uri(tag['src'])
return str(soup) {% filter absolutize_urls:request %}
<body>
<p>
<a href="{% url 'some-view' %}">this URL will be made absolute using request.build_absolute_uri</a>
...
</body>
{% endfilter %} |
As far as I can see if you use the StreamlineField in the Email Body as described in the documentation:
class SaleEmail(Campaign): body = StreamField(DefaultBlocks())
All Page and Document Links inserted with the Editors Link Buttons have relative Urls.
The text was updated successfully, but these errors were encountered: