简体   繁体   English

如何使用包含 javascript django 的 html 模板发送 email?

[英]How to send an email using a html template that contains javascript django?

I have an email template that is rendering certain datetime strings from django.我有一个 email 模板,它从 django 呈现某些日期时间字符串。 I would like to render this time based on the user's timezone.我想根据用户的时区呈现这个时间。 To solve this I have opted to use momentjs however once, it seems the javascript in the template is not run and anything wrapped in javascript doesn't appear in the email.为了解决这个问题,我选择使用momentjs ,但是一次,模板中的javascript 似乎没有运行,并且javascript 中包含的任何内容都没有出现在email 中。 Based from this answer https://stackoverflow.com/a/42056995/8513875 email clients don't include external css, I assume it's the same issue with javascript, how would I go about making this recognized. Based from this answer https://stackoverflow.com/a/42056995/8513875 email clients don't include external css, I assume it's the same issue with javascript, how would I go about making this recognized.

To expand on what @IainShelvington stated, and answer...扩展@IainShelvington 所说的内容,并回答...

@oma0256... how I can determine a user's timezone... @oma0256 ...我如何确定用户的时区...

... here's how to obtain the timezone offset via JavaScript (in browser) prior to generating the email... ...这是在生成 email 之前通过 JavaScript(在浏览器中)获取时区偏移量的方法...

const timezone = (new Date).getTimezoneOffset() / 60;

... note again, this will not run within an email, but instead should be used when generating the email from whatever template the service is using. ...再次注意,这不会在 email 中运行,而是应该在从服务使用的任何模板生成 email 时使用。

And regarding the second part of the question...关于问题的第二部分......

@oma0256... for instance based on their email? @oma0256 ...例如基于他们的email?

... if you're fortunate that information will be within the email headers, however, such things (much like any other client-side supplied data) cannot be trusted. ...如果您很幸运,信息将包含在 email 标头中,但是,这些东西(就像任何其他客户端提供的数据一样)是不可信的。

Attribution归因

MDN -- Date.prototype.getTimezoneOffset() MDN -- Date.prototype.getTimezoneOffset()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM