通过 Django 跟踪打开的电子邮件



嗨,我试图用PHP跟踪打开的邮件,但谷歌的新图像代理现在不允许通过邮件执行php脚本(可能是我的想象,如果有人有一个工作脚本,请指出我在那里,我尝试了这个 https://github.com/brampauwelyn/php-email-tracker(。所以我正在通过这篇文章在 Django 中尝试这样做

https://www.pythoncircle.com/post/626/how-to-track-email-opens-sent-from-django-app/

但它似乎正在工作,但我无法弄清楚如何实现它。他跳过了一些部分。这太令人困惑了。 现在我有

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from mailer import views
urlpatterns = [
url(r'^image_load/$', views.image_load, name='image_load'),
path('admin/', admin.site.urls),
]

views.py

def image_load(request):
print("nImage Loadedn")
red = Image.new('RGB', (1, 1))
response = HttpResponse(content_type="image/png")
red.save(response, "PNG")
return response

执行此操作时出现错误

text_content = '<h1>This is an image message.</h1>'
tracker = '<img src="{{image_url}}" alt="" width="1" height="1" border="0">'
text_content += tracker
text_content["image_url"] = HttpRequest.build_absolute_uri(reverse("image_load"))
print(context_data)

在第 4 行说

tracker['image_url'] = request.path('image_load'( TypeError: 'str' 对象不可调用

请帮助度过难关。

我使用一种稍微不同的方法完成了此操作。在此示例中,我跟踪哪些用户打开了电子邮件,但您可以使用另一种方法。在网址中:

url(r"^open-tracking/(?P<user>[0-9]+)/$", PixelView.as_view(), name="pixel_view")

在视图中:

import os.path
class PixelView(View):
def get(self, request, *args, **kwargs):
script_dir = os.path.dirname(os.path.abspath(__file__))
image_data = open(os.path.join(script_dir, 'static/img/open-tracking/pixel.png'), 'rb').read()
user_id = kwargs.get('user')
###Record somewhere that user_id has viewed the email
return HttpResponse(image_data, content_type="image/png")

在电子邮件中:

<img src="{{ settings.PROJECT_DOMAIN }}/open-tracking/{{ user.id }}/">

当电子邮件加载时,它会使用用户 ID 调用 PixelView 视图。PixelView 获取用户 ID(然后您可以使用该 ID 登录用户已阅读电子邮件的位置(,然后返回要在电子邮件中显示的像素图像。

需要注意的几件事是,1(像素/图像应该是1x1的透明图像。2(如果电子邮件客户端关闭了图像加载,则这不起作用。 3(电子邮件客户端有时会在实际打开电子邮件之前加载图像内容

这是使用pytracking包执行此操作的非常简单和有用的方法:

from pytracking import Configuration
from pytracking.django import OpenTrackingView, ClickTrackingView
class MyOpenTrackingView(OpenTrackingView):
def notify_tracking_event(self, tracking_result):
# Override this method to do something with the tracking result.
# tracking_result.request_data["user_agent"] and
# tracking_result.request_data["user_ip"] contains the user agent
# and ip of the client.
send_tracking_result_to_queue(tracking_result)
def notify_decoding_error(self, exception, request):
# Called when the tracking link cannot be decoded
# Override this to, for example, log the exception
logger.log(exception)
def get_configuration(self):
# By defaut, fetchs the configuration parameters from the Django
# settings. You can return your own Configuration object here if
# you do not want to use Django settings.
return Configuration()

class MyClickTrackingView(ClickTrackingView):
def notify_tracking_event(self, tracking_result):
# Override this method to do something with the tracking result.
# tracking_result.request_data["user_agent"] and
# tracking_result.request_data["user_ip"] contains the user agent
# and ip of the client.
send_tracking_result_to_queue(tracking_result)
def notify_decoding_error(self, exception, request):
# Called when the tracking link cannot be decoded
# Override this to, for example, log the exception
logger.log(exception)
def get_configuration(self):
# By defaut, fetchs the configuration parameters from the Django
# settings. You can return your own Configuration object here if
# you do not want to use Django settings.
return Configuration()

然后将其添加到 urls.py:

urlpatterns = [
url(
"^open/(?P<path>[w=-]+)/$", MyOpenTrackingView.as_view(),
name="open_tracking"),
url(
"^click/(?P<path>[w=-]+)/$", MyClickTrackingView.as_view(),
name="click_tracking"),
]

最新更新