是否可以在wordpress中编辑所有已发送电子邮件的邮件正文



我正在测试是否可以编辑从wordpress发送的所有电子邮件的html代码。这是我直到现在想出的:

add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
add_filter('wp_mail', 'my_wp_mail');
function my_wp_mail($atts) {
$atts['message'] .= '  <br/><div id="result">my text line here</div>';
return $atts;
} 

上面的代码将在底部添加"我的文本行在这里"行,我正在寻找一种方法,以便能够使用 JavaScript 将div 替换为另一个页面的内容:

<script>
jQuery(document).ready(function(){
jQuery('#result').load('https://develop2020.000webhostapp.com/divdiv.html .myclass');
});
</script> 

然后是要发送的电子邮件。是否可以使用 JavaScript 来做到这一点,或者必须使用 PHP?如果可能,如何从 url 中提取div 并将其添加到所有电子邮件中?

欢迎来到堆栈溢出!

首先,在电子邮件中使用JS是不可能的。

使用Javascript发送或确定电子邮件的内容似乎非常麻烦,因为Wordpress通过PHP发送电子邮件。Javascript在浏览器中运行(当涉及到Wordpress时(而不是在服务器上运行,因此如果您尝试使用JS来发送电子邮件,则必须以某种方式在浏览器中打开页面以发送电子邮件。如果在服务器上执行某些操作,例如发送电子邮件,则希望使用PHP。

至于解决方案,更有意义的是使用 curl (https://www.php.net/manual/en/curl.examples-basic.php( 或file_get_contents(https://www.php.net/manual/en/function.file-get-contents.php( 将页面放入.load(),然后使用用于将内容添加到电子邮件底部的过滤器加载电子邮件的该部分。

如果你只想使用你通过curl/file_get_contents检索到的页面的一部分,你可以使用PHP的DOMdocument选择该部分:

https://www.tutorialspoint.com/php/php_dom_parser_example.htm https://www.php.net/manual/en/class.domdocument.php

我发现这种方式是这样工作的

add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
add_filter('wp_mail', 'my_wp_mail');
function my_wp_mail($atts) {
$url = 'https://develop2020.000webhostapp.com/divdiv.html';
$dom = new DOMDocument();
@$dom->loadHTMLFile($url);
$xpath = new DOMXpath($dom);
$elements = $xpath->query('//div[@class="myclass"]');
$link = $dom->saveHTML($elements->item(0));

$atts['message'] .= $link;
return $atts;
}

它有效,但现在我想从div 捕获div 的页面问题使用 ajax 加载..所以不确定有解决方案吗

最新更新